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.
|
2016-03-22 15:50:33 +01:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2016-03-23 16:53:42 +01:00
|
|
|
module.exports = function (models, modelName, details, attribute, toDrop, onlyDrop) {
|
2016-03-18 11:12:50 +01:00
|
|
|
let tplRelationUp;
|
|
|
|
let tplRelationDown;
|
|
|
|
|
2016-03-22 15:50:33 +01:00
|
|
|
const infos = utilsModels.getNature(details, attribute, models);
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-03-23 18:11:10 +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') {
|
2016-03-23 18:11:10 +01:00
|
|
|
// Force singular foreign key.
|
2016-03-18 11:12:50 +01:00
|
|
|
details.attribute = pluralize.singular(details.model);
|
|
|
|
|
2016-03-23 18:11:10 +01:00
|
|
|
// Define PK column.
|
2016-03-22 15:50:33 +01:00
|
|
|
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');
|
2016-03-23 18:11:10 +01:00
|
|
|
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.template'), 'utf8');
|
2016-03-23 18:11:10 +01:00
|
|
|
models[modelName].attributes[attribute].delete.others = _.unescape(_.template(tplRelationDown)({
|
2016-03-18 11:12:50 +01:00
|
|
|
tableName: modelName,
|
|
|
|
attribute: attribute,
|
|
|
|
details: details
|
|
|
|
}));
|
2016-03-23 18:11:10 +01:00
|
|
|
} else if (infos.verbose === 'belongsTo') {
|
|
|
|
// Force singular foreign key.
|
2016-03-18 11:12:50 +01:00
|
|
|
details.attribute = pluralize.singular(details.model);
|
|
|
|
|
2016-03-23 18:11:10 +01:00
|
|
|
// Define PK column.
|
2016-03-22 15:50:33 +01:00
|
|
|
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');
|
2016-03-23 18:11:10 +01:00
|
|
|
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
|
|
|
|
}));
|
|
|
|
} 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');
|
2016-03-23 18:11:10 +01:00
|
|
|
models[modelName].attributes[attribute].create.others = _.unescape(_.template(tplRelationUp)({
|
2016-03-18 11:12:50 +01:00
|
|
|
tableName: modelName,
|
|
|
|
attribute: attribute,
|
|
|
|
details: details
|
|
|
|
}));
|
|
|
|
}
|
2016-04-07 15:43:20 +02:00
|
|
|
|
|
|
|
// 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-23 18:11:10 +01:00
|
|
|
} 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];
|
|
|
|
|
2016-03-23 18:11:10 +01:00
|
|
|
// 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('__');
|
|
|
|
|
2016-03-23 18:11:10 +01:00
|
|
|
// Force singular foreign key.
|
2016-03-18 11:12:50 +01:00
|
|
|
relationship.attribute = pluralize.singular(relationship.collection);
|
|
|
|
details.attribute = pluralize.singular(details.collection);
|
|
|
|
|
2016-03-23 18:11:10 +01:00
|
|
|
// Define PK column.
|
2016-03-22 15:50:33 +01:00
|
|
|
details.column = utilsBookShelf.getPK(modelName, undefined, models);
|
|
|
|
relationship.column = utilsBookShelf.getPK(details.collection, undefined, models);
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-03-23 18:11:10 +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`.
|
|
|
|
models[relationTable] = {};
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
models[relationTable].up = _.unescape(_.template(tplTableUp)({
|
|
|
|
models: models,
|
|
|
|
tableName: relationTable,
|
|
|
|
details: details,
|
|
|
|
relationship: relationship
|
|
|
|
}));
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
models[relationTable].down = _.unescape(_.template(tplTableDown)({
|
|
|
|
tableName: relationTable
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|