169 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-11-15 16:59:12 +01:00
const _ = require('lodash');
module.exports = {
find: async function (params = {}, populate) {
const records = await this.query(function(qb) {
2017-11-28 15:32:25 +01:00
_.forEach(params.where, (where, key) => {
2018-04-25 12:17:45 +02:00
if (_.isArray(where.value)) {
for (const value in where.value) {
2018-05-16 12:07:02 +02:00
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value]);
2018-04-25 12:17:45 +02:00
}
} else {
qb.where(key, where.symbol, where.value);
}
2017-11-15 16:59:12 +01:00
});
2017-11-28 15:32:25 +01:00
if (params.start) {
qb.offset(params.start);
}
2017-11-28 15:32:25 +01:00
if (params.limit) {
qb.limit(params.limit);
}
2018-05-25 13:21:57 +02:00
if (params.sort) {
if (params.sort.key) {
qb.orderBy(params.sort.key, params.sort.order);
} else {
qb.orderBy(params.sort);
}
}
})
2018-05-16 12:07:02 +02:00
.fetchAll({
withRelated: populate || _.keys(_.groupBy(_.reject(this.associations, { autoPopulate: false }), 'alias'))
});
return records ? records.toJSON() : records;
2017-11-15 16:59:12 +01:00
},
count: async function (params = {}) {
2017-11-15 16:59:12 +01:00
return await this
.forge() // Instanciate the model
.query(qb => {
_.forEach(params.where, (where, key) => {
if (_.isArray(where.value)) {
for (const value in where.value) {
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value]);
}
} else {
qb.where(key, where.symbol, where.value);
}
});
})
2017-11-15 16:59:12 +01:00
.count();
},
findOne: async function (params, populate) {
const primaryKey = params[this.primaryKey] || params._id;
if (primaryKey) {
params = {
[this.primaryKey]: primaryKey
};
2017-11-28 15:32:25 +01:00
}
2017-11-15 16:59:12 +01:00
const record = await this
2017-12-07 16:53:30 +01:00
.forge(params)
2017-11-15 16:59:12 +01:00
.fetch({
withRelated: populate || this.associations.map(x => x.alias)
2017-11-15 16:59:12 +01:00
});
return record ? record.toJSON() : record;
},
create: async function (params) {
2017-11-29 17:09:19 +01:00
return this
2017-11-15 16:59:12 +01:00
.forge()
2017-11-29 17:09:19 +01:00
.save(Object.keys(params).reduce((acc, current) => {
if (_.get(this._attributes, [current, 'type']) || _.get(this._attributes, [current, 'model'])) {
acc[current] = params[current];
}
return acc;
}, {}))
.catch((err) => {
if (err.detail) {
const field = _.last(_.words(err.detail.split('=')[0]));
err = { message: `This ${field} is already taken`, field };
}
throw err;
});
2017-11-15 16:59:12 +01:00
},
update: async function (search, params = {}) {
if (_.isEmpty(params)) {
params = search;
2017-11-15 16:59:12 +01:00
}
const primaryKey = search[this.primaryKey] || search.id;
if (primaryKey) {
search = {
[this.primaryKey]: primaryKey
};
} else {
const entry = await module.exports.findOne.call(this, search);
search = {
[this.primaryKey]: entry[this.primaryKey] || entry.id
};
}
return this.forge(search)
.save(params, {
patch: true
})
.catch((err) => {
const field = _.last(_.words(err.detail.split('=')[0]));
const error = { message: `This ${field} is already taken`, field };
throw error;
});
2017-11-15 16:59:12 +01:00
},
delete: async function (params) {
return await this
.forge({
2018-01-23 19:08:36 +01:00
[this.primaryKey]: params[this.primaryKey] || params.id
2017-11-15 16:59:12 +01:00
})
.destroy();
},
deleteMany: async function (params) {
return await this
.query(qb => {
qb.whereIn(this.primaryKey, params[this.primaryKey] || params.id);
})
.destroy();
},
2017-11-29 16:24:11 +01:00
search: async function (params) {
return this
.query(function(qb) {
qb
.where('username', 'LIKE', `%${params.id}%`)
.orWhere('email', 'LIKE', `%${params.id}%`);
2017-11-29 16:24:11 +01:00
})
.fetchAll();
2017-11-30 12:27:04 +01:00
},
addPermission: async function (params) {
return this
.forge(params)
.save();
},
2017-11-30 12:27:04 +01:00
removePermission: async function (params) {
2018-05-09 15:11:55 +02:00
const value = params[this.primaryKey] ? {
[this.primaryKey]: params[this.primaryKey] || params.id
} : params;
return this
2018-05-09 15:11:55 +02:00
.forge()
.where(value)
.destroy();
2017-11-15 16:59:12 +01:00
}
};