approach to formatting dynamic refPaths

This commit is contained in:
James
2019-12-20 10:22:30 -05:00
parent a69d22c1d1
commit 915ab9813d
5 changed files with 35 additions and 26 deletions

View File

@@ -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 {

View File

@@ -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],

View File

@@ -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)
);
}
};