enables localized flexible content
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import mongoose from 'mongoose';
|
||||
import mongooseHidden from 'mongoose-hidden';
|
||||
import paginate from 'mongoose-paginate-v2';
|
||||
import autopopulate from 'mongoose-autopopulate';
|
||||
import buildQueryPlugin from '../mongoose/buildQuery';
|
||||
import localizationPlugin from '../localization/plugin';
|
||||
import passwordResetConfig from '../auth/passwordResets/config';
|
||||
import buildSchema from '../mongoose/schema/buildSchema';
|
||||
|
||||
@@ -14,7 +16,9 @@ const addSchema = (collection, config) => {
|
||||
|
||||
schema.plugin(paginate)
|
||||
.plugin(buildQueryPlugin)
|
||||
.plugin(autopopulate);
|
||||
.plugin(localizationPlugin, config.localization)
|
||||
.plugin(autopopulate)
|
||||
.plugin(mongooseHidden());
|
||||
|
||||
if (collection.plugins) {
|
||||
collection.plugins.forEach((plugin) => {
|
||||
|
||||
@@ -105,14 +105,12 @@ export default function localizationPlugin(schema, options) {
|
||||
this.set(`${path}.${owner.getLocale()}`, value);
|
||||
});
|
||||
|
||||
|
||||
// localized option is not needed for the current path any more,
|
||||
// and is unwanted for all child locale-properties
|
||||
// delete schemaType.options.localized; // This was removed to allow viewing inside query parser
|
||||
|
||||
const localizedObject = {};
|
||||
// TODO: setting equal to object is good for hasMany: false, but breaking for hasMany: true;
|
||||
// console.log(path, schemaType.options);
|
||||
localizedObject[key] = {};
|
||||
pluginOptions.locales.forEach(function (locale) {
|
||||
const localeOptions = Object.assign({}, schemaType.options);
|
||||
@@ -135,6 +133,16 @@ export default function localizationPlugin(schema, options) {
|
||||
schema.add(localizedObject, prefix);
|
||||
});
|
||||
|
||||
schema.eachPath((path, schemaType) => {
|
||||
if (schemaType.schema && schemaType.schema.discriminators) {
|
||||
Object.keys(schemaType.schema.discriminators).forEach((key) => {
|
||||
if (schema.path(path)) {
|
||||
schema.path(path).discriminator(key, schemaType.schema.discriminators[key]);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// document methods to set the locale for each model instance (document)
|
||||
schema.method({
|
||||
getLocales() {
|
||||
@@ -177,14 +185,16 @@ export default function localizationPlugin(schema, options) {
|
||||
},
|
||||
setDefaultLocale(locale) {
|
||||
const updateLocale = function (schemaToUpdate, localeToUpdate) {
|
||||
schemaToUpdate.options.localization.defaultLocale = localeToUpdate.slice(0);
|
||||
if (schemaToUpdate.options.localization) {
|
||||
schemaToUpdate.options.localization.defaultLocale = localeToUpdate.slice(0);
|
||||
|
||||
// default locale change for sub-documents schemas
|
||||
schemaToUpdate.eachPath((path, schemaType) => {
|
||||
if (schemaType.schema) {
|
||||
updateLocale(schemaType.schema, localeToUpdate);
|
||||
}
|
||||
});
|
||||
// default locale change for sub-documents schemas
|
||||
schemaToUpdate.eachPath((path, schemaType) => {
|
||||
if (schemaType.schema) {
|
||||
updateLocale(schemaType.schema, localeToUpdate);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
updateLocale(this.schema, locale);
|
||||
|
||||
@@ -11,6 +11,7 @@ const query = (req, res) => {
|
||||
|
||||
req.model.paginate(req.model.apiQuery(req.query, req.locale), { options: queryOptions }, (err, result) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err });
|
||||
}
|
||||
return res.json({
|
||||
|
||||
@@ -1,34 +1,35 @@
|
||||
import { Schema } from 'mongoose';
|
||||
import mongooseHidden from 'mongoose-hidden';
|
||||
import fieldToSchemaMap from './fieldToSchemaMap';
|
||||
import baseFields from './baseFields';
|
||||
import localizationPlugin from '../../localization/plugin';
|
||||
|
||||
const buildSchema = (configFields, config, options = {}) => {
|
||||
const fields = { ...baseFields };
|
||||
const flexibleFields = [];
|
||||
const flexiblefields = [];
|
||||
|
||||
configFields.forEach((field) => {
|
||||
const fieldSchema = fieldToSchemaMap[field.type];
|
||||
if (fieldSchema) fields[field.name] = fieldSchema(field, { localization: config.localization });
|
||||
if (field.type === 'flexible') flexibleFields.push(field);
|
||||
if (field.type === 'flexible') {
|
||||
flexiblefields.push(field);
|
||||
}
|
||||
|
||||
if (fieldSchema) {
|
||||
fields[field.name] = fieldSchema(field, { localization: config.localization });
|
||||
}
|
||||
});
|
||||
|
||||
const schema = new Schema(fields, options)
|
||||
.plugin(localizationPlugin, config.localization)
|
||||
.plugin(mongooseHidden());
|
||||
const schema = new Schema(fields, options);
|
||||
|
||||
flexibleFields.forEach((field) => {
|
||||
flexiblefields.forEach((field) => {
|
||||
field.blocks.forEach((block) => {
|
||||
const subSchema = buildSchema(block.fields, config, { _id: false });
|
||||
const blockSchemaFields = {};
|
||||
|
||||
if (field.localized && config.localization && config.localization.locales) {
|
||||
config.localization.locales.forEach((locale) => {
|
||||
schema.path(`${field.name}.${locale}`).discriminator(block.labels.singular, subSchema);
|
||||
});
|
||||
} else {
|
||||
schema.path(field.name).discriminator(block.labels.singular, subSchema);
|
||||
}
|
||||
block.fields.forEach((blockField) => {
|
||||
const fieldSchema = fieldToSchemaMap[blockField.type];
|
||||
if (fieldSchema) blockSchemaFields[blockField.name] = fieldSchema(blockField, { localization: config.localization });
|
||||
});
|
||||
|
||||
const blockSchema = new Schema(blockSchemaFields, { _id: false });
|
||||
schema.path(field.name).discriminator(block.labels.singular, blockSchema);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Schema } from 'mongoose';
|
||||
import config from '../../auth/passwordResets/config';
|
||||
|
||||
const formatBaseSchema = (field) => {
|
||||
return {
|
||||
@@ -61,10 +62,10 @@ const fieldToSchemaMap = {
|
||||
};
|
||||
},
|
||||
flexible: (field) => {
|
||||
const blockSchema = new Schema({ blockType: String }, { discriminatorKey: 'blockType', _id: false });
|
||||
const flexibleSchema = new Schema({ name: String }, { discriminatorKey: 'blockType', _id: false });
|
||||
|
||||
return {
|
||||
type: [blockSchema],
|
||||
type: [flexibleSchema],
|
||||
localized: field.localized || false,
|
||||
};
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user