159 lines
4.1 KiB
Plaintext
Raw Normal View History

2016-03-18 11:12:50 +01:00
'use strict';
2016-03-25 22:22:34 +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
// Strapi utilities.
const utils = require('strapi-bookshelf/lib/utils/');
2016-03-18 11:12:50 +01:00
module.exports = {
/**
* Promise to fetch all <%= idPluralized %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
fetchAll: (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);
2017-09-13 12:18:54 +02:00
return <%= globalID %>.query(function(qb) {
_.forEach(filters.where, (where, key) => {
2018-04-03 12:38:33 +02:00
if (_.isArray(where.value)) {
for (const value in where.value) {
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value])
}
} else {
qb.where(key, where.symbol, where.value);
}
2017-09-13 12:18:54 +02:00
});
if (filters.sort) {
qb.orderBy(filters.sort.key, filters.sort.order);
}
2017-09-13 12:18:54 +02:00
qb.offset(filters.start);
qb.limit(filters.limit);
2017-09-13 12:18:54 +02:00
}).fetchAll({
withRelated: populate
2016-03-18 11:12:50 +01:00
});
},
/**
* Promise to fetch a/an <%= id %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
fetch: (params) => {
// Select field to populate.
const populate = <%= globalID %>.associations
.filter(ast => ast.autoPopulate !== false)
.map(ast => ast.alias);
return <%= globalID %>.forge(_.pick(params, 'id')).fetch({
withRelated: populate
2016-03-18 11:12:50 +01: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) {
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value])
}
} else {
qb.where(key, where.symbol, where.value);
}
});
}).count();
},
2016-03-18 11:12:50 +01:00
/**
* Promise to add a/an <%= id %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
add: async (values) => {
// 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
},
/**
* Promise to edit a/an <%= id %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
edit: async (params, values) => {
// 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 = <%= globalID %>.forge(params).save(data, { path: true });
// Create relational data and return the entry.
return <%= globalID %>.updateRelations(Object.assign(params, { values: relations }));
2016-03-18 11:12:50 +01:00
},
/**
* Promise to remove a/an <%= id %>.
2016-03-18 11:12:50 +01:00
*
* @return {Promise}
*/
remove: async (params) => {
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);
return <%= globalID %>.forge(params).destroy();
2016-03-18 11:12:50 +01:00
}
};