2017-09-07 19:11:12 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
module.exports = {
|
2017-08-29 19:34:34 +02:00
|
|
|
find: async function (params) {
|
|
|
|
return this
|
2017-06-17 17:01:50 +02:00
|
|
|
.find()
|
|
|
|
.limit(Number(params.limit))
|
|
|
|
.sort(params.sort)
|
|
|
|
.skip(Number(params.skip));
|
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
count: async function (params) {
|
|
|
|
return Number(await this
|
|
|
|
.count());
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
findOne: async function (params) {
|
2017-09-07 19:11:12 +02:00
|
|
|
return this
|
2017-08-29 19:34:34 +02:00
|
|
|
.findOne({
|
|
|
|
[this.primaryKey]: params.id
|
2017-09-07 17:16:31 +02:00
|
|
|
})
|
|
|
|
.populate(this.associations.map(x => x.alias).join(' '));
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
create: async function (params) {
|
|
|
|
return await this
|
2017-06-17 17:01:50 +02:00
|
|
|
.create(params.values);
|
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
update: async function (params) {
|
2017-09-07 19:11:12 +02:00
|
|
|
const virtualFields = [];
|
|
|
|
|
|
|
|
const record = await module.exports.findOne.call(this, params);
|
|
|
|
const response = record ? record.toJSON() : {};
|
|
|
|
|
|
|
|
// Only update fields which are on this document.
|
|
|
|
const values = Object.keys(params.values).reduce((acc, current) => {
|
|
|
|
if (!this.schema.virtuals.hasOwnProperty(current)) {
|
|
|
|
acc[current] = params.values[current];
|
|
|
|
} else if (response.hasOwnProperty(current) && current !== 'id'){
|
|
|
|
const details = this.attributes[current];
|
|
|
|
|
|
|
|
const toAdd = _.differenceWith(params.values[current], response[current], _.isEqual);
|
|
|
|
const toRemove = _.differenceWith(response[current], params.values[current], _.isEqual);
|
|
|
|
|
|
|
|
toAdd.forEach(value => {
|
|
|
|
value[details.via] = params.values[this.primaryKey];
|
|
|
|
|
|
|
|
virtualFields.push(strapi.query(details.model || details.collection).update({
|
|
|
|
id: value.id || value[this.primaryKey] || '_id',
|
|
|
|
values: value
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
toRemove.forEach(value => {
|
|
|
|
value[details.via] = null;
|
|
|
|
|
|
|
|
virtualFields.push(strapi.query(details.model || details.collection).update({
|
|
|
|
id: value.id || value[this.primaryKey] || '_id',
|
|
|
|
values: value
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
// Add current model to the flow of updates.
|
|
|
|
virtualFields.push(this
|
2017-08-29 19:34:34 +02:00
|
|
|
.update({
|
|
|
|
[this.primaryKey]: params.id
|
2017-09-07 19:11:12 +02:00
|
|
|
}, values, {
|
2017-09-07 17:16:31 +02:00
|
|
|
strict: false
|
2017-09-07 19:11:12 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
// Update virtuals fields.
|
|
|
|
return await Promise.all(virtualFields);
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
delete: async function (params) {
|
|
|
|
return await this
|
2017-08-31 17:26:44 +02:00
|
|
|
.remove({
|
2017-08-29 19:34:34 +02:00
|
|
|
[this.primaryKey]: params.id
|
|
|
|
});
|
2017-06-17 17:01:50 +02:00
|
|
|
}
|
2017-08-29 19:34:34 +02:00
|
|
|
};
|