debugging issues with autoload for flexible schemas

This commit is contained in:
Dan Ribbens
2019-10-28 18:59:01 -04:00
parent c10d6fd34f
commit 897ef934f0
4 changed files with 36 additions and 33 deletions

View File

@@ -66,7 +66,7 @@ class Payload {
Object.values(this.schemaLoader.contentBlocks)
.forEach(block => {
const config = block.config;
const model = block.model;
const model = block.Model;
options.router.all(`/${config.slug}*`,
bindModelMiddleware(model),

View File

@@ -3,6 +3,10 @@ module.exports = function (schema) {
eachPathRecursive(schema, (pathname, schemaType) => {
let option;
// console.log(pathname);
// if (pathname === 'relation') {
// console.log(schemaType.options);
// }
if (schemaType.options && schemaType.options.autopopulate) {
option = schemaType.options.autopopulate;
pathsToPopulate.push({
@@ -137,9 +141,11 @@ function eachPathRecursive(schema, handler, path) {
if (!path) {
path = [];
}
// console.log(path, schema);
schema.eachPath((pathname, schemaType) => {
path.push(pathname);
if (schemaType.schema) {
console.log(pathname, schemaType.schema);
eachPathRecursive(schemaType.schema, handler, path);
} else {
handler(path.join('.'), schemaType);

View File

@@ -1,24 +1,24 @@
const modelById = (query, options) => {
return new Promise((resolve, reject) => {
query.Model.findOne({ _id: query.id }, {}, options, (err, doc) => {
if (err || !doc) {
return reject({ message: 'not found' })
}
let result = doc;
if (query.locale) {
doc.setLocale(query.locale, query.fallback);
result = doc.toJSON({ virtuals: true });
}
resolve(options.returnRawDoc
? doc
: result);
})
})
};
export default modelById;
const modelById = (query, options) => {
return new Promise((resolve, reject) => {
query.Model.findOne({ _id: query.id }, {}, options, (err, doc) => {
if (err || !doc) {
return reject({ message: 'not found' })
}
let result = doc;
if (query.locale) {
doc.setLocale && doc.setLocale(query.locale, query.fallback);
result = doc.toJSON({ virtuals: true });
}
resolve(options.returnRawDoc
? doc
: result);
})
})
};
export default modelById;

View File

@@ -46,13 +46,10 @@ this.blockSchema.plugin(autopopulate);
flexibleSchema[field.name] = field;
}
});
// TODO: instead of making the model with all the fields, create a separate model
// replace block schema with a ref to the new model type
const Schema = new mongoose.Schema(fields)
.plugin(paginate)
.plugin(buildQueryPlugin)
// .plugin(localizationPlugin, config.localization)
.plugin(autopopulate);
const Model = mongoose.model(blockConfig.slug, Schema);
@@ -64,7 +61,7 @@ this.blockSchema.plugin(autopopulate);
autopopulate: true,
ref: blockConfig.slug,
}
}
}, {_id: false}
);
RefSchema.plugin(autopopulate);
@@ -78,9 +75,9 @@ this.blockSchema.plugin(autopopulate);
this.contentBlocks[blockConfig.slug] = {
config: blockConfig,
schema: Schema,
refSchema: RefSchema,
model: Model
Schema,
RefSchema,
Model,
};
});
};
@@ -116,7 +113,7 @@ this.blockSchema.plugin(autopopulate);
Object.values(flexibleSchema).forEach(flexible => {
flexible.blocks.forEach(blockType => {
Schema.path(flexible.name)
.discriminator(blockType, this.contentBlocks[blockType].refSchema);
.discriminator(blockType, this.contentBlocks[blockType].RefSchema);
});
});