mirror of
https://github.com/strapi/strapi.git
synced 2025-07-23 09:00:19 +00:00
62 lines
1.6 KiB
JavaScript
62 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const pluralize = require('pluralize');
|
|
|
|
module.exports = async ({ model, definition, ORM, GLOBALS }) => {
|
|
const { collectionName, primaryKey } = definition;
|
|
|
|
const groupAttributes = Object.keys(definition.attributes).filter(
|
|
key => definition.attributes[key].type === 'group'
|
|
);
|
|
|
|
if (groupAttributes.length > 0) {
|
|
// create group model
|
|
const joinTable = `${collectionName}_groups`;
|
|
const joinColumn = `${pluralize.singular(collectionName)}_${primaryKey}`;
|
|
const joinModel = ORM.Model.extend({
|
|
tableName: joinTable,
|
|
slice() {
|
|
return this.morphTo(
|
|
'slice',
|
|
...groupAttributes.map(key => {
|
|
const groupKey = definition.attributes[key].group;
|
|
return GLOBALS[strapi.groups[groupKey].globalId];
|
|
})
|
|
);
|
|
},
|
|
});
|
|
|
|
groupAttributes.forEach(name => {
|
|
model[name] = function() {
|
|
return this.hasMany(joinModel).query(qb => {
|
|
qb.where('field', name).orderBy('order');
|
|
});
|
|
};
|
|
});
|
|
|
|
await ORM.knex.schema.createTableIfNotExists(joinTable, table => {
|
|
table.increments();
|
|
table.string('field').notNullable();
|
|
table
|
|
.integer('order')
|
|
.unsigned()
|
|
.notNullable();
|
|
table.string('slice_type').notNullable();
|
|
table
|
|
.integer('slice_id')
|
|
.unsigned()
|
|
.notNullable();
|
|
table
|
|
.integer(joinColumn)
|
|
.unsigned()
|
|
.notNullable();
|
|
|
|
table
|
|
.foreign(joinColumn)
|
|
.references(primaryKey)
|
|
.inTable(collectionName)
|
|
.onDelete('CASCADE');
|
|
});
|
|
}
|
|
};
|