2017-09-07 19:11:12 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
module.exports = {
|
2018-06-05 18:35:19 +02:00
|
|
|
find: async function (params, populate, raw = false) {
|
|
|
|
const query = this
|
2018-04-02 19:24:36 +02:00
|
|
|
.find(params.where)
|
2017-06-17 17:01:50 +02:00
|
|
|
.limit(Number(params.limit))
|
|
|
|
.sort(params.sort)
|
2018-04-03 11:30:39 +02:00
|
|
|
.skip(Number(params.skip))
|
|
|
|
.populate(populate || this.associations.map(x => x.alias).join(' '));
|
2018-06-05 18:35:19 +02:00
|
|
|
|
|
|
|
return raw ? query.lean() : query;
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2018-05-22 17:11:35 +02:00
|
|
|
count: async function (params) {
|
2017-08-29 19:34:34 +02:00
|
|
|
return Number(await this
|
2018-05-22 17:11:35 +02:00
|
|
|
.where(params.where)
|
2017-08-29 19:34:34 +02:00
|
|
|
.count());
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2018-06-07 15:08:11 +02:00
|
|
|
search: async function (params, populate) { // eslint-disable-line no-unused-vars
|
2018-06-07 18:50:43 +02:00
|
|
|
const $or = Object.keys(this.attributes).reduce((acc, curr) => {
|
|
|
|
switch (this.attributes[curr].type) {
|
|
|
|
case 'integer':
|
|
|
|
case 'float':
|
|
|
|
case 'decimal':
|
|
|
|
if (!_.isNaN(_.toNumber(params.search))) {
|
|
|
|
return acc.concat({ [curr]: params.search });
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
case 'string':
|
|
|
|
case 'text':
|
|
|
|
case 'password':
|
|
|
|
return acc.concat({ [curr]: { $regex: params.search, $options: 'i' } });
|
|
|
|
case 'boolean':
|
|
|
|
if (params.search === 'true' || params.search === 'false') {
|
|
|
|
return acc.concat({ [curr]: params.search === 'true' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
default:
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return this
|
|
|
|
.find({ $or })
|
|
|
|
.limit(Number(params.limit))
|
|
|
|
.sort(params.sort)
|
|
|
|
.skip(Number(params.skip))
|
|
|
|
.populate(populate || this.associations.map(x => x.alias).join(' '))
|
|
|
|
.lean();
|
2018-06-07 14:35:09 +02:00
|
|
|
},
|
|
|
|
|
2018-06-07 15:08:11 +02:00
|
|
|
countSearch: async function (params = {}) { // eslint-disable-line no-unused-vars
|
2018-06-07 18:50:43 +02:00
|
|
|
const $or = Object.keys(this.attributes).reduce((acc, curr) => {
|
|
|
|
switch (this.attributes[curr].type) {
|
|
|
|
case 'integer':
|
|
|
|
case 'float':
|
|
|
|
case 'decimal':
|
|
|
|
if (!_.isNaN(_.toNumber(params.search))) {
|
|
|
|
return acc.concat({ [curr]: params.search });
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
case 'string':
|
|
|
|
case 'text':
|
|
|
|
case 'password':
|
|
|
|
return acc.concat({ [curr]: { $regex: params.search, $options: 'i' } });
|
|
|
|
case 'boolean':
|
|
|
|
if (params.search === 'true' || params.search === 'false') {
|
|
|
|
return acc.concat({ [curr]: params.search === 'true' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
default:
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return this
|
|
|
|
.find({ $or })
|
|
|
|
.count();
|
2018-06-07 14:35:09 +02:00
|
|
|
},
|
|
|
|
|
2018-04-05 15:20:24 +02:00
|
|
|
findOne: async function (params, populate, raw = true) {
|
|
|
|
const query = this
|
2017-08-29 19:34:34 +02:00
|
|
|
.findOne({
|
2017-09-18 16:41:31 +02:00
|
|
|
[this.primaryKey]: params[this.primaryKey] || params.id
|
2017-09-07 17:16:31 +02:00
|
|
|
})
|
2018-04-05 15:20:24 +02:00
|
|
|
.populate(populate || this.associations.map(x => x.alias).join(' '));
|
|
|
|
|
|
|
|
return raw ? query.lean() : query;
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +02:00
|
|
|
create: async function (params) {
|
2018-02-27 11:52:18 +01:00
|
|
|
// Exclude relationships.
|
|
|
|
const values = Object.keys(params.values).reduce((acc, current) => {
|
2018-02-27 16:53:06 +01:00
|
|
|
if (this._attributes[current] && this._attributes[current].type) {
|
2017-09-18 16:41:31 +02:00
|
|
|
acc[current] = params.values[current];
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
2018-02-27 11:52:18 +01:00
|
|
|
}, {});
|
2017-12-06 15:58:20 +01:00
|
|
|
|
2018-04-25 12:23:53 +02:00
|
|
|
const request = await this.create(values)
|
2018-02-27 11:52:18 +01:00
|
|
|
.catch((err) => {
|
|
|
|
const message = err.message.split('index:');
|
|
|
|
const field = _.words(_.last(message).split('_')[0]);
|
|
|
|
const error = { message: `This ${field} is already taken`, field };
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
});
|
2017-09-18 16:41:31 +02:00
|
|
|
|
2018-05-08 18:44:47 +02:00
|
|
|
// Transform to JSON object.
|
2018-04-25 12:23:53 +02:00
|
|
|
const entry = request.toJSON ? request.toJSON() : request;
|
|
|
|
|
2018-05-08 18:44:47 +02:00
|
|
|
// Extract relations.
|
2018-05-02 15:39:12 +02:00
|
|
|
const relations = this.associations.reduce((acc, association) => {
|
|
|
|
if (params.values[association.alias]) {
|
|
|
|
acc[association.alias] = params.values[association.alias];
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
2017-09-18 16:41:31 +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-18 18:20:33 +02:00
|
|
|
id: entry[this.primaryKey]
|
2018-05-02 15:39:12 +02:00
|
|
|
}, relations)
|
2017-09-18 16:41:31 +02:00
|
|
|
});
|
2017-06-17 17:01:50 +02:00
|
|
|
},
|
|
|
|
|
2017-08-29 19:34:34 +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-08-29 19:34:34 +02:00
|
|
|
delete: async function (params) {
|
2017-09-20 13:19:36 +02:00
|
|
|
// Delete entry.
|
2017-09-15 18:29:50 +02:00
|
|
|
return this
|
2017-08-31 17:26:44 +02:00
|
|
|
.remove({
|
2017-09-20 18:14:18 +02:00
|
|
|
[this.primaryKey]: params.id
|
2017-08-29 19:34:34 +02:00
|
|
|
});
|
2018-06-05 18:35:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
deleteMany: async function (params) {
|
|
|
|
return this
|
|
|
|
.remove({
|
|
|
|
[this.primaryKey]: {
|
|
|
|
$in: params[this.primaryKey] || params.id
|
|
|
|
}
|
|
|
|
});
|
2017-06-17 17:01:50 +02:00
|
|
|
}
|
2017-08-29 19:34:34 +02:00
|
|
|
};
|