work in progress on flexible intl fields
This commit is contained in:
@@ -19,18 +19,23 @@ export default function localizationPlugin(schema, options) {
|
||||
}
|
||||
|
||||
schema.eachPath((path, schemaType) => {
|
||||
|
||||
// TODO: We need a way to set if this should recurse or not. What is happening on global's flexible field is that it tries to save flexibleGlobal.en.en on post.
|
||||
let recurse = true;
|
||||
if (path.endsWith('global.en')) {
|
||||
recurse = false;
|
||||
}
|
||||
if (schemaType.schema) { // propagate plugin initialization for sub-documents schemas
|
||||
schemaType.schema.plugin(localizationPlugin, pluginOptions);
|
||||
if (!recurse)
|
||||
return;
|
||||
}
|
||||
|
||||
if (!schemaType.options.localized && !(schemaType.schema && schemaType.schema.options.localized)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!schemaType.options.localized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(schemaType instanceof mongoose.Schema.Types.String)) {
|
||||
throw new mongoose.Error('localization can only be used on type String');
|
||||
if (!((schemaType instanceof mongoose.Schema.Types.String) || (schemaType instanceof mongoose.Schema.Types.Embedded))) {
|
||||
throw new mongoose.Error('localization can only be used on type String or Embedded');
|
||||
}
|
||||
|
||||
let pathArray = path.split('.'),
|
||||
@@ -89,11 +94,11 @@ export default function localizationPlugin(schema, options) {
|
||||
let locales = this.schema.options.localization.locales;
|
||||
locales.forEach(locale => {
|
||||
if (!value[locale]) {
|
||||
// this.set(`${path}.${locale}`, value);
|
||||
return;
|
||||
}
|
||||
this.set(path + '.' + locale, value[locale]);
|
||||
this.set(`${path}.${locale}`, value[locale]);
|
||||
}, this);
|
||||
return;
|
||||
}
|
||||
|
||||
// embedded and sub-documents will use locale methods from the top level document
|
||||
@@ -107,6 +112,8 @@ export default function localizationPlugin(schema, options) {
|
||||
// delete schemaType.options.localized; // This was removed to allow viewing inside query parser
|
||||
|
||||
let 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) {
|
||||
let localeOptions = Object.assign({}, schemaType.options);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import mongoose from 'mongoose';
|
||||
import localizationPlugin from '../../localization/localization.plugin';
|
||||
import autopopulate from '../autopopulate.plugin';
|
||||
|
||||
const formatBaseSchema = field => {
|
||||
return {
|
||||
@@ -11,13 +13,13 @@ const formatBaseSchema = field => {
|
||||
|
||||
const fieldToSchemaMap = {
|
||||
number: field => {
|
||||
return { ...formatBaseSchema(field), type: Number };
|
||||
return {...formatBaseSchema(field), type: Number};
|
||||
},
|
||||
input: field => {
|
||||
return { ...formatBaseSchema(field), type: String };
|
||||
return {...formatBaseSchema(field), type: String};
|
||||
},
|
||||
textarea: field => {
|
||||
return { ...formatBaseSchema(field), type: String };
|
||||
return {...formatBaseSchema(field), type: String};
|
||||
},
|
||||
date: field => {
|
||||
return {
|
||||
@@ -50,17 +52,33 @@ const fieldToSchemaMap = {
|
||||
enum: field.enum,
|
||||
};
|
||||
},
|
||||
flexible: (field, path) => {
|
||||
const schema = {
|
||||
flexible: (field, options = {}) => {
|
||||
const flexible = {
|
||||
value: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
autopopulate: true,
|
||||
refPath: `${path ? (path + '.') : ''}${field.name}.blockType`,
|
||||
refPath: `${options.path ? (options.path + '.') : ''}${field.name}.blockType`,
|
||||
},
|
||||
blockType: { type: String, enum: field.blocks },
|
||||
blockType: {type: String, enum: field.blocks},
|
||||
_id: false,
|
||||
};
|
||||
return field.hasMany !== false ? [schema] : schema;
|
||||
|
||||
const schema = new mongoose.Schema(
|
||||
field.hasMany !== false ? [flexible] : flexible,
|
||||
{
|
||||
hasMany: field.hasMany,
|
||||
localized: field.localized || false,
|
||||
}
|
||||
);
|
||||
if (field.name === 'flexibleGlobal') {
|
||||
console.log(field.hasMany !== false ? [flexible] : flexible);
|
||||
}
|
||||
if (field.localized) {
|
||||
schema.plugin(localizationPlugin, options.localization);
|
||||
}
|
||||
schema.plugin(autopopulate);
|
||||
|
||||
return schema;
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ class SchemaLoader {
|
||||
|
||||
const Schema = new mongoose.Schema(fields)
|
||||
.plugin(paginate)
|
||||
.plugin(localizationPlugin, config.localization)
|
||||
.plugin(buildQueryPlugin)
|
||||
.plugin(autopopulate);
|
||||
|
||||
@@ -96,11 +97,11 @@ class SchemaLoader {
|
||||
|
||||
collectionConfig.fields.forEach(field => {
|
||||
const fieldSchema = fieldToSchemaMap[field.type];
|
||||
if (fieldSchema) fields[field.name] = fieldSchema(field);
|
||||
if (fieldSchema) fields[field.name] = fieldSchema(field, {localization: config.localization});
|
||||
});
|
||||
|
||||
const Schema = new mongoose.Schema(fields, { timestamps: collectionConfig.timestamps });
|
||||
Schema.plugin(paginate)
|
||||
const Schema = new mongoose.Schema(fields, { timestamps: collectionConfig.timestamps })
|
||||
.plugin(paginate)
|
||||
.plugin(buildQueryPlugin)
|
||||
.plugin(localizationPlugin, config.localization)
|
||||
.plugin(autopopulate);
|
||||
@@ -128,7 +129,7 @@ class SchemaLoader {
|
||||
|
||||
globalConfig.fields.forEach(field => {
|
||||
const fieldSchema = fieldToSchemaMap[field.type];
|
||||
if (fieldSchema) globalFields[globalConfig.slug][field.name] = fieldSchema(field, globalConfig.slug);
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user