2019-07-01 16:59:14 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const pluralize = require('pluralize');
|
|
|
|
|
2019-10-24 14:07:41 +02:00
|
|
|
const { getComponentAttributes } = require('./utils/attributes');
|
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
const createComponentModels = async ({ model, definition, ORM, GLOBALS }) => {
|
2019-07-01 16:59:14 +02:00
|
|
|
const { collectionName, primaryKey } = definition;
|
|
|
|
|
2019-10-28 12:25:06 +01:00
|
|
|
const componentAttributes = getComponentAttributes(definition);
|
2019-07-01 16:59:14 +02:00
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
if (componentAttributes.length > 0) {
|
|
|
|
// create component model
|
|
|
|
const joinTable = `${collectionName}_components`;
|
2019-07-01 16:59:14 +02:00
|
|
|
const joinColumn = `${pluralize.singular(collectionName)}_${primaryKey}`;
|
2019-12-12 15:45:47 +01:00
|
|
|
|
|
|
|
const relatedComponents = componentAttributes
|
|
|
|
.map(key => {
|
|
|
|
const attr = definition.attributes[key];
|
|
|
|
const { type } = attr;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'component': {
|
|
|
|
const { component } = attr;
|
|
|
|
return strapi.components[component];
|
|
|
|
}
|
|
|
|
case 'dynamiczone': {
|
|
|
|
const { components } = attr;
|
|
|
|
return components.map(component => strapi.components[component]);
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error(`Invalid type for attribute ${key}: ${type}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.reduce((acc, arr) => acc.concat(arr), []);
|
|
|
|
|
2019-07-01 16:59:14 +02:00
|
|
|
const joinModel = ORM.Model.extend({
|
2019-10-25 10:12:35 +02:00
|
|
|
requireFetch: false,
|
2019-07-08 18:35:39 +02:00
|
|
|
tableName: joinTable,
|
2019-10-22 18:01:03 +02:00
|
|
|
component() {
|
2019-07-01 16:59:14 +02:00
|
|
|
return this.morphTo(
|
2019-10-22 18:01:03 +02:00
|
|
|
'component',
|
2019-12-12 15:45:47 +01:00
|
|
|
...relatedComponents.map(component => {
|
|
|
|
return GLOBALS[component.globalId];
|
|
|
|
})
|
2019-07-01 16:59:14 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-07-09 18:20:36 +02:00
|
|
|
joinModel.foreignKey = joinColumn;
|
2019-10-22 18:01:03 +02:00
|
|
|
definition.componentsJoinModel = joinModel;
|
2019-07-09 18:20:36 +02:00
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
componentAttributes.forEach(name => {
|
2019-07-09 18:20:36 +02:00
|
|
|
model[name] = function relation() {
|
2019-07-01 16:59:14 +02:00
|
|
|
return this.hasMany(joinModel).query(qb => {
|
2019-12-12 15:45:47 +01:00
|
|
|
qb.where('field', name)
|
|
|
|
.whereIn(
|
|
|
|
'component_type',
|
|
|
|
relatedComponents.map(component => component.collectionName)
|
|
|
|
)
|
|
|
|
.orderBy('order');
|
2019-07-01 16:59:14 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
2019-07-16 18:05:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
const createComponentJoinTables = async ({ definition, ORM }) => {
|
2019-07-16 18:05:24 +02:00
|
|
|
const { collectionName, primaryKey } = definition;
|
|
|
|
|
2019-10-28 12:25:06 +01:00
|
|
|
const componentAttributes = getComponentAttributes(definition);
|
2019-07-16 18:05:24 +02:00
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
if (componentAttributes.length > 0) {
|
|
|
|
const joinTable = `${collectionName}_components`;
|
2019-07-16 18:05:24 +02:00
|
|
|
const joinColumn = `${pluralize.singular(collectionName)}_${primaryKey}`;
|
2019-07-01 16:59:14 +02:00
|
|
|
|
2019-07-16 15:31:15 +02:00
|
|
|
if (await ORM.knex.schema.hasTable(joinTable)) return;
|
|
|
|
|
|
|
|
await ORM.knex.schema.createTable(joinTable, table => {
|
2019-07-01 16:59:14 +02:00
|
|
|
table.increments();
|
|
|
|
table.string('field').notNullable();
|
|
|
|
table
|
|
|
|
.integer('order')
|
|
|
|
.unsigned()
|
|
|
|
.notNullable();
|
2019-10-22 18:01:03 +02:00
|
|
|
table.string('component_type').notNullable();
|
|
|
|
table.integer('component_id').notNullable();
|
2019-12-05 12:29:24 +01:00
|
|
|
table
|
|
|
|
.integer(joinColumn)
|
|
|
|
.unsigned()
|
|
|
|
.notNullable();
|
2019-07-01 16:59:14 +02:00
|
|
|
|
|
|
|
table
|
2019-12-13 11:50:14 +01:00
|
|
|
.foreign(joinColumn, `${joinColumn}_fk`)
|
2019-07-01 16:59:14 +02:00
|
|
|
.references(primaryKey)
|
|
|
|
.inTable(collectionName)
|
|
|
|
.onDelete('CASCADE');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2019-07-16 18:05:24 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2019-10-22 18:01:03 +02:00
|
|
|
createComponentModels,
|
|
|
|
createComponentJoinTables,
|
2019-07-16 18:05:24 +02:00
|
|
|
};
|