Init i18n fields

This commit is contained in:
Alexandre Bodin 2021-01-04 22:01:37 +01:00
parent 2b342ec69d
commit 68f3e2bee2
6 changed files with 126 additions and 3 deletions

View File

@ -34,4 +34,4 @@
"required": false
}
}
}
}

View File

@ -10,6 +10,11 @@
"timestamps": ["created_at", "updated_at"],
"comment": ""
},
"pluginOptions": {
"i18n": {
"enabled": true
}
},
"attributes": {
"name": {
"maxLength": 50,

View File

@ -1,6 +1,7 @@
'use strict';
const { capitalize } = require('lodash/fp');
const _ = require('lodash');
const { capitalize, prop } = require('lodash/fp');
const actions = ['create', 'read', 'update', 'delete'].map(uid => ({
section: 'settings',
@ -14,4 +15,34 @@ const actions = ['create', 'read', 'update', 'delete'].map(uid => ({
module.exports = () => {
const { actionProvider } = strapi.admin.services.permission;
actionProvider.register(actions);
// register custom permissions
// register database mixin to modify model behaviours
// update model lifecycles
// create the localization of the object & link it to the other localizations it has
Object.values(strapi.contentTypes).forEach(contentType => {
if (prop('pluginOptions.i18n.enabled')(contentType) === true) {
console.log('i18N is enabled for ', contentType.modelName);
const model = strapi.getModel(contentType.uid);
_.set(model, 'lifecycles.beforeCreate', async () => {});
_.set(model, 'lifecycles.afterCreate', async () => {});
_.set(model, 'lifecycles.beforeFind', async () => {});
}
});
// wrap content manager routes
strapi.plugin('content-manager').config.routes.forEach(() => {
// add a policy to the route we want to extend
});
// or overwrite controllers
};

View File

@ -0,0 +1,43 @@
'use strict';
const _ = require('lodash');
// add a register function to do some stuff after the loading but before the boot
module.exports = () => {
// need to add some logic to the db layer so we can add fields to the models
Object.values(strapi.models).forEach(model => {
if (_.get(model, 'pluginOptions.i18n.enabled', false) === true) {
// find a way to specify the id to use in the relations or compo relations
// model.relationalId = 'strapi_id';
// model.attributes.compo.relationalId = 'strapi_id';
_.set(model.attributes, 'strapi_id', {
writable: true,
private: false,
configurable: false,
type: 'string',
});
_.set(model.attributes, 'localizations', {
writable: true,
private: false,
configurable: false,
type: 'json',
});
_.set(model.attributes, 'locale', {
writable: true,
private: false,
configurable: false,
type: 'string',
default: 'en-US',
});
}
});
// strapi.database.migrations.push({
// before() {},
// after() {},
// });
};

View File

@ -67,7 +67,12 @@ const getWritableAttributes = (model = {}) => {
};
const getNonVisibleAttributes = model => {
return _.uniq([model.primaryKey, ...getTimestamps(model), ...NON_VISIBLE_ATTRIBUTES]);
return _.uniq([
model.primaryKey,
...NON_VISIBLE_ATTRIBUTES,
...getTimestamps(model),
...getNonWritableAttributes(model),
]);
};
const getVisibleAttributes = model => {

View File

@ -335,6 +335,8 @@ class Strapi {
this.models['core_store'] = coreStoreModel(this.config);
this.models['strapi_webhooks'] = webhookModel(this.config);
await this.runRegisterFunctions();
this.db = createDatabaseManager(this);
await this.db.initialize();
@ -443,6 +445,35 @@ class Strapi {
});
}
async runRegisterFunctions() {
const execRegister = async fn => {
if (!fn) return;
return fn();
};
// plugins bootstrap
const pluginBoostraps = Object.keys(this.plugins).map(plugin => {
return execRegister(_.get(this.plugins[plugin], 'config.functions.register')).catch(err => {
strapi.log.error(`Register function in plugin "${plugin}" failed`);
strapi.log.error(err);
strapi.stop();
});
});
await Promise.all(pluginBoostraps);
// // user bootstrap
// await execBootstrap(_.get(this.config, ['functions', 'bootstrap']));
// // admin bootstrap : should always run after the others
// const adminBootstrap = _.get(this.admin.config, 'functions.bootstrap');
// return execBootstrap(adminBootstrap).catch(err => {
// strapi.log.error(`Bootstrap function in admin failed`);
// strapi.log.error(err);
// strapi.stop();
// });
}
async freeze() {
Object.freeze(this.config);
Object.freeze(this.dir);
@ -463,6 +494,14 @@ class Strapi {
query(entity, plugin) {
return this.db.query(entity, plugin);
}
plugin(name) {
if (!_.has(this.plugins, name)) {
throw new Error(`Undefined plugin ${name}`);
}
return this.plugins[name];
}
}
module.exports = options => {