183 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-11-15 16:59:12 +01:00
const _ = require('lodash');
module.exports = {
find: async function (params = {}) {
let collectionName = this.collectionName;
if (this.collectionName.split('_')) {
collectionName = this.collectionName.split('_')[this.collectionName.split('_').length - 1];
}
const populate = this.associations
.filter(ast => ast.autoPopulate)
.reduce((acc, ast) => {
const from = ast.plugin ? `${ast.plugin}_${ast.model}` : ast.collection ? ast.collection : ast.model;
const as = ast.alias;
const localField = !ast.dominant ? '_id' : ast.via === collectionName || ast.via === 'related' ? '_id' : ast.alias;
const foreignField = ast.filter ? `${ast.via}.ref` :
ast.dominant ?
(ast.via === collectionName ? ast.via : '_id') :
(ast.via === collectionName ? '_id' : ast.via);
acc.push({
$lookup: {
from,
localField,
foreignField,
as,
}
});
if (ast.type === 'model') {
acc.push({
$unwind: {
path: `$${ast.alias}`,
preserveNullAndEmptyArrays: true
}
});
}
if (params.relations) {
Object.keys(params.relations).forEach(
(relationName) => {
if (ast.alias === relationName) {
const association = this.associations.find(a => a.alias === relationName);
if (association) {
const relation = params.relations[relationName];
Object.keys(relation).forEach(
(filter) => {
acc.push({
$match: { [`${relationName}.${filter}`]: relation[filter] }
});
}
);
}
}
}
);
}
return acc;
}, []);
const result = this
.aggregate([
{
$match: params.where ? params.where : {}
},
...populate,
]);
if (params.start) result.skip(params.start);
if (params.limit) result.limit(params.limit);
if (params.sort) result.sort(params.sort);
return result;
2017-11-15 16:59:12 +01:00
},
count: async function (params = {}) {
2017-11-15 16:59:12 +01:00
return Number(await this
.count(params));
2017-11-15 16:59:12 +01:00
},
findOne: async function (params, populate) {
const primaryKey = params[this.primaryKey] || params.id;
if (primaryKey) {
params = {
[this.primaryKey]: primaryKey
};
}
2017-11-15 16:59:12 +01:00
return this
2017-11-16 14:29:49 +01:00
.findOne(params)
.populate(populate || this.associations.map(x => x.alias).join(' '))
.lean();
2017-11-15 16:59:12 +01:00
},
create: async function (params) {
2017-11-29 17:09:19 +01:00
return this.create(Object.keys(params).reduce((acc, current) => {
if (_.get(this._attributes, [current, 'type']) || _.get(this._attributes, [current, 'model'])) {
2017-11-29 17:09:19 +01:00
acc[current] = params[current];
2017-11-15 16:59:12 +01:00
}
2017-12-06 11:47:39 +01:00
2017-11-15 16:59:12 +01:00
return acc;
2017-12-06 11:47:39 +01:00
}, {}))
.catch((err) => {
if (err.message.indexOf('index:') !== -1) {
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-12-06 15:11:55 +01:00
throw err;
});
2017-11-15 16:59:12 +01:00
},
update: async function (search, params = {}) {
if (_.isEmpty(params)) {
params = search;
2018-01-22 18:19:44 +01:00
}
const primaryKey = search[this.primaryKey] || search.id;
2018-01-22 18:19:44 +01:00
if (primaryKey) {
search = {
[this.primaryKey]: primaryKey
};
2018-01-22 18:19:44 +01:00
}
return this.update(search, params, {
2017-11-29 18:45:51 +01:00
strict: false
2017-12-06 15:11:55 +01:00
})
.catch((error) => {
const field = _.last(_.words(error.message.split('_')[0]));
const err = { message: `This ${field} is already taken`, field };
2017-12-06 15:11:55 +01:00
throw err;
});
2017-11-15 16:59:12 +01:00
},
delete: async function (params) {
// Delete entry.
return this
.remove({
2017-11-29 18:45:51 +01:00
[this.primaryKey]: params[this.primaryKey] || params.id
2017-11-15 16:59:12 +01:00
});
},
deleteMany: async function (params) {
// Delete entry.
return this
.remove({
[this.primaryKey]: {
$in: params[this.primaryKey] || params.id
}
});
},
search: async function (params) {
const re = new RegExp(params.id);
return this
.find({
'$or': [
{ username: re },
{ email: re }
]
});
2017-11-30 12:27:04 +01:00
},
addPermission: async function (params) {
return this
.create(params);
},
removePermission: async function (params) {
return this
.remove(params);
2017-11-15 16:59:12 +01:00
}
};