2016-08-09 11:47:48 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public dependencies.
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A set of functions called "actions" for `<%= globalID %>`
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
/**
|
2016-08-12 12:04:00 +02:00
|
|
|
* Promise to fetch all <%= humanizeIdPluralized %>.
|
2016-08-09 11:47:48 +02:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
|
|
|
fetchAll: params => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2016-08-12 12:04:00 +02:00
|
|
|
<%= globalID %>.find(params).populate(_.keys(_.pickBy(strapi.models.<%= humanizeId %>.attributes, { autoPopulate: true })).join(' '))
|
|
|
|
.exec((err, <%= idPluralized %>) => {
|
2016-08-09 11:47:48 +02:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
2016-08-12 12:04:00 +02:00
|
|
|
resolve(<%= idPluralized %>);
|
2016-08-09 11:47:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Promise to fetch a/an <%= id %>.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
|
|
|
fetch: params => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2016-08-12 12:04:00 +02:00
|
|
|
<%= globalID %>.findOne(params).populate(_.keys(_.pickBy(strapi.models.<%= humanizeId %>.attributes, { autoPopulate: true })).join(' '))
|
|
|
|
.exec((err, <%= humanizeId %>) => {
|
2016-08-09 11:47:48 +02:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
2016-08-12 12:04:00 +02:00
|
|
|
resolve(<%= humanizeId %>);
|
2016-08-09 11:47:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Promise to add a/an <%= id %>.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
|
|
|
add: values => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2016-08-12 12:04:00 +02:00
|
|
|
const <%= humanizeId %> = new <%= globalID %>(values);
|
2016-08-09 11:47:48 +02:00
|
|
|
|
2016-08-12 12:04:00 +02:00
|
|
|
<%= humanizeId %>.save((err, <%= humanizeId %>) => {
|
2016-08-09 11:47:48 +02:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(<%= id %>);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Promise to edit a/an <%= id %>.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
|
|
|
edit: (params, values) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
<%= globalID %>.update(params, values, { multi: true }, (err, raw) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
2016-08-12 12:04:00 +02:00
|
|
|
|
2016-08-09 11:47:48 +02:00
|
|
|
// NB: Raw contains 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.
|
|
|
|
|
|
|
|
resolve(raw);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Promise to remove a/an <%= id %>.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
|
|
|
remove: params => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2016-08-12 12:04:00 +02:00
|
|
|
<%= globalID %>.findOneAndRemove(params, {}, (err, <%= humanizeId %>) => {
|
2016-08-09 11:47:48 +02:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: To get the full response of Mongo, use the `remove()` method
|
|
|
|
// or add spent the parameter `{ passRawResult: true }` as second argument.
|
|
|
|
|
2016-08-12 12:04:00 +02:00
|
|
|
resolve(<%= humanizeId %>);
|
2016-08-09 11:47:48 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|