Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #519 from parameter1/sitemap-images
Browse files Browse the repository at this point in the history
Preload sitemap images to prevent `o(n)` query issue
  • Loading branch information
solocommand authored Dec 15, 2022
2 parents f68c0a5 + 7ce057d commit 3b9849e
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions services/graphql-server/src/graphql/resolvers/platform/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,6 @@ module.exports = {
*/
ContentSitemapUrl: {
loc: (content, _, ctx) => createSitemapLoc(content, ctx),
images: (content, _, { basedb }) => loadSitemapImages({ content, basedb }),
},

/**
Expand Down Expand Up @@ -1157,10 +1156,29 @@ module.exports = {
sort,
});
const docs = [];
const imageIds = [];
await cursor.forEach((doc) => {
docs.push({ ...doc, changefreq, priority });
imageIds.push(...getAsArray(doc, 'images'));
});

const imageCursor = await basedb.findCursor('platform.Asset', {
...criteriaFor('assetImage'), _id: { $in: imageIds },
}, {
projection: {
name: 1,
caption: 1,
filePath: 1,
fileName: 1,
cropDimensions: 1,
},
});
return docs;

const imageMap = new Map(await imageCursor.map(image => [`${image._id}`, image]).toArray());
return docs.map(doc => ({
...doc,
images: getAsArray(doc, 'images').map(imageId => imageMap.get(`${imageId}`)).filter(v => v),
}));
},

contentSitemapNewsUrls: async (_, { input }, { basedb, site }) => {
Expand Down

0 comments on commit 3b9849e

Please sign in to comment.