2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
2018-09-14 00:22:57 +02:00
|
|
|
/* global <%= globalID %> */
|
2016-03-18 11:12:50 +01:00
|
|
|
|
2016-03-25 22:22:34 +01:00
|
|
|
/**
|
2017-01-11 17:53:40 +01:00
|
|
|
* <%= filename %> service
|
|
|
|
*
|
|
|
|
* @description: A set of functions similar to controller's actions to avoid code duplication.
|
2016-03-25 22:22:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Public dependencies.
|
2016-03-18 11:12:50 +01:00
|
|
|
const _ = require('lodash');
|
2016-03-25 22:22:34 +01:00
|
|
|
|
2016-03-18 11:12:50 +01:00
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to fetch all <%= idPluralized %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-01-11 17:53:40 +01:00
|
|
|
fetchAll: (params) => {
|
2018-05-09 16:08:58 +02:00
|
|
|
// Convert `params` object to filters compatible with Bookshelf.
|
|
|
|
const filters = strapi.utils.models.convertParams('<%= globalID.toLowerCase() %>', params);
|
|
|
|
// Select field to populate.
|
|
|
|
const populate = <%= globalID %>.associations
|
|
|
|
.filter(ast => ast.autoPopulate !== false)
|
|
|
|
.map(ast => ast.alias);
|
2017-09-13 12:18:54 +02:00
|
|
|
|
|
|
|
return <%= globalID %>.query(function(qb) {
|
2018-05-09 16:08:58 +02:00
|
|
|
_.forEach(filters.where, (where, key) => {
|
2018-10-17 11:50:43 -05:00
|
|
|
if (_.isArray(where.value) && where.symbol !== 'IN') {
|
2018-04-03 12:38:33 +02:00
|
|
|
for (const value in where.value) {
|
2018-09-14 00:22:57 +02:00
|
|
|
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value]);
|
2018-04-03 12:38:33 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qb.where(key, where.symbol, where.value);
|
|
|
|
}
|
2017-09-13 12:18:54 +02:00
|
|
|
});
|
|
|
|
|
2018-09-14 00:22:57 +02:00
|
|
|
Object.keys(filters.relations).forEach(
|
|
|
|
(relationName) => {
|
|
|
|
const ast = <%= globalID.toLowerCase() %>.associations.find(a => a.alias === relationName);
|
|
|
|
if (ast) {
|
|
|
|
const model = ast.plugin ?
|
2018-09-22 21:45:22 +02:00
|
|
|
strapi.plugins[ast.plugin].models[ast.model || ast.collection] :
|
|
|
|
strapi.models[ast.model || ast.collection];
|
2018-09-14 00:22:57 +02:00
|
|
|
|
|
|
|
qb.distinct();
|
|
|
|
|
|
|
|
if (ast.tableCollectionName) {
|
|
|
|
qb.innerJoin(
|
|
|
|
ast.tableCollectionName,
|
|
|
|
`${ast.tableCollectionName}.${<%= globalID.toLowerCase() %>.info.name}_${<%= globalID.toLowerCase() %>.primaryKey}`,
|
|
|
|
`${<%= globalID.toLowerCase() %>.collectionName}.${<%= globalID.toLowerCase() %>.primaryKey}`,
|
|
|
|
);
|
|
|
|
qb.innerJoin(
|
|
|
|
`${relationName}`,
|
|
|
|
`${relationName}.${<%= globalID.toLowerCase() %>.attributes[relationName].column}`,
|
|
|
|
`${ast.tableCollectionName}.${<%= globalID.toLowerCase() %>.attributes[relationName].attribute}_${<%= globalID.toLowerCase() %>.attributes[relationName].column}`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const relationTable = model.collectionName;
|
|
|
|
const externalKey = ast.type === 'collection' ?
|
|
|
|
`${model.collectionName}.${ast.via}` :
|
|
|
|
`${model.collectionName}.${model.primaryKey}`;
|
2018-09-22 21:45:22 +02:00
|
|
|
const internalKey = !ast.dominant
|
|
|
|
? `${<%= globalID.toLowerCase() %>.collectionName}.${<%= globalID.toLowerCase() %>.primaryKey}`
|
|
|
|
: ast.via === <%= globalID.toLowerCase() %>.collectionName
|
|
|
|
? `${<%= globalID.toLowerCase() %>.collectionName}.${ast.alias}`
|
|
|
|
: `${<%= globalID.toLowerCase() %>.collectionName}.${<%= globalID.toLowerCase() %>.primaryKey}`;
|
2018-09-14 00:22:57 +02:00
|
|
|
|
|
|
|
qb.innerJoin(relationTable, externalKey, internalKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
const relation = filters.relations[relationName];
|
|
|
|
Object.keys(relation).forEach(
|
|
|
|
(filter) => {
|
|
|
|
qb.where(`${model.collectionName}.${filter}`, `${relation[filter].symbol}`, `${relation[filter].value}`);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-10-08 22:46:43 +02:00
|
|
|
if (_.has(filters, 'start')) qb.offset(filters.start);
|
|
|
|
if (_.has(filters, 'limit')) qb.limit(filters.limit);
|
|
|
|
if (_.has(filters, 'sort')) qb.orderBy(filters.sort.key, filters.sort.order);
|
2018-09-22 21:45:22 +02:00
|
|
|
|
2017-09-13 12:18:54 +02:00
|
|
|
}).fetchAll({
|
2018-05-09 16:08:58 +02:00
|
|
|
withRelated: populate
|
2016-03-18 11:12:50 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to fetch a/an <%= id %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-01-11 17:53:40 +01:00
|
|
|
fetch: (params) => {
|
2018-05-09 16:08:58 +02:00
|
|
|
// Select field to populate.
|
|
|
|
const populate = <%= globalID %>.associations
|
|
|
|
.filter(ast => ast.autoPopulate !== false)
|
|
|
|
.map(ast => ast.alias);
|
|
|
|
|
2017-01-11 17:53:40 +01:00
|
|
|
return <%= globalID %>.forge(_.pick(params, 'id')).fetch({
|
2018-05-09 16:08:58 +02:00
|
|
|
withRelated: populate
|
2016-03-18 11:12:50 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-06-01 08:10:25 +03:00
|
|
|
/**
|
|
|
|
* Promise to count a/an <%= id %>.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
|
|
|
count: (params) => {
|
|
|
|
// Convert `params` object to filters compatible with Bookshelf.
|
|
|
|
const filters = strapi.utils.models.convertParams('<%= globalID.toLowerCase() %>', params);
|
|
|
|
|
|
|
|
return <%= globalID %>.query(function(qb) {
|
|
|
|
_.forEach(filters.where, (where, key) => {
|
|
|
|
if (_.isArray(where.value)) {
|
|
|
|
for (const value in where.value) {
|
2018-09-14 00:22:57 +02:00
|
|
|
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value]);
|
2018-06-01 08:10:25 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qb.where(key, where.symbol, where.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).count();
|
|
|
|
},
|
|
|
|
|
2016-03-18 11:12:50 +01:00
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to add a/an <%= id %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-10-30 14:02:54 +01:00
|
|
|
add: async (values) => {
|
2018-05-09 16:08:58 +02:00
|
|
|
// Extract values related to relational data.
|
|
|
|
const relations = _.pick(values, <%= globalID %>.associations.map(ast => ast.alias));
|
|
|
|
const data = _.omit(values, <%= globalID %>.associations.map(ast => ast.alias));
|
|
|
|
|
|
|
|
// Create entry with no-relational data.
|
|
|
|
const entry = await <%= globalID %>.forge(data).save();
|
|
|
|
|
|
|
|
// Create relational data and return the entry.
|
|
|
|
return <%= globalID %>.updateRelations({ id: entry.id , values: relations });
|
2016-03-18 11:12:50 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to edit a/an <%= id %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-10-26 17:21:23 +02:00
|
|
|
edit: async (params, values) => {
|
2018-05-09 16:08:58 +02:00
|
|
|
// Extract values related to relational data.
|
|
|
|
const relations = _.pick(values, <%= globalID %>.associations.map(ast => ast.alias));
|
|
|
|
const data = _.omit(values, <%= globalID %>.associations.map(ast => ast.alias));
|
|
|
|
|
|
|
|
// Create entry with no-relational data.
|
2018-10-04 17:17:25 +02:00
|
|
|
const entry = <%= globalID %>.forge(params).save(data);
|
2018-05-09 16:08:58 +02:00
|
|
|
|
|
|
|
// Create relational data and return the entry.
|
|
|
|
return <%= globalID %>.updateRelations(Object.assign(params, { values: relations }));
|
2016-03-18 11:12:50 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to remove a/an <%= id %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2018-05-09 16:08:58 +02:00
|
|
|
remove: async (params) => {
|
2018-06-21 19:42:03 +02:00
|
|
|
params.values = {};
|
|
|
|
<%= globalID %>.associations.map(association => {
|
|
|
|
switch (association.nature) {
|
|
|
|
case 'oneWay':
|
|
|
|
case 'oneToOne':
|
|
|
|
case 'manyToOne':
|
|
|
|
case 'oneToManyMorph':
|
|
|
|
params.values[association.alias] = null;
|
|
|
|
break;
|
|
|
|
case 'oneToMany':
|
|
|
|
case 'manyToMany':
|
|
|
|
case 'manyToManyMorph':
|
|
|
|
params.values[association.alias] = [];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await <%= globalID %>.updateRelations(params);
|
2018-05-09 16:08:58 +02:00
|
|
|
|
2017-01-11 17:53:40 +01:00
|
|
|
return <%= globalID %>.forge(params).destroy();
|
2018-09-14 00:22:57 +02:00
|
|
|
},
|
2018-07-05 11:28:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Promise to search a/an <%= id %>.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
|
|
|
search: async (params) => {
|
|
|
|
// Convert `params` object to filters compatible with Bookshelf.
|
|
|
|
const filters = strapi.utils.models.convertParams('<%= globalID.toLowerCase() %>', params);
|
|
|
|
// Select field to populate.
|
|
|
|
const populate = <%= globalID %>.associations
|
|
|
|
.filter(ast => ast.autoPopulate !== false)
|
|
|
|
.map(ast => ast.alias);
|
|
|
|
|
|
|
|
const associations = <%= globalID %>.associations.map(x => x.alias);
|
|
|
|
const searchText = Object.keys(<%= globalID %>._attributes)
|
|
|
|
.filter(attribute => attribute !== <%= globalID %>.primaryKey && !associations.includes(attribute))
|
|
|
|
.filter(attribute => ['string', 'text'].includes(<%= globalID %>._attributes[attribute].type));
|
|
|
|
|
|
|
|
const searchNoText = Object.keys(<%= globalID %>._attributes)
|
|
|
|
.filter(attribute => attribute !== <%= globalID %>.primaryKey && !associations.includes(attribute))
|
|
|
|
.filter(attribute => !['string', 'text', 'boolean', 'integer', 'decimal', 'float'].includes(<%= globalID %>._attributes[attribute].type));
|
|
|
|
|
|
|
|
const searchInt = Object.keys(<%= globalID %>._attributes)
|
|
|
|
.filter(attribute => attribute !== <%= globalID %>.primaryKey && !associations.includes(attribute))
|
|
|
|
.filter(attribute => ['integer', 'decimal', 'float'].includes(<%= globalID %>._attributes[attribute].type));
|
|
|
|
|
|
|
|
const searchBool = Object.keys(<%= globalID %>._attributes)
|
|
|
|
.filter(attribute => attribute !== <%= globalID %>.primaryKey && !associations.includes(attribute))
|
|
|
|
.filter(attribute => ['boolean'].includes(<%= globalID %>._attributes[attribute].type));
|
|
|
|
|
|
|
|
const query = (params._q || '').replace(/[^a-zA-Z0-9.-\s]+/g, '');
|
|
|
|
|
|
|
|
return <%= globalID %>.query(qb => {
|
|
|
|
// Search in columns which are not text value.
|
|
|
|
searchNoText.forEach(attribute => {
|
|
|
|
qb.orWhereRaw(`LOWER(${attribute}) LIKE '%${_.toLower(query)}%'`);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!_.isNaN(_.toNumber(query))) {
|
|
|
|
searchInt.forEach(attribute => {
|
|
|
|
qb.orWhereRaw(`${attribute} = ${_.toNumber(query)}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query === 'true' || query === 'false') {
|
|
|
|
searchBool.forEach(attribute => {
|
|
|
|
qb.orWhereRaw(`${attribute} = ${_.toNumber(query === 'true')}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search in columns with text using index.
|
|
|
|
switch (<%= globalID %>.client) {
|
|
|
|
case 'pg': {
|
|
|
|
const searchQuery = searchText.map(attribute =>
|
|
|
|
_.toLower(attribute) === attribute
|
|
|
|
? `to_tsvector(${attribute})`
|
|
|
|
: `to_tsvector('${attribute}')`
|
|
|
|
);
|
|
|
|
|
|
|
|
qb.orWhereRaw(`${searchQuery.join(' || ')} @@ to_tsquery(?)`, query);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
qb.orWhereRaw(`MATCH(${searchText.join(',')}) AGAINST(? IN BOOLEAN MODE)`, `*${query}*`);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filters.sort) {
|
|
|
|
qb.orderBy(filters.sort.key, filters.sort.order);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filters.skip) {
|
|
|
|
qb.offset(_.toNumber(filters.skip));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filters.limit) {
|
|
|
|
qb.limit(_.toNumber(filters.limit));
|
|
|
|
}
|
|
|
|
}).fetchAll({
|
|
|
|
width: populate
|
|
|
|
});
|
2016-03-18 11:12:50 +01:00
|
|
|
}
|
|
|
|
};
|