Update user bookshelf fetch query

This commit is contained in:
Jim Laurie 2017-11-28 15:32:25 +01:00
parent baa2f92040
commit 44c511ad50

View File

@ -2,10 +2,20 @@ const _ = require('lodash');
module.exports = { module.exports = {
find: async function (params) { find: async function (params) {
return await this return Article.query(function(qb) {
.forge() _.forEach(params.where, (where, key) => {
.fetchAll({ qb.where(key, where.symbol, where.value);
withRelated: this.associations.map(x => x.alias) });
if (params.sort) {
qb.orderBy(params.sort);
}
qb.offset(params.start);
qb.limit(params.limit);
}).fetchAll({
withRelated: _.keys(_.groupBy(_.reject(this.associations, {autoPopulate: false}), 'alias'))
}); });
}, },
@ -16,10 +26,13 @@ module.exports = {
}, },
findOne: async function (params) { findOne: async function (params) {
if (_.get(params, 'where._id')) {
params.where.id = params.where._id;
delete params.where._id;
}
const record = await this const record = await this
.forge({ .forge(params.where)
[this.primaryKey]: params[this.primaryKey]
})
.fetch({ .fetch({
withRelated: this.associations.map(x => x.alias) withRelated: this.associations.map(x => x.alias)
}); });