177 lines
7.5 KiB
JavaScript
Raw Normal View History

2016-03-18 11:12:50 +01:00
'use strict';
/**
* Module dependencies
*/
// Node.js core.
const fs = require('fs');
const path = require('path');
// Public node modules.
const _ = require('lodash');
const pluralize = require('pluralize');
2016-03-23 15:18:37 +01:00
// Collections utils.
const utilsModels = require('strapi/lib/configuration/hooks/models/utils/');
2016-03-18 11:12:50 +01:00
const utilsBookShelf = require('strapi-bookshelf/lib/utils/');
/**
* Relationship templates
*/
module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop) {
2016-03-18 11:12:50 +01:00
let tplRelationUp;
let tplRelationDown;
const infos = utilsModels.getNature(details, attribute, models);
2016-03-18 11:12:50 +01:00
_.set(models[modelName].attributes, attribute + '.create', {});
_.set(models[modelName].attributes, attribute + '.delete', {});
2016-03-18 11:12:50 +01:00
// If it's a "one-to-one" relationship.
if (infos.verbose === 'hasOne') {
// Force singular foreign key.
2016-03-18 11:12:50 +01:00
details.attribute = pluralize.singular(details.model);
// Define PK column.
details.column = utilsBookShelf.getPK(modelName, undefined, models);
2016-03-18 11:12:50 +01:00
// Template: create a new column thanks to the attribute's relation.
// Simply make a `create` template for this attribute wich will be added
// to the table template-- either `./builder/tables/selectTable` or
// `./builder/tables/createTableIfNotExists`.
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'hasOne.template'), 'utf8');
models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplRelationUp)({
2016-03-18 11:12:50 +01:00
tableName: modelName,
attribute: attribute,
details: details
}));
// Template: drop the column.
// Simply make a `delete` template for this attribute wich drop the column
// with the `./builder/columns/dropColumn` template.
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn-unique.template'), 'utf8');
models[modelName].attributes[attribute].delete.others = _.unescape(_.template(tplRelationDown)({
2016-03-18 11:12:50 +01:00
tableName: modelName,
attribute: attribute,
details: details
}));
} else if (infos.verbose === 'belongsTo') {
// Force singular foreign key.
2016-03-18 11:12:50 +01:00
details.attribute = pluralize.singular(details.model);
// Define PK column.
details.column = utilsBookShelf.getPK(modelName, undefined, models);
2016-03-18 11:12:50 +01:00
if (infos.nature === 'oneToMany' || infos.nature === 'oneWay') {
// Template: create a new column thanks to the attribute's relation.
// Simply make a `create` template for this attribute wich will be added
// to the table template-- either `./builder/tables/selectTable` or
// `./builder/tables/createTableIfNotExists`.
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo.template'), 'utf8');
models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplRelationUp)({
2016-03-18 11:12:50 +01:00
tableName: modelName,
attribute: attribute,
details: details,
nature: infos.nature
}));
// Template: drop the column.
// Simply make a `delete` template for this attribute wich drop the column
// with the `./builder/columns/dropColumn` template.
tplRelationDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropColumn.template'), 'utf8');
models[modelName].attributes[attribute].delete.others = _.unescape(_.template(tplRelationDown)({
tableName: modelName,
attribute: attribute,
details: details
}));
2016-03-18 11:12:50 +01:00
} else {
// Template: create a new column thanks to the attribute's relation.
// Simply make a `create` template for this attribute wich will be added
// to the table template-- either `./builder/tables/selectTable` or
// `./builder/tables/createTableIfNotExists`.
tplRelationUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsTo-unique.template'), 'utf8');
models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplRelationUp)({
2016-03-18 11:12:50 +01:00
tableName: modelName,
attribute: attribute,
details: details
}));
// Template: drop the column.
// Simply make a `delete` template for this attribute wich drop the column
// with the `./builder/columns/dropColumn` template.
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: attribute,
details: details
}));
}
} else if (infos.verbose === 'belongsToMany') {
// Otherwise if it's a "many-to-many" relationship.
2016-03-18 11:12:50 +01:00
// Save the relationship.
const relationship = models[details.collection].attributes[details.via];
// Construct relation table name.
2016-03-18 11:12:50 +01:00
const relationTable = _.map(_.sortBy([relationship, details], 'collection'), function (table) {
return _.snakeCase(pluralize.plural(table.collection) + ' ' + pluralize.plural(table.via));
}).join('__');
// Force singular foreign key.
2016-03-18 11:12:50 +01:00
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);
2016-03-18 11:12:50 +01:00
// Avoid to create table both times.
2016-03-18 11:12:50 +01:00
if (!models.hasOwnProperty(relationTable)) {
// Save the relation table as a new model in the scope
// aiming to benefit of templates for the table such as
// `createTableIfNotExists` and `dropTable`.
// Template: create the table for the `up` export if it doesn't exist.
// This adds a `up` logic for the relation table.
const tplTableUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsToMany.template'), 'utf8');
_.set(models, relationTable + '.up.others', _.unescape(_.template(tplTableUp)({
2016-03-18 11:12:50 +01:00
models: models,
tableName: relationTable,
details: details,
relationship: relationship
})));
2016-03-18 11:12:50 +01:00
// Template: drop the table for the `down` export.
// This adds a `down` logic for the relation table.
const tplTableDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'tables', 'dropTable.template'), 'utf8');
_.set(models, relationTable + '.down.others', _.unescape(_.template(tplTableDown)({
2016-03-18 11:12:50 +01:00
tableName: relationTable
})));
const tplFKDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropForeign.template'), 'utf8');
const tplSelectTableDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'tables', 'select', 'down.template'), 'utf8');
_.set(models, relationTable + '.attributes', {
fk: {
delete: {
drop: _.unescape(_.template(tplFKDown)({
attribute: details.attribute + '_' + details.column
})) + _.unescape(_.template(tplFKDown)({
attribute: relationship.attribute + '_' + relationship.column
}))
}
}
});
models[relationTable].down.drop = _.unescape(_.template(tplSelectTableDown)({
models: models,
tableName: relationTable,
attributes: models[relationTable].attributes,
toDrop: true
2016-03-18 11:12:50 +01:00
}));
}
}
};