mirror of
https://github.com/strapi/strapi.git
synced 2025-11-14 01:02:04 +00:00
Load groups
This commit is contained in:
parent
3a1b85de82
commit
0fc01f5a40
@ -28,6 +28,14 @@
|
|||||||
"model": "file",
|
"model": "file",
|
||||||
"via": "related",
|
"via": "related",
|
||||||
"plugin": "upload"
|
"plugin": "upload"
|
||||||
|
},
|
||||||
|
"ingredients": {
|
||||||
|
"type": "group",
|
||||||
|
"groups": ["ingredients"],
|
||||||
|
"required": true,
|
||||||
|
"repeatable": true,
|
||||||
|
"min": 2,
|
||||||
|
"max": 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ type Articles {
|
|||||||
content2: String
|
content2: String
|
||||||
title: String
|
title: String
|
||||||
image: UploadFile
|
image: UploadFile
|
||||||
|
ingredients: String!
|
||||||
posts(sort: String, limit: Int, start: Int, where: JSON): [Post]
|
posts(sort: String, limit: Int, start: Int, where: JSON): [Post]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ input ArticlesInput {
|
|||||||
posts: [ID]
|
posts: [ID]
|
||||||
title: String
|
title: String
|
||||||
image: ID
|
image: ID
|
||||||
|
ingredients: String!
|
||||||
}
|
}
|
||||||
|
|
||||||
input createArticlesInput {
|
input createArticlesInput {
|
||||||
@ -89,6 +91,7 @@ input editArticlesInput {
|
|||||||
posts: [ID]
|
posts: [ID]
|
||||||
title: String
|
title: String
|
||||||
image: ID
|
image: ID
|
||||||
|
ingredients: String
|
||||||
}
|
}
|
||||||
|
|
||||||
input editFileInput {
|
input editFileInput {
|
||||||
|
|||||||
18
examples/getstarted/groups/Ingredients.json
Normal file
18
examples/getstarted/groups/Ingredients.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"collectionName": "group_ingredients",
|
||||||
|
"attributes": {
|
||||||
|
"name": {
|
||||||
|
"type": "string",
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
|
"quantity": {
|
||||||
|
"type": "float",
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
|
"picture": {
|
||||||
|
"model": "file",
|
||||||
|
"via": "related",
|
||||||
|
"plugin": "upload"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
const createDefaults = require('../create-defaults');
|
||||||
|
|
||||||
|
test('createDefaults', () => {
|
||||||
|
const input = {
|
||||||
|
field: {
|
||||||
|
type: 'text',
|
||||||
|
default: 'someVal',
|
||||||
|
},
|
||||||
|
relation: {
|
||||||
|
model: 'model',
|
||||||
|
via: 'field',
|
||||||
|
default: null, // should be in the defaults
|
||||||
|
},
|
||||||
|
groupes: {
|
||||||
|
type: 'group',
|
||||||
|
default: 'azdaz',
|
||||||
|
},
|
||||||
|
boolField: {
|
||||||
|
type: 'boolean',
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
|
azdaz: {
|
||||||
|
type: 'invalidType',
|
||||||
|
default: 'azdaz',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const expected = {
|
||||||
|
boolField: false,
|
||||||
|
field: 'someVal',
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(createDefaults(input)).toEqual(expected);
|
||||||
|
});
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
const knex = require('knex');
|
||||||
|
const createTable = require('../create-table');
|
||||||
|
|
||||||
|
let con;
|
||||||
|
|
||||||
|
describe('Create Table', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
con = knex({
|
||||||
|
client: 'sqlite',
|
||||||
|
connection: {
|
||||||
|
filename: './test.sqlite',
|
||||||
|
},
|
||||||
|
useNullAsDefault: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('That works', () => {
|
||||||
|
return createTable(
|
||||||
|
{
|
||||||
|
collectionName: 'something',
|
||||||
|
attributes: {
|
||||||
|
id: {
|
||||||
|
type: 'specificType',
|
||||||
|
specificType: 'serial primary key',
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
type: 'string',
|
||||||
|
required: true, // not nullable
|
||||||
|
unique: true, // or [args]
|
||||||
|
default: 'hello',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ knex: con, client: 'pg' }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -9,10 +9,17 @@ module.exports = async ({
|
|||||||
connection,
|
connection,
|
||||||
model,
|
model,
|
||||||
}) => {
|
}) => {
|
||||||
|
const { hasTimestamps } = loadedModel;
|
||||||
|
|
||||||
|
let [createAtCol, updatedAtCol] = ['created_at', 'updated_at'];
|
||||||
|
if (Array.isArray(hasTimestamps)) {
|
||||||
|
[createAtCol, updatedAtCol] = hasTimestamps;
|
||||||
|
}
|
||||||
|
|
||||||
const quote = definition.client === 'pg' ? '"' : '`';
|
const quote = definition.client === 'pg' ? '"' : '`';
|
||||||
|
|
||||||
// Equilize database tables
|
// Equilize database tables
|
||||||
const handler = async (table, attributes) => {
|
const createOrUpdateTable = async (table, attributes) => {
|
||||||
const tableExists = await ORM.knex.schema.hasTable(table);
|
const tableExists = await ORM.knex.schema.hasTable(table);
|
||||||
|
|
||||||
// Apply field type of attributes definition
|
// Apply field type of attributes definition
|
||||||
@ -98,13 +105,13 @@ module.exports = async ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const createTable = async table => {
|
const createTable = async table => {
|
||||||
const defaultAttributeDifinitions = {
|
const defaultAttributeDefinitions = {
|
||||||
mysql: ['id INT AUTO_INCREMENT NOT NULL PRIMARY KEY'],
|
mysql: ['id INT AUTO_INCREMENT NOT NULL PRIMARY KEY'],
|
||||||
pg: ['id SERIAL NOT NULL PRIMARY KEY'],
|
pg: ['id SERIAL NOT NULL PRIMARY KEY'],
|
||||||
sqlite3: ['id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'],
|
sqlite3: ['id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'],
|
||||||
};
|
};
|
||||||
|
|
||||||
let idAttributeBuilder = defaultAttributeDifinitions[definition.client];
|
let idAttributeBuilder = defaultAttributeDefinitions[definition.client];
|
||||||
|
|
||||||
if (definition.primaryKeyType === 'uuid' && definition.client === 'pg') {
|
if (definition.primaryKeyType === 'uuid' && definition.client === 'pg') {
|
||||||
idAttributeBuilder = [
|
idAttributeBuilder = [
|
||||||
@ -132,7 +139,9 @@ module.exports = async ({
|
|||||||
await createTable(table);
|
await createTable(table);
|
||||||
await generateIndexes(table, attributes);
|
await generateIndexes(table, attributes);
|
||||||
await storeTable(table, attributes);
|
await storeTable(table, attributes);
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const columns = Object.keys(attributes);
|
const columns = Object.keys(attributes);
|
||||||
|
|
||||||
// Fetch existing column
|
// Fetch existing column
|
||||||
@ -250,26 +259,17 @@ module.exports = async ({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
await storeTable(table, attributes);
|
await storeTable(table, attributes);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add created_at and updated_at field if timestamp option is true
|
// Add created_at and updated_at field if timestamp option is true
|
||||||
if (loadedModel.hasTimestamps) {
|
if (hasTimestamps) {
|
||||||
definition.attributes[
|
definition.attributes[createAtCol] = {
|
||||||
_.isString(loadedModel.hasTimestamps[0])
|
|
||||||
? loadedModel.hasTimestamps[0]
|
|
||||||
: 'created_at'
|
|
||||||
] = {
|
|
||||||
type: 'timestamp',
|
type: 'timestamp',
|
||||||
};
|
};
|
||||||
definition.attributes[
|
definition.attributes[updatedAtCol] = {
|
||||||
_.isString(loadedModel.hasTimestamps[1])
|
|
||||||
? loadedModel.hasTimestamps[1]
|
|
||||||
: 'updated_at'
|
|
||||||
] = {
|
|
||||||
type: 'timestampUpdate',
|
type: 'timestampUpdate',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -279,7 +279,7 @@ module.exports = async ({
|
|||||||
|
|
||||||
// Equilize tables
|
// Equilize tables
|
||||||
if (connection.options && connection.options.autoMigration !== false) {
|
if (connection.options && connection.options.autoMigration !== false) {
|
||||||
await handler(loadedModel.tableName, definition.attributes);
|
await createOrUpdateTable(loadedModel.tableName, definition.attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equilize polymorphic releations
|
// Equilize polymorphic releations
|
||||||
@ -304,7 +304,7 @@ module.exports = async ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (connection.options && connection.options.autoMigration !== false) {
|
if (connection.options && connection.options.autoMigration !== false) {
|
||||||
await handler(`${loadedModel.tableName}_morph`, attributes);
|
await createOrUpdateTable(`${loadedModel.tableName}_morph`, attributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -339,22 +339,14 @@ module.exports = async ({
|
|||||||
manyRelation
|
manyRelation
|
||||||
);
|
);
|
||||||
|
|
||||||
await handler(table, attributes);
|
await createOrUpdateTable(table, attributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove from attributes (auto handled by bookshlef and not displayed on ctb)
|
// Remove from attributes (auto handled by bookshlef and not displayed on ctb)
|
||||||
if (loadedModel.hasTimestamps) {
|
if (hasTimestamps) {
|
||||||
delete definition.attributes[
|
delete definition.attributes[createAtCol];
|
||||||
_.isString(loadedModel.hasTimestamps[0])
|
delete definition.attributes[updatedAtCol];
|
||||||
? loadedModel.hasTimestamps[0]
|
|
||||||
: 'created_at'
|
|
||||||
];
|
|
||||||
delete definition.attributes[
|
|
||||||
_.isString(loadedModel.hasTimestamps[1])
|
|
||||||
? loadedModel.hasTimestamps[1]
|
|
||||||
: 'updated_at'
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -412,6 +404,9 @@ const getType = ({ definition, attribute, name, tableExists = false }) => {
|
|||||||
case 'pg':
|
case 'pg':
|
||||||
return 'timestamp with time zone';
|
return 'timestamp with time zone';
|
||||||
case 'sqlite3':
|
case 'sqlite3':
|
||||||
|
if (tableExists) {
|
||||||
|
return 'timestamp DEFAULT NULL';
|
||||||
|
}
|
||||||
return 'timestamp DEFAULT CURRENT_TIMESTAMP';
|
return 'timestamp DEFAULT CURRENT_TIMESTAMP';
|
||||||
default:
|
default:
|
||||||
return 'timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
|
return 'timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
|
||||||
|
|||||||
20
packages/strapi-hook-bookshelf/lib/create-defaults.js
Normal file
20
packages/strapi-hook-bookshelf/lib/create-defaults.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { typesArray } = require('./types');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return a map of default values
|
||||||
|
* @param {*} attributes
|
||||||
|
*/
|
||||||
|
function createDefaults(attributes) {
|
||||||
|
return Object.keys(attributes).reduce((acc, key) => {
|
||||||
|
const { type, default: defaultVal } = attributes[key];
|
||||||
|
if (typesArray.includes(type) && defaultVal !== undefined) {
|
||||||
|
acc[key] = defaultVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createDefaults;
|
||||||
99
packages/strapi-hook-bookshelf/lib/create-table.js
Normal file
99
packages/strapi-hook-bookshelf/lib/create-table.js
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
const createTable = ({ collectionName, attributes }, { knex, client }) => {
|
||||||
|
return knex.schema.createTable(collectionName, table => {
|
||||||
|
const createCol = createColumn({ table, client });
|
||||||
|
// create every columns
|
||||||
|
Object.keys(attributes).forEach(name => {
|
||||||
|
const attribute = attributes[name];
|
||||||
|
const { unique, default: defaultValue, required } = attribute;
|
||||||
|
|
||||||
|
const col = createCol({ name, ...attribute });
|
||||||
|
|
||||||
|
// apply required
|
||||||
|
if (required === true) col.notNullable();
|
||||||
|
// apply defaultValue
|
||||||
|
if (defaultValue !== undefined) col.defaultTo(knex.raw(defaultValue));
|
||||||
|
// apply unique constraint
|
||||||
|
if (unique === true) table.unique(name);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const createColumn = ({ table, client }) => attribute => {
|
||||||
|
const { name, type } = attribute;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case 'specificType':
|
||||||
|
return table.specificType(name, attribute.specificType);
|
||||||
|
case 'uuid':
|
||||||
|
return table.uuid(name);
|
||||||
|
case 'text':
|
||||||
|
return table.text(name);
|
||||||
|
// return client === 'pg' ? 'text' : 'longtext';
|
||||||
|
case 'json':
|
||||||
|
return table.jsonb(name);
|
||||||
|
// return client === 'pg' ? 'jsonb' : 'longtext';
|
||||||
|
case 'enumeration':
|
||||||
|
case 'string':
|
||||||
|
case 'password':
|
||||||
|
case 'email':
|
||||||
|
// TODO: add optional length;
|
||||||
|
return table.string(name);
|
||||||
|
// return 'varchar(255)';
|
||||||
|
case 'integer':
|
||||||
|
// TODO: support unsigned
|
||||||
|
return table.integer(name);
|
||||||
|
// return client === 'pg' ? 'integer' : 'int';
|
||||||
|
case 'biginteger':
|
||||||
|
return table.bigInteger(name);
|
||||||
|
// return client === 'pg' ? 'bigint' : 'bigint(53)';
|
||||||
|
case 'float':
|
||||||
|
// TODO: support precision and scale
|
||||||
|
return table.float(name);
|
||||||
|
// return client === 'pg' ? 'double precision' : 'double';
|
||||||
|
case 'decimal':
|
||||||
|
// TODO: support precision and scale
|
||||||
|
// return 'decimal(10,2)';
|
||||||
|
return table.decimal(name);
|
||||||
|
case 'date':
|
||||||
|
return table.date(name);
|
||||||
|
case 'time':
|
||||||
|
// TODO: support precision for mysql
|
||||||
|
return table.time(name);
|
||||||
|
case 'datetime':
|
||||||
|
// support precision
|
||||||
|
return table.datetime(name);
|
||||||
|
case 'timestamp':
|
||||||
|
// TODO: support precision for mysql and useTz
|
||||||
|
return table.timestamp(name);
|
||||||
|
// if (client === 'pg') {
|
||||||
|
// return 'timestamp with time zone';
|
||||||
|
// } else if (client === 'sqlite3' && tableExists) {
|
||||||
|
// return 'timestamp DEFAULT NULL';
|
||||||
|
// }
|
||||||
|
// return 'timestamp DEFAULT CURRENT_TIMESTAMP';
|
||||||
|
case 'timestampUpdate':
|
||||||
|
switch (client) {
|
||||||
|
case 'pg':
|
||||||
|
return table.specificType(name, 'timestamp with time zone');
|
||||||
|
case 'sqlite3':
|
||||||
|
return table.specificType(
|
||||||
|
name,
|
||||||
|
'timestamp DEFAULT CURRENT_TIMESTAMP'
|
||||||
|
);
|
||||||
|
default:
|
||||||
|
return table.specificType(
|
||||||
|
name,
|
||||||
|
'timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
case 'binary':
|
||||||
|
// TODO: handle length for mysql
|
||||||
|
return table.binary(name);
|
||||||
|
case 'boolean':
|
||||||
|
return table.boolean(name);
|
||||||
|
default:
|
||||||
|
throw new Error(`Unsupported type ${type} for attrbiute ${name}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = createTable;
|
||||||
96
packages/strapi-hook-bookshelf/lib/get-query-params.js
Normal file
96
packages/strapi-hook-bookshelf/lib/get-query-params.js
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
'use strict';
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
|
module.exports = (value, type, key) => {
|
||||||
|
const result = {};
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case '=':
|
||||||
|
result.key = `where.${key}`;
|
||||||
|
result.value = {
|
||||||
|
symbol: '=',
|
||||||
|
value,
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case '_ne':
|
||||||
|
result.key = `where.${key}`;
|
||||||
|
result.value = {
|
||||||
|
symbol: '!=',
|
||||||
|
value,
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case '_lt':
|
||||||
|
result.key = `where.${key}`;
|
||||||
|
result.value = {
|
||||||
|
symbol: '<',
|
||||||
|
value,
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case '_gt':
|
||||||
|
result.key = `where.${key}`;
|
||||||
|
result.value = {
|
||||||
|
symbol: '>',
|
||||||
|
value,
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case '_lte':
|
||||||
|
result.key = `where.${key}`;
|
||||||
|
result.value = {
|
||||||
|
symbol: '<=',
|
||||||
|
value,
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case '_gte':
|
||||||
|
result.key = `where.${key}`;
|
||||||
|
result.value = {
|
||||||
|
symbol: '>=',
|
||||||
|
value,
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case '_sort':
|
||||||
|
result.key = 'sort';
|
||||||
|
result.value = {
|
||||||
|
key,
|
||||||
|
order: value.toUpperCase(),
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case '_start':
|
||||||
|
result.key = 'start';
|
||||||
|
result.value = parseFloat(value);
|
||||||
|
break;
|
||||||
|
case '_limit':
|
||||||
|
result.key = 'limit';
|
||||||
|
result.value = parseFloat(value);
|
||||||
|
break;
|
||||||
|
case '_populate':
|
||||||
|
result.key = 'populate';
|
||||||
|
result.value = value;
|
||||||
|
break;
|
||||||
|
case '_contains':
|
||||||
|
case '_containss':
|
||||||
|
result.key = `where.${key}`;
|
||||||
|
result.value = {
|
||||||
|
symbol: 'like',
|
||||||
|
value: `%${value}%`,
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case '_in':
|
||||||
|
result.key = `where.${key}`;
|
||||||
|
result.value = {
|
||||||
|
symbol: 'IN',
|
||||||
|
value: _.castArray(value),
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case '_nin':
|
||||||
|
result.key = `where.${key}`;
|
||||||
|
result.value = {
|
||||||
|
symbol: 'NOT IN',
|
||||||
|
value: _.castArray(value),
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
File diff suppressed because it is too large
Load Diff
736
packages/strapi-hook-bookshelf/lib/mount-models.js
Normal file
736
packages/strapi-hook-bookshelf/lib/mount-models.js
Normal file
@ -0,0 +1,736 @@
|
|||||||
|
'use strict';
|
||||||
|
const _ = require('lodash');
|
||||||
|
const pluralize = require('pluralize');
|
||||||
|
|
||||||
|
const utilsModels = require('strapi-utils').models;
|
||||||
|
const utils = require('./utils/');
|
||||||
|
const relations = require('./relations');
|
||||||
|
const buildDatabaseSchema = require('./buildDatabaseSchema');
|
||||||
|
|
||||||
|
const PIVOT_PREFIX = '_pivot_';
|
||||||
|
|
||||||
|
const getDatabaseName = connection => {
|
||||||
|
const dbName = _.get(connection.settings, 'database');
|
||||||
|
const dbSchema = _.get(connection.settings, 'schema', 'public');
|
||||||
|
switch (_.get(connection.settings, 'client')) {
|
||||||
|
case 'sqlite3':
|
||||||
|
return 'main';
|
||||||
|
case 'pg':
|
||||||
|
return `${dbName}.${dbSchema}`;
|
||||||
|
case 'mysql':
|
||||||
|
return dbName;
|
||||||
|
default:
|
||||||
|
return dbName;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = ({ models, target, plugin = false }, ctx) => {
|
||||||
|
const { GLOBALS, connection, ORM } = ctx;
|
||||||
|
|
||||||
|
// Parse every authenticated model.
|
||||||
|
const updates = Object.keys(models).map(async model => {
|
||||||
|
const definition = models[model];
|
||||||
|
definition.globalName = _.upperFirst(_.camelCase(definition.globalId));
|
||||||
|
definition.associations = [];
|
||||||
|
|
||||||
|
// Define local GLOBALS to expose every models in this file.
|
||||||
|
GLOBALS[definition.globalId] = {};
|
||||||
|
|
||||||
|
// Add some informations about ORM & client connection & tableName
|
||||||
|
definition.orm = 'bookshelf';
|
||||||
|
definition.databaseName = getDatabaseName(connection);
|
||||||
|
definition.client = _.get(connection.settings, 'client');
|
||||||
|
_.defaults(definition, {
|
||||||
|
primaryKey: 'id',
|
||||||
|
primaryKeyType: _.get(definition, 'options.idAttributeType', 'integer'),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Use default timestamp column names if value is `true`
|
||||||
|
if (_.get(definition, 'options.timestamps', false) === true) {
|
||||||
|
_.set(definition, 'options.timestamps', ['created_at', 'updated_at']);
|
||||||
|
}
|
||||||
|
// Use false for values other than `Boolean` or `Array`
|
||||||
|
if (
|
||||||
|
!_.isArray(_.get(definition, 'options.timestamps')) &&
|
||||||
|
!_.isBoolean(_.get(definition, 'options.timestamps'))
|
||||||
|
) {
|
||||||
|
_.set(definition, 'options.timestamps', false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register the final model for Bookshelf.
|
||||||
|
const loadedModel = _.assign(
|
||||||
|
{
|
||||||
|
tableName: definition.collectionName,
|
||||||
|
hasTimestamps: _.get(definition, 'options.timestamps', false),
|
||||||
|
idAttribute: _.get(definition, 'options.idAttribute', 'id'),
|
||||||
|
associations: [],
|
||||||
|
defaults: Object.keys(definition.attributes).reduce((acc, current) => {
|
||||||
|
if (
|
||||||
|
definition.attributes[current].type &&
|
||||||
|
definition.attributes[current].default
|
||||||
|
) {
|
||||||
|
acc[current] = definition.attributes[current].default;
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, {}),
|
||||||
|
},
|
||||||
|
definition.options
|
||||||
|
);
|
||||||
|
|
||||||
|
if (_.isString(_.get(connection, 'options.pivot_prefix'))) {
|
||||||
|
loadedModel.toJSON = function(options = {}) {
|
||||||
|
const { shallow = false, omitPivot = false } = options;
|
||||||
|
const attributes = this.serialize(options);
|
||||||
|
|
||||||
|
if (!shallow) {
|
||||||
|
const pivot = this.pivot && !omitPivot && this.pivot.attributes;
|
||||||
|
|
||||||
|
// Remove pivot attributes with prefix.
|
||||||
|
_.keys(pivot).forEach(
|
||||||
|
key => delete attributes[`${PIVOT_PREFIX}${key}`]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add pivot attributes without prefix.
|
||||||
|
const pivotAttributes = _.mapKeys(
|
||||||
|
pivot,
|
||||||
|
(value, key) => `${connection.options.pivot_prefix}${key}`
|
||||||
|
);
|
||||||
|
|
||||||
|
return Object.assign({}, attributes, pivotAttributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return attributes;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the global variable with the
|
||||||
|
// capitalized model name.
|
||||||
|
if (!plugin) {
|
||||||
|
global[definition.globalName] = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasGroups =
|
||||||
|
Object.values(definition.attributes).filter(({ type } = {}) => {
|
||||||
|
return type && type === 'group';
|
||||||
|
}).length > 0;
|
||||||
|
|
||||||
|
if (hasGroups) {
|
||||||
|
const groupAttributes = Object.keys(definition.attributes).filter(
|
||||||
|
key => definition.attributes[key].type === 'group'
|
||||||
|
);
|
||||||
|
|
||||||
|
// create group model
|
||||||
|
const joinTable = `${definition.collectionName}_groups`;
|
||||||
|
const joinModel = ORM.Model.extend({
|
||||||
|
tableName: joinTable,
|
||||||
|
slice() {
|
||||||
|
return this.morphTo(
|
||||||
|
'slice',
|
||||||
|
...groupAttributes.map(key => GLOBALS[strapi.groups[key].globalId])
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
groupAttributes.forEach(name => {
|
||||||
|
loadedModel[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');
|
||||||
|
table.integer('order').unsigned();
|
||||||
|
table.string('slice_type');
|
||||||
|
table.integer('slice_id').unsigned();
|
||||||
|
table.integer(`${pluralize.singular(model)}_id`).unsigned();
|
||||||
|
table.timestamps(null, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add every relationships to the loaded model for Bookshelf.
|
||||||
|
// Basic attributes don't need this-- only relations.
|
||||||
|
_.forEach(definition.attributes, (details, name) => {
|
||||||
|
if (details.type !== undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const verbose =
|
||||||
|
_.get(
|
||||||
|
utilsModels.getNature(details, name, undefined, model.toLowerCase()),
|
||||||
|
'verbose'
|
||||||
|
) || '';
|
||||||
|
|
||||||
|
// Build associations key
|
||||||
|
utilsModels.defineAssociations(
|
||||||
|
model.toLowerCase(),
|
||||||
|
definition,
|
||||||
|
details,
|
||||||
|
name
|
||||||
|
);
|
||||||
|
|
||||||
|
let globalId;
|
||||||
|
const globalName = details.model || details.collection || '';
|
||||||
|
|
||||||
|
// Exclude polymorphic association.
|
||||||
|
if (globalName !== '*') {
|
||||||
|
globalId = details.plugin
|
||||||
|
? _.get(
|
||||||
|
strapi.plugins,
|
||||||
|
`${details.plugin}.models.${globalName.toLowerCase()}.globalId`
|
||||||
|
)
|
||||||
|
: _.get(strapi.models, `${globalName.toLowerCase()}.globalId`);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (verbose) {
|
||||||
|
case 'hasOne': {
|
||||||
|
const target = details.plugin
|
||||||
|
? strapi.plugins[details.plugin].models[details.model]
|
||||||
|
: strapi.models[details.model];
|
||||||
|
|
||||||
|
const FK = _.findKey(target.attributes, details => {
|
||||||
|
if (
|
||||||
|
details.hasOwnProperty('model') &&
|
||||||
|
details.model === model &&
|
||||||
|
details.hasOwnProperty('via') &&
|
||||||
|
details.via === name
|
||||||
|
) {
|
||||||
|
return details;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const columnName = _.get(target.attributes, [FK, 'columnName'], FK);
|
||||||
|
|
||||||
|
loadedModel[name] = function() {
|
||||||
|
return this.hasOne(GLOBALS[globalId], columnName);
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'hasMany': {
|
||||||
|
const columnName = details.plugin
|
||||||
|
? _.get(
|
||||||
|
strapi.plugins,
|
||||||
|
`${
|
||||||
|
details.plugin
|
||||||
|
}.models.${globalId.toLowerCase()}.attributes.${
|
||||||
|
details.via
|
||||||
|
}.columnName`,
|
||||||
|
details.via
|
||||||
|
)
|
||||||
|
: _.get(
|
||||||
|
strapi.models[globalId.toLowerCase()].attributes,
|
||||||
|
`${details.via}.columnName`,
|
||||||
|
details.via
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set this info to be able to see if this field is a real database's field.
|
||||||
|
details.isVirtual = true;
|
||||||
|
|
||||||
|
loadedModel[name] = function() {
|
||||||
|
return this.hasMany(GLOBALS[globalId], columnName);
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'belongsTo': {
|
||||||
|
loadedModel[name] = function() {
|
||||||
|
return this.belongsTo(
|
||||||
|
GLOBALS[globalId],
|
||||||
|
_.get(details, 'columnName', name)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'belongsToMany': {
|
||||||
|
const collection = details.plugin
|
||||||
|
? strapi.plugins[details.plugin].models[details.collection]
|
||||||
|
: strapi.models[details.collection];
|
||||||
|
|
||||||
|
const collectionName =
|
||||||
|
_.get(details, 'collectionName') ||
|
||||||
|
utilsModels.getCollectionName(
|
||||||
|
collection.attributes[details.via],
|
||||||
|
details
|
||||||
|
);
|
||||||
|
|
||||||
|
const relationship = collection.attributes[details.via];
|
||||||
|
|
||||||
|
// Force singular foreign key
|
||||||
|
relationship.attribute = pluralize.singular(relationship.collection);
|
||||||
|
details.attribute = pluralize.singular(details.collection);
|
||||||
|
|
||||||
|
// Define PK column
|
||||||
|
details.column = utils.getPK(model, strapi.models);
|
||||||
|
relationship.column = utils.getPK(details.collection, strapi.models);
|
||||||
|
|
||||||
|
// Sometimes the many-to-many relationships
|
||||||
|
// is on the same keys on the same models (ex: `friends` key in model `User`)
|
||||||
|
if (
|
||||||
|
`${details.attribute}_${details.column}` ===
|
||||||
|
`${relationship.attribute}_${relationship.column}`
|
||||||
|
) {
|
||||||
|
relationship.attribute = pluralize.singular(details.via);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set this info to be able to see if this field is a real database's field.
|
||||||
|
details.isVirtual = true;
|
||||||
|
|
||||||
|
loadedModel[name] = function() {
|
||||||
|
if (
|
||||||
|
_.isArray(_.get(details, 'withPivot')) &&
|
||||||
|
!_.isEmpty(details.withPivot)
|
||||||
|
) {
|
||||||
|
return this.belongsToMany(
|
||||||
|
GLOBALS[globalId],
|
||||||
|
collectionName,
|
||||||
|
`${relationship.attribute}_${relationship.column}`,
|
||||||
|
`${details.attribute}_${details.column}`
|
||||||
|
).withPivot(details.withPivot);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.belongsToMany(
|
||||||
|
GLOBALS[globalId],
|
||||||
|
collectionName,
|
||||||
|
`${relationship.attribute}_${relationship.column}`,
|
||||||
|
`${details.attribute}_${details.column}`
|
||||||
|
);
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'morphOne': {
|
||||||
|
const model = details.plugin
|
||||||
|
? strapi.plugins[details.plugin].models[details.model]
|
||||||
|
: strapi.models[details.model];
|
||||||
|
|
||||||
|
const globalId = `${model.collectionName}_morph`;
|
||||||
|
|
||||||
|
loadedModel[name] = function() {
|
||||||
|
return this.morphOne(
|
||||||
|
GLOBALS[globalId],
|
||||||
|
details.via,
|
||||||
|
`${definition.collectionName}`
|
||||||
|
).query(qb => {
|
||||||
|
qb.where(
|
||||||
|
_.get(model, `attributes.${details.via}.filter`, 'field'),
|
||||||
|
name
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'morphMany': {
|
||||||
|
const collection = details.plugin
|
||||||
|
? strapi.plugins[details.plugin].models[details.collection]
|
||||||
|
: strapi.models[details.collection];
|
||||||
|
|
||||||
|
const globalId = `${collection.collectionName}_morph`;
|
||||||
|
|
||||||
|
loadedModel[name] = function() {
|
||||||
|
return this.morphMany(
|
||||||
|
GLOBALS[globalId],
|
||||||
|
details.via,
|
||||||
|
`${definition.collectionName}`
|
||||||
|
).query(qb => {
|
||||||
|
qb.where(
|
||||||
|
_.get(collection, `attributes.${details.via}.filter`, 'field'),
|
||||||
|
name
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'belongsToMorph':
|
||||||
|
case 'belongsToManyMorph': {
|
||||||
|
const association = definition.associations.find(
|
||||||
|
association => association.alias === name
|
||||||
|
);
|
||||||
|
|
||||||
|
const morphValues = association.related.map(id => {
|
||||||
|
let models = Object.values(strapi.models).filter(
|
||||||
|
model => model.globalId === id
|
||||||
|
);
|
||||||
|
|
||||||
|
if (models.length === 0) {
|
||||||
|
models = Object.keys(strapi.plugins).reduce((acc, current) => {
|
||||||
|
const models = Object.values(
|
||||||
|
strapi.plugins[current].models
|
||||||
|
).filter(model => model.globalId === id);
|
||||||
|
|
||||||
|
if (acc.length === 0 && models.length > 0) {
|
||||||
|
acc = models;
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (models.length === 0) {
|
||||||
|
strapi.log.error(`Impossible to register the '${model}' model.`);
|
||||||
|
strapi.log.error(
|
||||||
|
'The collection name cannot be found for the morphTo method.'
|
||||||
|
);
|
||||||
|
strapi.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
return models[0].collectionName;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Define new model.
|
||||||
|
const options = {
|
||||||
|
tableName: `${definition.collectionName}_morph`,
|
||||||
|
[definition.collectionName]: function() {
|
||||||
|
return this.belongsTo(
|
||||||
|
GLOBALS[definition.globalId],
|
||||||
|
`${definition.collectionName}_id`
|
||||||
|
);
|
||||||
|
},
|
||||||
|
related: function() {
|
||||||
|
return this.morphTo(
|
||||||
|
name,
|
||||||
|
...association.related.map((id, index) => [
|
||||||
|
GLOBALS[id],
|
||||||
|
morphValues[index],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
GLOBALS[options.tableName] = ORM.Model.extend(options);
|
||||||
|
|
||||||
|
// Set polymorphic table name to the main model.
|
||||||
|
target[model].morph = GLOBALS[options.tableName];
|
||||||
|
|
||||||
|
// Hack Bookshelf to create a many-to-many polymorphic association.
|
||||||
|
// Upload has many Upload_morph that morph to different model.
|
||||||
|
loadedModel[name] = function() {
|
||||||
|
if (verbose === 'belongsToMorph') {
|
||||||
|
return this.hasOne(
|
||||||
|
GLOBALS[options.tableName],
|
||||||
|
`${definition.collectionName}_id`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.hasMany(
|
||||||
|
GLOBALS[options.tableName],
|
||||||
|
`${definition.collectionName}_id`
|
||||||
|
);
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Call this callback function after we are done parsing
|
||||||
|
// all attributes for relationships-- see below.
|
||||||
|
|
||||||
|
try {
|
||||||
|
// External function to map key that has been updated with `columnName`
|
||||||
|
const mapper = (params = {}) => {
|
||||||
|
if (definition.client === 'mysql' || definition.client === 'sqlite3') {
|
||||||
|
Object.keys(params).map(key => {
|
||||||
|
const attr = definition.attributes[key] || {};
|
||||||
|
|
||||||
|
if (attr.type === 'json') {
|
||||||
|
params[key] = JSON.stringify(params[key]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return _.mapKeys(params, (value, key) => {
|
||||||
|
const attr = definition.attributes[key] || {};
|
||||||
|
|
||||||
|
return _.isPlainObject(attr) && _.isString(attr['columnName'])
|
||||||
|
? attr['columnName']
|
||||||
|
: key;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update serialize to reformat data for polymorphic associations.
|
||||||
|
loadedModel.serialize = function(options) {
|
||||||
|
const attrs = _.clone(this.attributes);
|
||||||
|
|
||||||
|
if (options && options.shallow) {
|
||||||
|
return attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
const relations = this.relations;
|
||||||
|
|
||||||
|
// Extract association except polymorphic.
|
||||||
|
const associations = definition.associations.filter(
|
||||||
|
association =>
|
||||||
|
association.nature.toLowerCase().indexOf('morph') === -1
|
||||||
|
);
|
||||||
|
// Extract polymorphic association.
|
||||||
|
const polymorphicAssociations = definition.associations.filter(
|
||||||
|
association =>
|
||||||
|
association.nature.toLowerCase().indexOf('morph') !== -1
|
||||||
|
);
|
||||||
|
|
||||||
|
polymorphicAssociations.map(association => {
|
||||||
|
// Retrieve relation Bookshelf object.
|
||||||
|
const relation = relations[association.alias];
|
||||||
|
|
||||||
|
if (relation) {
|
||||||
|
// Extract raw JSON data.
|
||||||
|
attrs[association.alias] = relation.toJSON
|
||||||
|
? relation.toJSON(options)
|
||||||
|
: relation;
|
||||||
|
|
||||||
|
// Retrieve opposite model.
|
||||||
|
const model = association.plugin
|
||||||
|
? strapi.plugins[association.plugin].models[
|
||||||
|
association.collection || association.model
|
||||||
|
]
|
||||||
|
: strapi.models[association.collection || association.model];
|
||||||
|
|
||||||
|
// Reformat data by bypassing the many-to-many relationship.
|
||||||
|
switch (association.nature) {
|
||||||
|
case 'oneToManyMorph':
|
||||||
|
attrs[association.alias] =
|
||||||
|
attrs[association.alias][model.collectionName];
|
||||||
|
break;
|
||||||
|
case 'manyToManyMorph':
|
||||||
|
attrs[association.alias] = attrs[association.alias].map(
|
||||||
|
rel => rel[model.collectionName]
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case 'oneMorphToOne':
|
||||||
|
attrs[association.alias] = attrs[association.alias].related;
|
||||||
|
break;
|
||||||
|
case 'manyMorphToOne':
|
||||||
|
case 'manyMorphToMany':
|
||||||
|
attrs[association.alias] = attrs[association.alias].map(
|
||||||
|
obj => obj.related
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
associations.map(association => {
|
||||||
|
const relation = relations[association.alias];
|
||||||
|
|
||||||
|
if (relation) {
|
||||||
|
// Extract raw JSON data.
|
||||||
|
attrs[association.alias] = relation.toJSON
|
||||||
|
? relation.toJSON(options)
|
||||||
|
: relation;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return attrs;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Initialize lifecycle callbacks.
|
||||||
|
loadedModel.initialize = function() {
|
||||||
|
const lifecycle = {
|
||||||
|
creating: 'beforeCreate',
|
||||||
|
created: 'afterCreate',
|
||||||
|
destroying: 'beforeDestroy',
|
||||||
|
destroyed: 'afterDestroy',
|
||||||
|
updating: 'beforeUpdate',
|
||||||
|
updated: 'afterUpdate',
|
||||||
|
fetching: 'beforeFetch',
|
||||||
|
'fetching:collection': 'beforeFetchAll',
|
||||||
|
fetched: 'afterFetch',
|
||||||
|
'fetched:collection': 'afterFetchAll',
|
||||||
|
saving: 'beforeSave',
|
||||||
|
saved: 'afterSave',
|
||||||
|
};
|
||||||
|
|
||||||
|
_.forEach(lifecycle, (fn, key) => {
|
||||||
|
if (_.isFunction(target[model.toLowerCase()][fn])) {
|
||||||
|
this.on(key, target[model.toLowerCase()][fn]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const findModelByAssoc = ({ assoc }) => {
|
||||||
|
return assoc.plugin
|
||||||
|
? strapi.plugins[assoc.plugin].models[
|
||||||
|
assoc.collection || assoc.model
|
||||||
|
]
|
||||||
|
: strapi.models[assoc.collection || assoc.model];
|
||||||
|
};
|
||||||
|
|
||||||
|
const isPolymorphic = ({ assoc }) => {
|
||||||
|
return assoc.nature.toLowerCase().indexOf('morph') !== -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatPolymorphicPopulate = ({ assoc, path, prefix = '' }) => {
|
||||||
|
if (_.isString(path) && path === assoc.via) {
|
||||||
|
return `related.${assoc.via}`;
|
||||||
|
} else if (_.isString(path) && path === assoc.alias) {
|
||||||
|
// MorphTo side.
|
||||||
|
if (assoc.related) {
|
||||||
|
return `${prefix}${assoc.alias}.related`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// oneToMorph or manyToMorph side.
|
||||||
|
// Retrieve collection name because we are using it to build our hidden model.
|
||||||
|
const model = findModelByAssoc({ assoc });
|
||||||
|
|
||||||
|
return {
|
||||||
|
[`${prefix}${assoc.alias}.${model.collectionName}`]: function(
|
||||||
|
query
|
||||||
|
) {
|
||||||
|
query.orderBy('created_at', 'desc');
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update withRelated level to bypass many-to-many association for polymorphic relationshiips.
|
||||||
|
// Apply only during fetching.
|
||||||
|
this.on('fetching fetching:collection', (instance, attrs, options) => {
|
||||||
|
if (_.isArray(options.withRelated)) {
|
||||||
|
options.withRelated = options.withRelated
|
||||||
|
.map(path => {
|
||||||
|
const assoc = definition.associations.find(
|
||||||
|
assoc => assoc.alias === path || assoc.via === path
|
||||||
|
);
|
||||||
|
|
||||||
|
if (assoc && isPolymorphic({ assoc })) {
|
||||||
|
return formatPolymorphicPopulate({
|
||||||
|
assoc,
|
||||||
|
path,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let extraAssocs = [];
|
||||||
|
if (assoc) {
|
||||||
|
const assocModel = findModelByAssoc({ assoc });
|
||||||
|
|
||||||
|
extraAssocs = assocModel.associations
|
||||||
|
.filter(assoc => isPolymorphic({ assoc }))
|
||||||
|
.map(assoc =>
|
||||||
|
formatPolymorphicPopulate({
|
||||||
|
assoc,
|
||||||
|
path: assoc.alias,
|
||||||
|
prefix: `${path}.`,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [path, ...extraAssocs];
|
||||||
|
})
|
||||||
|
.reduce((acc, paths) => acc.concat(paths), []);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _.isFunction(target[model.toLowerCase()]['beforeFetchAll'])
|
||||||
|
? target[model.toLowerCase()]['beforeFetchAll']
|
||||||
|
: Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
//eslint-disable-next-line
|
||||||
|
this.on('saving', (instance, attrs, options) => {
|
||||||
|
instance.attributes = mapper(instance.attributes);
|
||||||
|
attrs = mapper(attrs);
|
||||||
|
|
||||||
|
return _.isFunction(target[model.toLowerCase()]['beforeSave'])
|
||||||
|
? target[model.toLowerCase()]['beforeSave']
|
||||||
|
: Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Convert to JSON format stringify json for mysql database
|
||||||
|
if (definition.client === 'mysql' || definition.client === 'sqlite3') {
|
||||||
|
const events = [
|
||||||
|
{
|
||||||
|
name: 'saved',
|
||||||
|
target: 'afterSave',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'fetched',
|
||||||
|
target: 'afterFetch',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'fetched:collection',
|
||||||
|
target: 'afterFetchAll',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const jsonFormatter = attributes => {
|
||||||
|
Object.keys(attributes).map(key => {
|
||||||
|
const attr = definition.attributes[key] || {};
|
||||||
|
|
||||||
|
if (attr.type === 'json') {
|
||||||
|
attributes[key] = JSON.parse(attributes[key]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
events.forEach(event => {
|
||||||
|
let fn;
|
||||||
|
|
||||||
|
if (event.name.indexOf('collection') !== -1) {
|
||||||
|
fn = instance =>
|
||||||
|
instance.models.map(entry => {
|
||||||
|
jsonFormatter(entry.attributes);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
fn = instance => jsonFormatter(instance.attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.on(event.name, instance => {
|
||||||
|
fn(instance);
|
||||||
|
|
||||||
|
return _.isFunction(target[model.toLowerCase()][event.target])
|
||||||
|
? target[model.toLowerCase()][event.target]
|
||||||
|
: Promise.resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
loadedModel.hidden = _.keys(
|
||||||
|
_.keyBy(
|
||||||
|
_.filter(definition.attributes, (value, key) => {
|
||||||
|
if (
|
||||||
|
value.hasOwnProperty('columnName') &&
|
||||||
|
!_.isEmpty(value.columnName) &&
|
||||||
|
value.columnName !== key
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
'columnName'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
GLOBALS[definition.globalId] = ORM.Model.extend(loadedModel);
|
||||||
|
|
||||||
|
if (!plugin) {
|
||||||
|
// Only expose as real global variable the models which
|
||||||
|
// are not scoped in a plugin.
|
||||||
|
global[definition.globalId] = GLOBALS[definition.globalId];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expose ORM functions through the `strapi.models[xxx]`
|
||||||
|
// or `strapi.plugins[xxx].models[yyy]` object.
|
||||||
|
target[model] = _.assign(GLOBALS[definition.globalId], target[model]);
|
||||||
|
|
||||||
|
// Push attributes to be aware of model schema.
|
||||||
|
target[model]._attributes = definition.attributes;
|
||||||
|
target[model].updateRelations = relations.update;
|
||||||
|
|
||||||
|
return buildDatabaseSchema({
|
||||||
|
ORM,
|
||||||
|
definition,
|
||||||
|
loadedModel,
|
||||||
|
connection,
|
||||||
|
model: target[model],
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
strapi.log.error(`Impossible to register the '${model}' model.`);
|
||||||
|
strapi.log.error(err);
|
||||||
|
strapi.stop();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.all(updates);
|
||||||
|
};
|
||||||
25
packages/strapi-hook-bookshelf/lib/types.js
Normal file
25
packages/strapi-hook-bookshelf/lib/types.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
const types = {
|
||||||
|
UUID: 'uuid',
|
||||||
|
TEXT: 'text',
|
||||||
|
JSON: 'json',
|
||||||
|
STRING: 'string',
|
||||||
|
ENUM: 'enumeration',
|
||||||
|
PASSWORD: 'password',
|
||||||
|
EMAIL: 'email',
|
||||||
|
INTEGER: 'integer',
|
||||||
|
BIGINTEGER: 'biginteger',
|
||||||
|
FLOAT: 'float',
|
||||||
|
DECIMAL: 'decimal',
|
||||||
|
DATE: 'date',
|
||||||
|
TIME: 'time',
|
||||||
|
DATETIME: 'datetime',
|
||||||
|
TIMESTAMP: 'timestamp',
|
||||||
|
BOOLEAN: 'boolean',
|
||||||
|
};
|
||||||
|
|
||||||
|
const typesArray = Object.values(types);
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
types,
|
||||||
|
typesArray,
|
||||||
|
};
|
||||||
@ -30,7 +30,7 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"no tests yet\""
|
"test": "jest"
|
||||||
},
|
},
|
||||||
"author": {
|
"author": {
|
||||||
"email": "hi@strapi.io",
|
"email": "hi@strapi.io",
|
||||||
|
|||||||
@ -507,6 +507,10 @@ module.exports = {
|
|||||||
return [trimmedNotConfigurableAttributes, errors];
|
return [trimmedNotConfigurableAttributes, errors];
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getConnections() {
|
||||||
|
return _.keys(strapi.config.currentEnvironment.database.connections);
|
||||||
|
},
|
||||||
|
|
||||||
clearRelations(model, source, force) {
|
clearRelations(model, source, force) {
|
||||||
const errors = [];
|
const errors = [];
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,7 @@ const {
|
|||||||
bootstrap,
|
bootstrap,
|
||||||
loadExtensions,
|
loadExtensions,
|
||||||
initCoreStore,
|
initCoreStore,
|
||||||
|
loadGroups,
|
||||||
} = require('./core');
|
} = require('./core');
|
||||||
const initializeMiddlewares = require('./middlewares');
|
const initializeMiddlewares = require('./middlewares');
|
||||||
const initializeHooks = require('./hooks');
|
const initializeHooks = require('./hooks');
|
||||||
@ -100,8 +101,6 @@ class Strapi extends EventEmitter {
|
|||||||
installedHooks: getPrefixedDeps('strapi-hook', pkgJSON),
|
installedHooks: getPrefixedDeps('strapi-hook', pkgJSON),
|
||||||
};
|
};
|
||||||
|
|
||||||
this.groupManager = new UniqueWritetMap('Group');
|
|
||||||
this.serviceManager = new UniqueWritetMap('Service');
|
|
||||||
this.fs = createStrapiFs(this);
|
this.fs = createStrapiFs(this);
|
||||||
this.requireProjectBootstrap();
|
this.requireProjectBootstrap();
|
||||||
}
|
}
|
||||||
@ -247,6 +246,7 @@ class Strapi extends EventEmitter {
|
|||||||
middlewares,
|
middlewares,
|
||||||
hook,
|
hook,
|
||||||
extensions,
|
extensions,
|
||||||
|
groups,
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
loadConfig(this),
|
loadConfig(this),
|
||||||
loadApis(this),
|
loadApis(this),
|
||||||
@ -255,12 +255,14 @@ class Strapi extends EventEmitter {
|
|||||||
loadMiddlewares(this),
|
loadMiddlewares(this),
|
||||||
loadHooks(this.config),
|
loadHooks(this.config),
|
||||||
loadExtensions(this.config),
|
loadExtensions(this.config),
|
||||||
|
loadGroups(this),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
_.merge(this.config, config);
|
_.merge(this.config, config);
|
||||||
|
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.admin = admin;
|
this.admin = admin;
|
||||||
|
this.groups = groups;
|
||||||
this.plugins = plugins;
|
this.plugins = plugins;
|
||||||
this.middleware = middlewares;
|
this.middleware = middlewares;
|
||||||
this.hook = hook;
|
this.hook = hook;
|
||||||
@ -284,10 +286,6 @@ class Strapi extends EventEmitter {
|
|||||||
|
|
||||||
await bootstrap(this);
|
await bootstrap(this);
|
||||||
|
|
||||||
// init service manager
|
|
||||||
this.initServices();
|
|
||||||
this.initGroups();
|
|
||||||
|
|
||||||
// Usage.
|
// Usage.
|
||||||
await utils.usage(this.config);
|
await utils.usage(this.config);
|
||||||
|
|
||||||
@ -455,56 +453,6 @@ class Strapi extends EventEmitter {
|
|||||||
associations: model.associations,
|
associations: model.associations,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
initServices() {
|
|
||||||
this.serviceManager.clear();
|
|
||||||
|
|
||||||
Object.entries(this.plugins).forEach(([pluginKey, plugin]) => {
|
|
||||||
if (!plugin.services) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.entries(plugin.services).forEach(([serviceKey, service]) => {
|
|
||||||
this.serviceManager.set(`${pluginKey}.${serviceKey}`, service);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
if (this.admin.services) {
|
|
||||||
Object.entries(this.admin.services).forEach(([serviceKey, service]) => {
|
|
||||||
this.serviceManager.set(`admin.${serviceKey}`, service);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.entries(this.api).forEach(([apiKey, api]) => {
|
|
||||||
if (!api.services) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.entries(api.services).forEach(([serviceKey, service]) => {
|
|
||||||
this.serviceManager.set(`${apiKey}.${serviceKey}`, service);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
service(key) {
|
|
||||||
return this.serviceManager.get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
initGroups() {
|
|
||||||
this.groupManager.clear();
|
|
||||||
|
|
||||||
Object.entries(this.api).forEach(([apiKey, api]) => {
|
|
||||||
if (!api.groups) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.entries(api.groups).forEach(([groupKey, group]) => {
|
|
||||||
this.groupManager.set(`${apiKey}.${groupKey}`, group);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = options => {
|
module.exports = options => {
|
||||||
@ -512,31 +460,3 @@ module.exports = options => {
|
|||||||
global.strapi = strapi;
|
global.strapi = strapi;
|
||||||
return strapi;
|
return strapi;
|
||||||
};
|
};
|
||||||
|
|
||||||
class UniqueWritetMap extends Map {
|
|
||||||
constructor(mapName) {
|
|
||||||
super();
|
|
||||||
this.mapName = mapName;
|
|
||||||
}
|
|
||||||
|
|
||||||
get(key) {
|
|
||||||
if (!this.has(key)) {
|
|
||||||
throw new Error(`${this.mapName} ${key} not found`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return super.get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
set(key, value) {
|
|
||||||
if (this.has(key)) {
|
|
||||||
throw new Error(
|
|
||||||
`${
|
|
||||||
this.mapName
|
|
||||||
} ${key} already exists. Make sure you don't have conflicts with your installed plugins`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
super.set(key, value);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
10
packages/strapi/lib/core/bootstrap.js
vendored
10
packages/strapi/lib/core/bootstrap.js
vendored
@ -41,6 +41,16 @@ module.exports = function(strapi) {
|
|||||||
// Initialize main router to use it in middlewares.
|
// Initialize main router to use it in middlewares.
|
||||||
strapi.router = routerJoi();
|
strapi.router = routerJoi();
|
||||||
|
|
||||||
|
Object.keys(strapi.groups).forEach(key => {
|
||||||
|
const group = strapi.groups[key];
|
||||||
|
return Object.assign(group, {
|
||||||
|
globalId: group.globalId || _.upperFirst(_.camelCase(`group_${key}`)),
|
||||||
|
collectionName:
|
||||||
|
group.collectionName || `group_${key}`.toLocaleLowerCase(),
|
||||||
|
connection: group.connection || defaultConnection,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Set models.
|
// Set models.
|
||||||
strapi.models = Object.keys(strapi.api || []).reduce((acc, key) => {
|
strapi.models = Object.keys(strapi.api || []).reduce((acc, key) => {
|
||||||
for (let index in strapi.api[key].models) {
|
for (let index in strapi.api[key].models) {
|
||||||
|
|||||||
@ -9,6 +9,7 @@ const loadExtensions = require('./load-extensions');
|
|||||||
const loadHooks = require('./load-hooks');
|
const loadHooks = require('./load-hooks');
|
||||||
const bootstrap = require('./bootstrap');
|
const bootstrap = require('./bootstrap');
|
||||||
const initCoreStore = require('./init-core-store');
|
const initCoreStore = require('./init-core-store');
|
||||||
|
const loadGroups = require('./load-groups');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
loadConfig,
|
loadConfig,
|
||||||
@ -18,6 +19,7 @@ module.exports = {
|
|||||||
loadMiddlewares,
|
loadMiddlewares,
|
||||||
loadHooks,
|
loadHooks,
|
||||||
loadExtensions,
|
loadExtensions,
|
||||||
|
loadGroups,
|
||||||
bootstrap,
|
bootstrap,
|
||||||
initCoreStore,
|
initCoreStore,
|
||||||
};
|
};
|
||||||
|
|||||||
15
packages/strapi/lib/core/load-groups.js
Normal file
15
packages/strapi/lib/core/load-groups.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { join } = require('path');
|
||||||
|
const { exists } = require('fs-extra');
|
||||||
|
const loadFiles = require('../load/load-files');
|
||||||
|
|
||||||
|
module.exports = async ({ dir }) => {
|
||||||
|
const groupsDir = join(dir, 'groups');
|
||||||
|
|
||||||
|
if (!(await exists(groupsDir))) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
return await loadFiles(groupsDir, '*.*(js|json)');
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user