2017-10-11 17:30:08 +02:00

79 lines
1.8 KiB
Plaintext
Executable File

'use strict';
/**
* <%= filename %> service
*
* @description: A set of functions similar to controller's actions to avoid code duplication.
*/
// Public dependencies.
const _ = require('lodash');
module.exports = {
/**
* Promise to fetch all <%= humanizeIdPluralized %>.
*
* @return {Promise}
*/
fetchAll: (params) => {
const convertedParams = strapi.utils.models.convertParams('<%= globalID.toLowerCase() %>', params);
return <%= globalID %>
.find()
.where(convertedParams.where)
.sort(convertedParams.sort)
.skip(convertedParams.start)
.limit(convertedParams.limit)
.populate(_.keys(_.pickBy(strapi.models.<%= humanizeId %>.attributes, { autoPopulate: true })).join(' '));
},
/**
* Promise to fetch a/an <%= id %>.
*
* @return {Promise}
*/
fetch: (params) => {
return <%= globalID %>
.findOne(params)
.populate(_.keys(_.pickBy(strapi.models.<%= humanizeId %>.attributes, { autoPopulate: true })).join(' '));
},
/**
* Promise to add a/an <%= id %>.
*
* @return {Promise}
*/
add: (values) => {
return <%= globalID %>.create(values);
},
/**
* Promise to edit a/an <%= id %>.
*
* @return {Promise}
*/
edit: (params, values) => {
// Note: The current method will return the full response of Mongo.
// To get the updated object, you have to execute the `findOne()` method
// or use the `findOneOrUpdate()` method with `{ new:true }` option.
return <%= globalID %>.update(params, values, { multi: true });
},
/**
* Promise to remove a/an <%= id %>.
*
* @return {Promise}
*/
remove: params => {
// Note: To get the full response of Mongo, use the `remove()` method
// or add spent the parameter `{ passRawResult: true }` as second argument.
return <%= globalID %>.findOneAndRemove(params, {});
}
};