From 915ab9813dabc3ac74fbd022075beed0867bed74 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 20 Dec 2019 10:22:30 -0500 Subject: [PATCH] approach to formatting dynamic refPaths --- demo/collections/Page.js | 2 +- demo/globals/Header.js | 1 - src/mongoose/autopopulate.plugin.js | 36 +++++++++++++++++-------- src/mongoose/schema/fieldToSchemaMap.js | 6 +---- src/mongoose/schema/schemaLoader.js | 16 +++++------ 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/demo/collections/Page.js b/demo/collections/Page.js index e06a2cbee5..ffb867176e 100644 --- a/demo/collections/Page.js +++ b/demo/collections/Page.js @@ -79,7 +79,7 @@ module.exports = { name: 'blocks', label: 'Flexible Content Blocks', type: 'flexible', - blocks: ['Quote', 'cta'], + blocks: ['quote', 'cta'], localized: true, }, { diff --git a/demo/globals/Header.js b/demo/globals/Header.js index 581d685ad2..4ea0da651c 100644 --- a/demo/globals/Header.js +++ b/demo/globals/Header.js @@ -36,7 +36,6 @@ module.exports = { type: 'flexible', blocks: ['quote', 'cta'], localized: true, - hasMany: false, }, { // TODO: this is some proof of concept parts that are not done. diff --git a/src/mongoose/autopopulate.plugin.js b/src/mongoose/autopopulate.plugin.js index a441b28733..f6e24e4887 100644 --- a/src/mongoose/autopopulate.plugin.js +++ b/src/mongoose/autopopulate.plugin.js @@ -5,6 +5,7 @@ module.exports = function (schema) { let option; if (schemaType.options && schemaType.options.autopopulate) { option = schemaType.options.autopopulate; + pathsToPopulate.push({ options: defaultOptions(pathname, schemaType.options), autopopulate: option @@ -32,7 +33,9 @@ module.exports = function (schema) { }); } - var autopopulateHandler = function () { + console.log(JSON.stringify(pathsToPopulate)); + + function autopopulateHandler() { if (this._mongooseOptions && this._mongooseOptions.lean && // If lean and user didn't explicitly do `lean({ autopulate: true })`, @@ -69,7 +72,7 @@ module.exports = function (schema) { processOption.call(this, pathsToPopulate[i].autopopulate, pathsToPopulate[i].options); } - }; + } schema. pre('find', autopopulateHandler). @@ -125,20 +128,31 @@ function handleFunction(fn, options) { processOption.call(this, val, options); } -function mergeOptions(destination, source) { - const keys = Object.keys(source); - const numKeys = keys.length; - for (let i = 0; i < numKeys; ++i) { - destination[keys[i]] = source[keys[i]]; - } -} +function eachPathRecursive(currentSchema, handler, path) { -function eachPathRecursive(schema, handler, path) { if (!path) { path = []; } - schema.eachPath((pathname, schemaType) => { + + currentSchema.eachPath((pathname, schemaType) => { path.push(pathname); + + if (schemaType.options.refPath && schemaType.options.refPath.includes('{{LOCALE}}')) { + currentSchema.remove(pathname); + + // If we can get a value for 'en' here, we can fix everything + + schemaType.options.refPath = schemaType.options.refPath.replace('{{LOCALE}}', 'en'); + + currentSchema.add({ + [pathname]: { + ...schemaType.options, + }, + }); + + currentSchema.tree[pathname].refPath = schemaType.options.refPath; + } + if (schemaType.schema) { eachPathRecursive(schemaType.schema, handler, path); } else { diff --git a/src/mongoose/schema/fieldToSchemaMap.js b/src/mongoose/schema/fieldToSchemaMap.js index 432d5d801a..eae2bd6047 100644 --- a/src/mongoose/schema/fieldToSchemaMap.js +++ b/src/mongoose/schema/fieldToSchemaMap.js @@ -1,6 +1,4 @@ import mongoose from 'mongoose'; -import localizationPlugin from '../../localization/localization.plugin'; -import autopopulate from '../autopopulate.plugin'; const formatBaseSchema = field => { return { @@ -67,14 +65,12 @@ const fieldToSchemaMap = { value: { autopopulate: true, type: mongoose.Types.ObjectId, - refPath: `${options.path ? (options.path + '.') : ''}${field.name}.blockType`, + refPath: `${options.path ? (options.path + '.') : ''}${field.name}${field.localized ? '.{{LOCALE}}' : ''}.blockType`, }, blockType: { type: String, enum: field.blocks }, _id: false, }); - schema.plugin(autopopulate); - return { localized: field.localized || false, type: [schema], diff --git a/src/mongoose/schema/schemaLoader.js b/src/mongoose/schema/schemaLoader.js index bbea814a03..419b9188df 100644 --- a/src/mongoose/schema/schemaLoader.js +++ b/src/mongoose/schema/schemaLoader.js @@ -51,7 +51,7 @@ class SchemaLoader { .plugin(paginate) .plugin(localizationPlugin, config.localization) .plugin(buildQueryPlugin) - .plugin(autopopulate); + .plugin(autopopulate, config.localization); const Model = mongoose.model(blockConfig.slug, Schema); @@ -64,7 +64,7 @@ class SchemaLoader { } }, { _id: false } ); - RefSchema.plugin(autopopulate); + RefSchema.plugin(autopopulate, config.localization); Object.values(flexibleSchema).forEach(flexible => { flexible.blocks.forEach(blockType => { @@ -97,14 +97,14 @@ class SchemaLoader { collectionConfig.fields.forEach(field => { const fieldSchema = fieldToSchemaMap[field.type]; - if (fieldSchema) fields[field.name] = fieldSchema(field, {localization: config.localization}); + if (fieldSchema) fields[field.name] = fieldSchema(field, { localization: config.localization }); }); const Schema = new mongoose.Schema(fields, { timestamps: collectionConfig.timestamps }) .plugin(paginate) .plugin(buildQueryPlugin) .plugin(localizationPlugin, config.localization) - .plugin(autopopulate); + .plugin(autopopulate, config.localization); if (collectionConfig.plugins) { collectionConfig.plugins.forEach(plugin => { @@ -129,11 +129,11 @@ class SchemaLoader { globalConfig.fields.forEach(field => { const fieldSchema = fieldToSchemaMap[field.type]; - if (fieldSchema) globalFields[globalConfig.slug][field.name] = fieldSchema(field, {path: globalConfig.slug, localization: config.localization}); + if (fieldSchema) globalFields[globalConfig.slug][field.name] = fieldSchema(field, { path: globalConfig.slug, localization: config.localization }); }); globalSchemaGroups[globalConfig.slug] = new mongoose.Schema(globalFields[globalConfig.slug], { _id: false }) - .plugin(localizationPlugin, config.localization) - .plugin(autopopulate); + .plugin(localizationPlugin, config.localization) + .plugin(autopopulate); }); if (config.globals) { @@ -141,7 +141,7 @@ class SchemaLoader { 'global', new mongoose.Schema({ ...globalSchemaGroups, timestamps: false }) .plugin(localizationPlugin, config.localization) - .plugin(autopopulate) + .plugin(autopopulate, config.localization) ); } };