Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Otherwise if it's a "many-to-many" relationship.
// Save the relationship.
const relationship = models[details.collection].attributes[details.via];
// Construct relation table name.
const relationTable = _.map(_.sortBy([relationship, details], 'collection'), table => {
return _.snakeCase(pluralize.plural(table.collection) + ' ' + pluralize.plural(table.via));
}).join('__');
// Force singular foreign key.
relationship.attribute = pluralize.singular(relationship.collection);
details.attribute = pluralize.singular(details.collection);
// Define PK column.
details.column = utilsBookShelf.getPK(modelName, undefined, models);
relationship.column = utilsBookShelf.getPK(details.collection, undefined, models);
// Avoid to create table both times.
if (!rootModels.hasOwnProperty(relationTable) || !_.isEmpty(_.get(rootModels, relationTable + '.up.drop'))) {
// Set objects
if (_.isUndefined(_.get(models, relationTable + '.up.others'))) {
_.set(rootModels, relationTable + '.up.others', '');
}
if (_.isUndefined(_.get(rootModels, relationTable + '.up.drop'))) {
_.set(rootModels, relationTable + '.up.drop', '');
}
if (_.isUndefined(_.get(rootModels, relationTable + '.down.others'))) {
_.set(rootModels, relationTable + '.down.others', '');
}
}
if (_.isEmpty(_.get(rootModels[modelName].attributes, attribute + '.delete'))) {
_.set(rootModels[modelName].attributes, attribute + '.delete', {
drop: '',
others: ''
});
}
// If it's a "one-to-one" relationship.
if (infos.verbose === 'hasOne') {
// Force singular foreign key.
details.attribute = pluralize.singular(details.model);
// Define PK column.
details.column = utilsBookShelf.getPK(modelName, undefined, models);
if (!toDrop) {
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'hasOne.template'), 'utf8');
models[modelName].attributes[attribute].create.others += _.unescape(_.template(tplRelationUp)({
tableName: modelName,
attribute,
details
}));
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn-unique.template'), 'utf8');
models[modelName].attributes[attribute].delete.others += _.unescape(_.template(tplRelationDown)({
tableName: modelName,
attribute,
details
}));
} else {
// Save the relationship.
const relationship = models[details.collection].attributes[details.via];
// Construct relation table name.
const relationTable = _.map(_.sortBy([relationship, details], 'collection'), table => {
return _.snakeCase(pluralize.plural(table.collection) + ' ' + pluralize.plural(table.via));
}).join('__');
// Force singular foreign key.
relationship.attribute = pluralize.singular(relationship.collection);
details.attribute = pluralize.singular(details.collection);
// Define PK column.
details.column = utilsBookShelf.getPK(modelName, undefined, models);
relationship.column = utilsBookShelf.getPK(details.collection, undefined, models);
// Avoid to create table both times.
if (!rootModels.hasOwnProperty(relationTable) || !_.isEmpty(_.get(rootModels, relationTable + '.up.drop'))) {
// Set objects
if (_.isUndefined(_.get(models, relationTable + '.up.others'))) {
_.set(rootModels, relationTable + '.up.others', '');
}
if (_.isUndefined(_.get(rootModels, relationTable + '.up.drop'))) {
_.set(rootModels, relationTable + '.up.drop', '');
}
if (_.isUndefined(_.get(rootModels, relationTable + '.down.others'))) {
_.set(rootModels, relationTable + '.down.others', '');
}