Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const buildRelation = ({ definition, model, instance, attribute, name }) => {
const { nature, verbose } =
utilsModels.getNature(attribute, name, undefined, model.toLowerCase()) ||
{};
// Build associations key
utilsModels.defineAssociations(
model.toLowerCase(),
definition,
attribute,
name
);
switch (verbose) {
case 'hasOne': {
const ref = attribute.plugin
? strapi.plugins[attribute.plugin].models[attribute.model].globalId
: strapi.models[attribute.model].globalId;
_.forEach(definition.attributes, (details, name) => {
const verbose = _.get(utilsModels.getNature(details, name, undefined, model.toLowerCase()), 'verbose') || '';
// Build associations key
utilsModels.defineAssociations(model.toLowerCase(), definition, details, name);
if (_.isEmpty(verbose)) {
definition.loadedModel[name].type = utils(instance).convertType(details.type);
}
switch (verbose) {
case 'hasOne': {
const ref = details.plugin ? strapi.plugins[details.plugin].models[details.model].globalId : strapi.models[details.model].globalId;
definition.loadedModel[name] = {
type: instance.Schema.Types.ObjectId,
ref
};
_.forEach(definition.attributes, (details, name) => {
const verbose = _.get(
utilsModels.getNature(details, name, undefined, model.toLowerCase()),
'verbose'
) || '';
// Build associations key
utilsModels.defineAssociations(
model.toLowerCase(),
definition,
details,
name
);
let globalId;
const globalName = details.model || details.collection || '';
// Exclude polymorphic association.
if (globalName !== '*') {
_.forEach(definition.attributes, (details, name) => {
const verbose = _.get(utilsModels.getNature(details, name, undefined, model.toLowerCase()), 'verbose') || '';
// Build associations key
utilsModels.defineAssociations(model.toLowerCase(), definition, details, name);
if (_.isEmpty(verbose)) {
definition.loadedModel[name].type = utils(instance).convertType(details.type);
}
switch (verbose) {
case 'hasOne': {
const ref = details.plugin ? strapi.plugins[details.plugin].models[details.model].globalId : strapi.models[details.model].globalId;
definition.loadedModel[name] = {
type: instance.Schema.Types.ObjectId,
ref
};
Object.keys(definition.attributes).forEach(name => {
const details = definition.attributes[name];
if (details.type !== undefined) {
return;
}
const { nature, verbose } =
utilsModels.getNature(details, name, undefined, model.toLowerCase()) ||
{};
// Build associations key
utilsModels.defineAssociations(
model.toLowerCase(),
definition,
details,
name
);
let globalId;
const globalName = details.model || details.collection || '';
// Exclude polymorphic association.
if (globalName !== '*') {
globalId = details.plugin
module.exports = (rootModels, modelName, details, attribute, toDrop, onlyDrop, history) => {
let tplRelationUp;
let tplRelationDown;
let infos = {};
let oldInfos = {};
if (!onlyDrop && toDrop) {
infos = utilsModels.getNature(details, attribute, rootModels);
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);
if (isDifferentVerbose) {
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
handleRelation(infos, rootModels, modelName, details, attribute);
} else {
handleRelation(infos, rootModels, modelName, details, attribute, true, true);
}
} else if (onlyDrop || toDrop) {
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
} else {
infos = utilsModels.getNature(details, attribute, rootModels);
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);
if (isDifferentVerbose) {
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
handleRelation(infos, rootModels, modelName, details, attribute);
} else {
handleRelation(infos, rootModels, modelName, details, attribute, true, true);
}
} else if (onlyDrop || toDrop) {
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
} else {
infos = utilsModels.getNature(details, attribute, rootModels);
handleRelation(infos, rootModels, modelName, details, attribute);
}
function handleRelation(infos, models, modelName, details, attribute, toDrop, onlyDrop) {
if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.create'))) {
_.set(rootModels[modelName].attributes, attribute + '.create', {
drop: '',
others: ''
});
}
if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.delete'))) {
_.set(rootModels[modelName].attributes, attribute + '.delete', {
drop: '',
others: ''
let oldInfos = {};
if (!onlyDrop && toDrop) {
infos = utilsModels.getNature(details, attribute, rootModels);
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);
if (isDifferentVerbose) {
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
handleRelation(infos, rootModels, modelName, details, attribute);
} else {
handleRelation(infos, rootModels, modelName, details, attribute, true, true);
}
} else if (onlyDrop || toDrop) {
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
} else {
infos = utilsModels.getNature(details, attribute, rootModels);
handleRelation(infos, rootModels, modelName, details, attribute);
}
function handleRelation(infos, models, modelName, details, attribute, toDrop, onlyDrop) {
if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.create'))) {
_.set(rootModels[modelName].attributes, attribute + '.create', {
drop: '',
others: ''
});
}
module.exports = (rootModels, modelName, details, attribute, toDrop, onlyDrop, history) => {
let tplRelationUp;
let tplRelationDown;
let infos = {};
let oldInfos = {};
if (!onlyDrop && toDrop) {
infos = utilsModels.getNature(details, attribute, rootModels);
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
const isDifferentVerbose = !(oldInfos.hasOwnProperty('nature') && oldInfos.nature === infos.nature);
if (isDifferentVerbose) {
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
handleRelation(infos, rootModels, modelName, details, attribute);
} else {
handleRelation(infos, rootModels, modelName, details, attribute, true, true);
}
} else if (onlyDrop || toDrop) {
oldInfos = utilsModels.getNature(_.get(rootModels[modelName].oldAttributes, attribute), attribute, history);
handleRelation(oldInfos, history, modelName, _.get(rootModels[modelName].oldAttributes, attribute), attribute, true, true);
} else {
infos = utilsModels.getNature(details, attribute, rootModels);