2017-09-18 18:20:33 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2018-03-01 17:39:31 +01:00
|
|
|
module.exports = {
|
2018-04-03 11:30:39 +02:00
|
|
|
find: async function (params, populate) {
|
2017-12-07 15:44:20 +01:00
|
|
|
return this.query(function(qb) {
|
|
|
|
_.forEach(params.where, (where, key) => {
|
2018-04-03 13:03:41 +02:00
|
|
|
if (_.isArray(where.value)) {
|
|
|
|
for (const value in where.value) {
|
2018-04-30 16:32:48 +02:00
|
|
|
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value]);
|
2018-04-03 13:03:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qb.where(key, where.symbol, where.value);
|
|
|
|
}
|
2017-06-18 17:23:58 +02:00
|
|
|
});
|
2017-12-07 15:44:20 +01:00
|
|
|
|
|
|
|
if (params.sort) {
|
|
|
|
qb.orderBy(params.sort);
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:54:17 +01:00
|
|
|
if (params.skip) {
|
|
|
|
qb.offset(_.toNumber(params.skip));
|
|
|
|
}
|
2017-12-07 15:44:20 +01:00
|
|
|
|
2018-01-23 18:54:17 +01:00
|
|
|
if (params.limit) {
|
|
|
|
qb.limit(_.toNumber(params.limit));
|
|
|
|
}
|
2017-12-07 15:44:20 +01:00
|
|
|
}).fetchAll({
|
2018-04-03 11:30:39 +02:00
|
|
|
withRelated: populate || this.associations.map(x => x.alias)
|
2017-12-07 15:44:20 +01:00
|
|
|
});
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2018-04-30 16:32:48 +02:00
|
|
|
count: async function () {
|
2017-09-14 16:33:56 +02:00
|
|
|
return await this
|
2017-06-17 17:01:50 +02:00
|
|
|
.forge()
|
|
|
|
.count();
|
|
|
|
},
|
|
|
|
|
2018-04-30 16:32:48 +02:00
|
|
|
findOne: async function (params, populate) {
|
2017-09-14 18:47:10 +02:00
|
|
|
const record = await this
|
2017-09-14 16:33:56 +02:00
|
|
|
.forge({
|
|
|
|
[this.primaryKey]: params[this.primaryKey]
|
|
|
|
})
|
2017-09-14 18:47:10 +02:00
|
|
|
.fetch({
|
2018-04-11 12:53:07 +02:00
|
|
|
withRelated: populate || this.associations.map(x => x.alias)
|
2017-09-14 18:47:10 +02:00
|
|
|
});
|
|
|
|
|
2018-04-11 12:53:07 +02:00
|
|
|
const data = record ? record.toJSON() : record;
|
|
|
|
|
|
|
|
// Retrieve data manually.
|
|
|
|
if (_.isEmpty(populate)) {
|
|
|
|
const arrayOfPromises = this.associations
|
|
|
|
.filter(association => ['manyMorphToOne', 'manyMorphToMany'].includes(association.nature))
|
2018-04-30 16:32:48 +02:00
|
|
|
.map(association => { // eslint-disable-line no-unused-vars
|
2018-04-11 12:53:07 +02:00
|
|
|
return this.morph.forge()
|
|
|
|
.where({
|
|
|
|
[`${this.collectionName}_id`]: params[this.primaryKey]
|
|
|
|
})
|
2018-04-30 16:32:48 +02:00
|
|
|
.fetchAll();
|
2018-04-11 12:53:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const related = await Promise.all(arrayOfPromises);
|
|
|
|
|
|
|
|
related.forEach((value, index) => {
|
|
|
|
data[this.associations[index].alias] = value ? value.toJSON() : value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-09-14 16:33:56 +02:00
|
|
|
create: async function (params) {
|
2018-03-01 17:39:31 +01:00
|
|
|
// Exclude relationships.
|
|
|
|
const values = Object.keys(params.values).reduce((acc, current) => {
|
|
|
|
if (this._attributes[current] && this._attributes[current].type) {
|
2017-09-19 11:17:04 +02:00
|
|
|
acc[current] = params.values[current];
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
2018-03-01 17:39:31 +01:00
|
|
|
}, {});
|
|
|
|
|
2018-04-26 14:10:17 +02:00
|
|
|
const request = await this
|
2018-03-01 17:39:31 +01:00
|
|
|
.forge(values)
|
2018-01-23 18:54:17 +01:00
|
|
|
.save()
|
2018-03-01 17:39:31 +01:00
|
|
|
.catch((err) => {
|
2018-04-30 16:32:48 +02:00
|
|
|
if (err.detail) {
|
2018-03-01 17:39:31 +01:00
|
|
|
const field = _.last(_.words(err.detail.split('=')[0]));
|
|
|
|
err = { message: `This ${field} is already taken`, field };
|
|
|
|
}
|
2017-12-06 15:58:20 +01:00
|
|
|
|
2018-03-01 17:39:31 +01:00
|
|
|
throw err;
|
|
|
|
});
|
2017-09-19 11:17:04 +02:00
|
|
|
|
2018-04-26 14:10:17 +02:00
|
|
|
const entry = request.toJSON ? request.toJSON() : request;
|
|
|
|
|
2018-05-02 15:39:12 +02:00
|
|
|
const relations = this.associations.reduce((acc, association) => {
|
|
|
|
acc[association.alias] = params.values[association.alias];
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
2017-09-19 11:17:04 +02:00
|
|
|
return module.exports.update.call(this, {
|
|
|
|
[this.primaryKey]: entry[this.primaryKey],
|
2018-04-25 12:23:53 +02:00
|
|
|
values: _.assign({
|
2017-09-19 11:17:04 +02:00
|
|
|
id: entry[this.primaryKey]
|
2018-05-02 15:39:12 +02:00
|
|
|
}, relations)
|
2017-09-19 11:17:04 +02:00
|
|
|
});
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-09-14 16:33:56 +02:00
|
|
|
update: async function (params) {
|
2018-05-09 16:57:16 +02:00
|
|
|
// Call the business logic located in the hook.
|
|
|
|
// This function updates no-relational and relational data.
|
|
|
|
return this.updateRelations(params);
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-09-14 16:33:56 +02:00
|
|
|
delete: async function (params) {
|
2017-09-19 11:17:04 +02:00
|
|
|
return await this
|
2017-09-14 16:33:56 +02:00
|
|
|
.forge({
|
2017-09-20 18:15:06 +02:00
|
|
|
[this.primaryKey]: params.id
|
2017-09-14 16:33:56 +02:00
|
|
|
})
|
2017-06-17 17:01:50 +02:00
|
|
|
.destroy();
|
|
|
|
}
|
2017-08-29 19:34:34 +02:00
|
|
|
};
|