Workspace from MongoDB source (no cache)
Commonplace added the getDynamicWorkspace method to the workspace cache module.
module.exports = async (req) => {
if (process.env.WORKSPACE === "dynamic") {
return await getDynamicWorkspace(req);
}
}
async function getDynamicWorkspace(req) {
try {
if (!req) return new Error("Not request found");
const { pageId, project, lang } = req.query;
const { mongoClient } = req;
if (!pageId) {
return new Error("No pageId specified");
}
if (!project) {
return new Error("No project specified");
}
if (!lang) {
return new Error("No lang specified");
}
const { ObjectId } = require("mongodb");
const Page = mongoClient.db("acorn").collection("pages");
const Version = mongoClient.db("acorn").collection("versions");
const proposalPage = await Page.findOne({ _id: ObjectId(pageId) });
if (!proposalPage) {
console.log('Proposal Page not found', pageId);
return;
} // get the most recent version id
const contentVersionId = proposalPage?.content[lang][0];
if (!contentVersionId) {
console.log('Content Version Id not found');
return;
}
const proposalContent = await Version.findOne({
_id: ObjectId(contentVersionId),
});
workspace = proposalContent.content.geolytixWorkspace;
await assignTemplates(workspace);
await assignDefaults(workspace);
if(!workspace){
console.log('No workspace found on cache.js');
}
return workspace;
} catch (err) {
return new Error("Error loading workspace: " + err);
}
}
A sample workspace is available here.
Whether a workspace should be cached should be controlled by a separate environment flag.
A provider method must be introduced to allow resource access to a MongoDB collection.
It is now possible to load the workspace from a mongodb document.
https://github.com/GEOLYTIX/xyz/wiki/XYZ#workspace
https://github.com/GEOLYTIX/xyz/wiki/XYZ#mongodb
It is also possible to configure the WORKSPACE_AGE environment variable after which the workspace cache will be refreshed.
https://github.com/GEOLYTIX/xyz/wiki/XYZ#cache
If you set this to 0 the workspace will be cached on every call. Though that is not recommended due to work on the process side.