move function checkDuplicatedTableNames in its own file

Signed-off-by: Pierre Noël <pierre.noel@strapi.io>
This commit is contained in:
Pierre Noël 2020-03-30 14:39:31 +02:00
parent 053714c7be
commit ab79327577
4 changed files with 67 additions and 55 deletions

View File

@ -670,7 +670,8 @@ module.exports = ({ models, target }, ctx) => {
if (['ER_TOO_LONG_IDENT'].includes(err.code)) {
strapi.stopWithError(
err,
'A table name is too long. If it is the name of a join table automatically generated by Strapi, you can customise it by adding `collectionName: "customName"` in the corresponding attribute model (on the dominant side if it\'s a many-to-many relation).'
`A table name is too long. If it is the name of a join table automatically generated by Strapi, you can customise it by adding \`collectionName: "customName"\` in the corresponding model's attribute.
When this happens on a manyToMany relation, make sure to set this parameter on the dominant side of the relation (e.g: where \`dominant: true\` is set)`
);
}
strapi.stopWithError(err);

View File

@ -4,6 +4,7 @@ const _ = require('lodash');
const requireConnector = require('./require-connector');
const { createQuery } = require('./queries');
const { checkDuplicatedTableNames } = require('./validation/before-mounting-models');
class DatabaseManager {
constructor(strapi) {
@ -16,59 +17,6 @@ class DatabaseManager {
this.models = new Map();
}
// Check if all collection names are unique
checkForDuplicates() {
const createErrorMessage = (
collectionA,
collectionB
) => `Duplicated collection name: \`${collectionA.collectionName}\`.
The same collection name can't be used for two different models.
First found in ${collectionA.origin} \`${collectionA.apiOrPluginName}\`, model \`${collectionA.modelName}\`.
Second found in ${collectionB.origin} \`${collectionB.apiOrPluginName}\`, model \`${collectionB.modelName}\`.`;
const collections = [];
_.forIn(this.strapi.admin.models, (model, modelName) => {
collections.push({
origin: 'Strapi internal',
collectionName: model.collectionName || `${modelName}`.toLocaleLowerCase(),
apiOrPluginName: 'admin',
modelName,
});
});
_.forIn(this.strapi.api, (api, apiName) => {
_.forIn(api.models, (model, modelName) => {
collections.push({
origin: 'API',
collectionName: model.collectionName || `${modelName}`.toLocaleLowerCase(),
apiOrPluginName: apiName,
modelName,
});
});
});
_.forIn(this.strapi.plugins, (plugin, pluginName) => {
_.forIn(plugin.models, (model, modelName) => {
collections.push({
origin: 'Plugin',
collectionName: model.collectionName || `${modelName}`.toLocaleLowerCase(),
apiOrPluginName: pluginName,
modelName,
});
});
});
collections.forEach((collectionA, indexA) => {
const similarCollectionFound = collections
.slice(indexA + 1)
.find(colB => colB.collectionName === collectionA.collectionName);
if (similarCollectionFound) {
strapi.stopWithError(new Error(createErrorMessage(collectionA, similarCollectionFound)));
}
});
}
async initialize() {
if (this.initialized === true) {
throw new Error('Database manager already initialized');
@ -84,7 +32,7 @@ Second found in ${collectionB.origin} \`${collectionB.apiOrPluginName}\`, model
}
}
this.checkForDuplicates();
checkDuplicatedTableNames(this.strapi);
for (const connectorToInitialize of connectorsToInitialize) {
const connector = requireConnector(connectorToInitialize)(strapi);

View File

@ -0,0 +1,58 @@
const _ = require('lodash');
const createErrorMessage = (
modelA,
modelB
) => `Duplicated collection name: \`${modelA.model.collectionName}\`.
The same collection name can't be used for two different models.
First found in ${modelA.origin} \`${modelA.apiOrPluginName}\`, model \`${modelA.modelName}\`.
Second found in ${modelB.origin} \`${modelB.apiOrPluginName}\`, model \`${modelB.modelName}\`.`;
// Check if all collection names are unique
const checkDuplicatedTableNames = strapi => {
const modelsWithInfo = [];
_.forIn(strapi.admin.models, (model, modelName) => {
modelsWithInfo.push({
origin: 'Strapi internal',
model,
apiOrPluginName: 'admin',
modelName,
});
});
_.forIn(strapi.api, (api, apiName) => {
_.forIn(api.models, (model, modelName) => {
modelsWithInfo.push({
origin: 'API',
model,
apiOrPluginName: apiName,
modelName,
});
});
});
_.forIn(strapi.plugins, (plugin, pluginName) => {
_.forIn(plugin.models, (model, modelName) => {
modelsWithInfo.push({
origin: 'Plugin',
model,
apiOrPluginName: pluginName,
modelName,
});
});
});
modelsWithInfo.forEach(modelA => {
const similarModelFound = modelsWithInfo.find(
modelB =>
modelB.model.collectionName === modelA.model.collectionName &&
modelB.model.uid !== modelA.model.uid
);
if (similarModelFound) {
strapi.stopWithError(new Error(createErrorMessage(modelA, similarModelFound)));
}
});
};
module.exports = checkDuplicatedTableNames;

View File

@ -0,0 +1,5 @@
const checkDuplicatedTableNames = require('./check-duplicated-table-names');
module.exports = {
checkDuplicatedTableNames,
};