Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /var/www/html/wincloud_gateway/node_modules/strapi-connector-bookshelf/lib/ drwxr-xr-x | |
| Viewing file: Select action/file-type: 'use strict';
const pluralize = require('pluralize');
const { getComponentAttributes } = require('./utils/attributes');
const createComponentModels = async ({ model, definition, ORM, GLOBALS }) => {
const { collectionName, primaryKey } = definition;
const componentAttributes = getComponentAttributes(definition);
if (componentAttributes.length > 0) {
// create component model
const joinTable = `${collectionName}_components`;
const joinColumn = `${pluralize.singular(collectionName)}_${primaryKey}`;
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), []);
const joinModel = ORM.Model.extend({
requireFetch: false,
tableName: joinTable,
component() {
return this.morphTo(
'component',
...relatedComponents.map(component => GLOBALS[component.globalId])
);
},
});
joinModel.foreignKey = joinColumn;
definition.componentsJoinModel = joinModel;
componentAttributes.forEach(name => {
model[name] = function relation() {
return this.hasMany(joinModel, joinColumn).query(qb => {
qb.where('field', name)
.whereIn(
'component_type',
relatedComponents.map(component => component.collectionName)
)
.orderBy('order');
});
};
});
}
};
const createComponentJoinTables = async ({ definition, ORM }) => {
const { collectionName, primaryKey } = definition;
const componentAttributes = getComponentAttributes(definition);
if (componentAttributes.length > 0) {
const joinTable = `${collectionName}_components`;
const joinColumn = `${pluralize.singular(collectionName)}_${primaryKey}`;
if (await ORM.knex.schema.hasTable(joinTable)) return;
await ORM.knex.schema.createTable(joinTable, table => {
table.increments();
table.string('field').notNullable();
table
.integer('order')
.unsigned()
.notNullable();
table.string('component_type').notNullable();
table.integer('component_id').notNullable();
table
.integer(joinColumn)
.unsigned()
.notNullable();
table
.foreign(joinColumn, `${joinColumn}_fk`)
.references(primaryKey)
.inTable(collectionName)
.onDelete('CASCADE');
});
}
};
module.exports = {
createComponentModels,
createComponentJoinTables,
};
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.1781 ]-- |