Update models attributes & get models from the database manager

Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
This commit is contained in:
Convly 2020-05-12 17:20:10 +02:00 committed by Alexandre Bodin
parent f10f51f372
commit 8a69d187d0
5 changed files with 16 additions and 21 deletions

View File

@ -7,7 +7,7 @@
"attributes": {
"name": {
"type": "string",
"minLength": 3,
"minLength": 1,
"unique": true,
"configurable": false,
"required": true

View File

@ -8,18 +8,20 @@
"firstname": {
"type": "string",
"unique": false,
"minLength": 1,
"configurable": false,
"required": false
},
"lastname": {
"type": "string",
"unique": false,
"minLength": 1,
"configurable": false,
"required": false
},
"username": {
"type": "string",
"unique": true,
"unique": false,
"configurable": false,
"required": false
},

View File

@ -138,11 +138,7 @@ module.exports = ({ models, target }, ctx) => {
// Exclude polymorphic association.
if (globalName !== '*') {
globalId = details.plugin
? details.plugin === 'admin'
? _.get(strapi.admin, `models.${globalName.toLowerCase()}.globalId`)
: _.get(strapi.plugins, `${details.plugin}.models.${globalName.toLowerCase()}.globalId`)
: _.get(strapi.models, `${globalName.toLowerCase()}.globalId`);
globalId = strapi.db.getModel(globalName.toLowerCase(), details.plugin).globalId;
}
switch (verbose) {

View File

@ -120,6 +120,14 @@ class DatabaseManager {
});
}
getModelsByPluginName(pluginName) {
if (!pluginName) {
return strapi.models;
}
return pluginName === 'admin' ? strapi.admin.models : strapi.plugins[pluginName].models;
}
getReservedNames() {
return {
models: constants.RESERVED_MODEL_NAMES,

View File

@ -38,11 +38,7 @@ module.exports = {
other: '',
};
const models = attribute.plugin
? attribute.plugin === 'admin'
? strapi.admin.models
: strapi.plugins[attribute.plugin].models
: strapi.models;
const models = strapi.db.getModelsByPluginName(attribute.plugin);
const pluginModels = Object.values(strapi.plugins).reduce((acc, plugin) => {
return acc.concat(Object.values(plugin.models));
@ -326,15 +322,8 @@ module.exports = {
});
if (targetName !== '*') {
if (association.plugin) {
const models =
association.plugin === 'admin'
? strapi.admin.models
: strapi.plugins[association.plugin].models || {};
details = _.get(models, [targetName, 'attributes', association.via]);
} else {
details = _.get(strapi.models, [targetName, 'attributes', association.via], {});
}
const models = strapi.db.getModelsByPluginName(association.plugin);
details = _.get(models, [targetName, 'attributes', association.via], {});
}
// Build associations object