Skip to content

Commit d92026c

Browse files
feat: addCollection API method
1 parent 0e236e3 commit d92026c

File tree

23 files changed

+169
-107
lines changed

23 files changed

+169
-107
lines changed

packages/endpoint-json-feed/lib/controllers/json-feed.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { jsonFeed } from "../json-feed.js";
33
export const jsonFeedController = async (request, response) => {
44
const { application } = request.app.locals;
55
const feedUrl = new URL(request.originalUrl, application.url).href;
6-
const posts = await application.posts
6+
7+
const postsCollection = application?.collections?.get("posts");
8+
const posts = await postsCollection
79
.find({
810
"properties.post-status": {
911
$ne: "draft",

packages/endpoint-media/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default class MediaEndpoint {
2929
}
3030

3131
init(Indiekit) {
32+
Indiekit.addCollection("media");
3233
Indiekit.addEndpoint(this);
3334

3435
// Use private value to register Micropub media endpoint path

packages/endpoint-media/lib/controllers/query.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getMediaProperties } from "../utils.js";
88
*/
99
export const queryController = async (request, response, next) => {
1010
const { application } = request.app.locals;
11+
const mediaCollection = application.collections.get("media");
1112

1213
try {
1314
const limit = Number(request.query.limit) || 0;
@@ -25,8 +26,8 @@ export const queryController = async (request, response, next) => {
2526
// Return properties for a given URL
2627
let mediaData;
2728

28-
if (application.media) {
29-
mediaData = await application.media.findOne({
29+
if (mediaCollection) {
30+
mediaData = await mediaCollection.findOne({
3031
"properties.url": url,
3132
});
3233
}
@@ -46,8 +47,8 @@ export const queryController = async (request, response, next) => {
4647
hasPrev: false,
4748
};
4849

49-
if (application.media) {
50-
cursor = await getCursor(application.media, after, before, limit);
50+
if (mediaCollection) {
51+
cursor = await getCursor(mediaCollection, after, before, limit);
5152
}
5253

5354
response.json({

packages/endpoint-media/lib/media-data.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const mediaData = {
1717
async create(application, publication, file) {
1818
debug(`Create %O`, { file });
1919

20-
const { media, timeZone } = application;
20+
const { timeZone } = application;
2121
const { me, postTypes } = publication;
2222

2323
// Media properties
@@ -59,9 +59,10 @@ export const mediaData = {
5959
const mediaData = { path, properties };
6060

6161
// Add data to media collection (or replace existing if present)
62-
if (media) {
62+
const mediaCollection = application?.collections?.get("media");
63+
if (mediaCollection) {
6364
const query = { "properties.url": properties.url };
64-
await media.replaceOne(query, mediaData, { upsert: true });
65+
await mediaCollection.replaceOne(query, mediaData, { upsert: true });
6566
}
6667

6768
return mediaData;
@@ -76,10 +77,10 @@ export const mediaData = {
7677
async read(application, url) {
7778
debug(`Read ${url}`);
7879

79-
const { media } = application;
8080
const query = { "properties.url": url };
8181

82-
const mediaData = await media.findOne(query);
82+
const mediaCollection = application?.collections?.get("media");
83+
const mediaData = await mediaCollection.findOne(query);
8384
if (!mediaData) {
8485
throw IndiekitError.notFound(url);
8586
}
@@ -96,10 +97,10 @@ export const mediaData = {
9697
async delete(application, url) {
9798
debug(`Delete ${url}`);
9899

99-
const { media } = application;
100100
const query = { "properties.url": url };
101101

102-
const result = await media.deleteOne(query);
102+
const mediaCollection = application?.collections?.get("media");
103+
const result = await mediaCollection.deleteOne(query);
103104
if (result?.deletedCount === 1) {
104105
return true;
105106
}

packages/endpoint-media/lib/media-type-count.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
export const mediaTypeCount = {
22
/**
33
* Count the number of media of a given type
4-
* @param {object} application - Application configuration
4+
* @param {object} postsCollection - Posts database collection
55
* @param {object} properties - Media properties
66
* @returns {Promise<object>} Media count
77
*/
8-
async get(application, properties) {
9-
if (!application.posts || !application.posts.count()) {
8+
async get(postsCollection, properties) {
9+
if (!postsCollection || !postsCollection.count()) {
1010
console.warn("No database configuration provided");
1111
console.info(
1212
"See https://getindiekit.com/configuration/application/#mongodburl",
@@ -20,7 +20,7 @@ export const mediaTypeCount = {
2020
const startDate = new Date(new Date(properties.published).toDateString());
2121
const endDate = new Date(startDate);
2222
endDate.setDate(endDate.getDate() + 1);
23-
const response = await application.posts
23+
const response = await postsCollection
2424
.aggregate([
2525
{
2626
$addFields: {

packages/endpoint-media/lib/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export const renderPath = async (path, properties, application) => {
4848
tokens.D60 = newbase60.DateToSxg(dateObject);
4949

5050
// Add count of media type for the day
51-
const count = await mediaTypeCount.get(application, properties);
51+
const postsCollection = application?.collections?.get("posts");
52+
const count = await mediaTypeCount.get(postsCollection, properties);
5253
tokens.n = count + 1;
5354

5455
// Add file extension token

packages/endpoint-media/test/unit/media-data.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ describe("endpoint-media/lib/media-data", async () => {
99
let application;
1010
let publication;
1111
const { client, database, mongoServer } = await testDatabase();
12-
const media = database.collection("media");
1312
const file = {
1413
data: getFixture("file-types/photo.jpg", false),
1514
name: "photo.jpg",
@@ -22,7 +21,8 @@ describe("endpoint-media/lib/media-data", async () => {
2221
});
2322

2423
beforeEach(async () => {
25-
await media.insertOne({
24+
const mediaCollection = database.collection("media");
25+
await mediaCollection.insertOne({
2626
path: "photo.jpg",
2727
properties: {
2828
"media-type": "photo",
@@ -31,8 +31,10 @@ describe("endpoint-media/lib/media-data", async () => {
3131
});
3232

3333
const config = await testConfig({ usePostTypes: true });
34+
const collections = new Map();
35+
collections.set("media", mediaCollection);
3436

35-
application = { media };
37+
application = { collections };
3638
publication = config.publication;
3739
});
3840

packages/endpoint-media/test/unit/media-type-count.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { mediaTypeCount } from "../../lib/media-type-count.js";
55

66
const { client, database, mongoServer } = await testDatabase();
77
const posts = database.collection("posts");
8-
const application = { posts };
98

109
describe("endpoint-media/lib/media-type-count", () => {
1110
before(async () => {
@@ -25,7 +24,7 @@ describe("endpoint-media/lib/media-type-count", () => {
2524
});
2625

2726
it("Counts the number of media of a given type", async () => {
28-
const result = await mediaTypeCount.get(application, {
27+
const result = await mediaTypeCount.get(posts, {
2928
type: "entry",
3029
published: new Date(),
3130
name: "Bar",

packages/endpoint-micropub/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default class MicropubEndpoint {
2020
}
2121

2222
init(Indiekit) {
23+
Indiekit.addCollection("posts");
2324
Indiekit.addEndpoint(this);
2425

2526
// Use private value to register Micropub endpoint path

packages/endpoint-micropub/lib/controllers/query.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getMf2Properties, jf2ToMf2 } from "../mf2.js";
99
*/
1010
export const queryController = async (request, response, next) => {
1111
const { application, publication } = request.app.locals;
12+
const postsCollection = application?.collections?.get("posts");
1213

1314
try {
1415
const config = getConfig(application, publication);
@@ -40,8 +41,8 @@ export const queryController = async (request, response, next) => {
4041
// Return mf2 for a given URL (optionally filtered by properties)
4142
let postData;
4243

43-
if (application.posts) {
44-
postData = await application.posts.findOne({
44+
if (postsCollection) {
45+
postData = await postsCollection.findOne({
4546
"properties.url": url,
4647
});
4748
}
@@ -62,8 +63,8 @@ export const queryController = async (request, response, next) => {
6263
hasPrev: false,
6364
};
6465

65-
if (application.posts) {
66-
cursor = await getCursor(application.posts, after, before, limit);
66+
if (postsCollection) {
67+
cursor = await getCursor(postsCollection, after, before, limit);
6768
}
6869

6970
const items = [];

packages/endpoint-micropub/lib/post-data.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const postData = {
2121
async create(application, publication, properties, draftMode = false) {
2222
debug(`Create %O`, { draftMode, properties });
2323

24-
const { posts, timeZone } = application;
24+
const { timeZone } = application;
2525
const { me, postTypes, syndicationTargets } = publication;
2626

2727
// Add syndication targets
@@ -67,9 +67,10 @@ export const postData = {
6767
const postData = { path, properties };
6868

6969
// Add data to posts collection (or replace existing if present)
70-
if (posts) {
70+
const postsCollection = application?.collections?.get("posts");
71+
if (postsCollection) {
7172
const query = { "properties.url": properties.url };
72-
await posts.replaceOne(query, postData, { upsert: true });
73+
await postsCollection.replaceOne(query, postData, { upsert: true });
7374
}
7475

7576
return postData;
@@ -84,10 +85,10 @@ export const postData = {
8485
async read(application, url) {
8586
debug(`Read ${url}`);
8687

87-
const { posts } = application;
8888
const query = { "properties.url": url };
89+
const postsCollection = application?.collections?.get("posts");
8990

90-
const postData = await posts.findOne(query);
91+
const postData = await postsCollection.findOne(query);
9192
if (!postData) {
9293
throw IndiekitError.notFound(url);
9394
}
@@ -108,8 +109,9 @@ export const postData = {
108109
async update(application, publication, url, operation) {
109110
debug(`Update ${url} %O`, { operation });
110111

111-
const { posts, timeZone } = application;
112+
const { timeZone } = application;
112113
const { me, postTypes } = publication;
114+
const postsCollection = application?.collections?.get("posts");
113115

114116
// Read properties
115117
let { path: _originalPath, properties } = await this.read(application, url);
@@ -171,7 +173,7 @@ export const postData = {
171173
// Update data in posts collection
172174
const postData = { _originalPath, path, properties };
173175
const query = { "properties.url": url };
174-
await posts.replaceOne(query, postData);
176+
await postsCollection.replaceOne(query, postData);
175177

176178
return postData;
177179
},
@@ -188,8 +190,9 @@ export const postData = {
188190
async delete(application, publication, url) {
189191
debug(`Delete ${url}`);
190192

191-
const { posts, timeZone } = application;
193+
const { timeZone } = application;
192194
const { postTypes } = publication;
195+
const postsCollection = application?.collections?.get("posts");
193196

194197
// Read properties
195198
const { properties } = await this.read(application, url);
@@ -222,7 +225,7 @@ export const postData = {
222225
// Update data in posts collection
223226
const postData = { path, properties, _deletedProperties };
224227
const query = { "properties.url": url };
225-
await posts.replaceOne(query, postData);
228+
await postsCollection.replaceOne(query, postData);
226229

227230
return postData;
228231
},
@@ -240,8 +243,8 @@ export const postData = {
240243
async undelete(application, publication, url, draftMode) {
241244
debug(`Undelete ${url} %O`, { draftMode });
242245

243-
const { posts } = application;
244246
const { postTypes } = publication;
247+
const postsCollection = application?.collections?.get("posts");
245248

246249
// Read deleted properties
247250
const { _deletedProperties } = await this.read(application, url);
@@ -270,7 +273,7 @@ export const postData = {
270273
// Update data in posts collection
271274
const postData = { path, properties };
272275
const query = { "properties.url": url };
273-
await posts.replaceOne(query, postData);
276+
await postsCollection.replaceOne(query, postData);
274277

275278
return postData;
276279
},

packages/endpoint-micropub/lib/post-type-count.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { getObjectId } from "@indiekit/util";
33
export const postTypeCount = {
44
/**
55
* Count the number of posts of a given type
6-
* @param {object} application - Application configuration
6+
* @param {object} postsCollection - Posts database collection
77
* @param {object} properties - JF2 properties
88
* @returns {Promise<object>} Post count
99
*/
10-
async get(application, properties) {
11-
if (!application.posts || !application.posts.count()) {
10+
async get(postsCollection, properties) {
11+
if (!postsCollection || !postsCollection.count()) {
1212
console.warn("No database configuration provided");
1313
console.info(
1414
"See https://getindiekit.com/configuration/application/#mongodburl",
@@ -23,7 +23,7 @@ export const postTypeCount = {
2323
const startDate = new Date(new Date(properties.published).toDateString());
2424
const endDate = new Date(startDate);
2525
endDate.setDate(endDate.getDate() + 1);
26-
const response = await application.posts
26+
const response = await postsCollection
2727
.aggregate([
2828
{
2929
$addFields: {

packages/endpoint-micropub/lib/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ export const renderPath = async (
111111
tokens.D60 = newbase60.DateToSxg(dateObject);
112112

113113
// Add count of post-type for the day
114-
const count = await postTypeCount.get(application, properties);
114+
const postsCollection = application?.collections?.get("posts");
115+
const count = await postTypeCount.get(postsCollection, properties);
115116
tokens.n = count + 1;
116117

117118
// Add slug token

packages/endpoint-micropub/test/unit/post-data.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ describe("endpoint-micropub/lib/post-data", async () => {
88
let application;
99
let publication;
1010
const { client, database, mongoServer } = await testDatabase();
11-
const posts = database.collection("posts");
1211
const properties = {
1312
type: "entry",
1413
published: "2020-07-26T20:10:57.062Z",
@@ -23,7 +22,8 @@ describe("endpoint-micropub/lib/post-data", async () => {
2322
});
2423

2524
beforeEach(async () => {
26-
await posts.insertOne({
25+
const postsCollection = database.collection("posts");
26+
await postsCollection.insertOne({
2727
path: "foo",
2828
properties: {
2929
type: "entry",
@@ -38,8 +38,10 @@ describe("endpoint-micropub/lib/post-data", async () => {
3838
});
3939

4040
const config = await testConfig({ usePostTypes: true });
41+
const collections = new Map();
42+
collections.set("posts", postsCollection);
4143

42-
application = { posts };
44+
application = { collections };
4345
publication = config.publication;
4446
});
4547

packages/endpoint-micropub/test/unit/post-type-count.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { postTypeCount } from "../../lib/post-type-count.js";
55

66
const { client, database, mongoServer } = await testDatabase();
77
const posts = database.collection("posts");
8-
const application = { posts };
98

109
describe("endpoint-media/lib/post-type-count", () => {
1110
before(async () => {
@@ -27,7 +26,7 @@ describe("endpoint-media/lib/post-type-count", () => {
2726

2827
it("Counts the number of posts of a given type", async () => {
2928
const post = await posts.findOne({});
30-
const result = await postTypeCount.get(application, {
29+
const result = await postTypeCount.get(posts, {
3130
uid: post._id.toString(),
3231
type: "entry",
3332
published: new Date(),

0 commit comments

Comments
 (0)