Update user-permissions query logic for both bookshelf/mongoose

This commit is contained in:
Kamal Bennani 2018-09-14 00:23:27 +02:00
parent ff0a87df7a
commit f8d844a48a
No known key found for this signature in database
GPG Key ID: 4513063CDB1A1C25
2 changed files with 120 additions and 12 deletions

View File

@ -2,7 +2,7 @@ const _ = require('lodash');
module.exports = {
find: async function (params = {}, populate) {
const records = await this.query(function(qb) {
const records = await this.query((qb) => {
_.forEach(params.where, (where, key) => {
if (_.isArray(where.value)) {
for (const value in where.value) {
@ -28,10 +28,54 @@ module.exports = {
qb.orderBy(params.sort);
}
}
if (params.relations) {
Object.keys(params.relations).forEach(
(relationName) => {
const ast = this.associations.find(a => a.alias === relationName);
if (ast) {
const model = ast.plugin ?
strapi.plugins[ast.plugin].models[ast.model ? ast.model : ast.collection] :
strapi.models[ast.model ? ast.model : ast.collection];
qb.distinct();
if (ast.tableCollectionName) {
qb.innerJoin(
ast.tableCollectionName,
`${ast.tableCollectionName}.${this.info.name}_${this.primaryKey}`,
`${this.collectionName}.${this.primaryKey}`,
);
qb.innerJoin(
`${relationName}`,
`${relationName}.${this.attributes[relationName].column}`,
`${ast.tableCollectionName}.${this.attributes[relationName].attribute}_${this.attributes[relationName].column}`,
);
} else {
const relationTable = model.collectionName;
const externalKey = ast.type === 'collection' ?
`${model.collectionName}.${ast.via}` :
`${model.collectionName}.${model.primaryKey}`;
const internalKey = !ast.dominant ? `${this.collectionName}.${this.primaryKey}` :
ast.via === this.collectionName ? `${this.collectionName}.${ast.alias}` : `${this.collectionName}.${this.primaryKey}`;
qb.innerJoin(relationTable, externalKey, internalKey);
}
const relation = params.relations[relationName];
Object.keys(params.relations[relationName]).forEach(
(filter) => {
qb.where(`${model.collectionName}.${filter}`, `${relation[filter].symbol}`, `${relation[filter].value}`);
}
);
}
}
);
}
})
.fetchAll({
withRelated: populate || _.keys(_.groupBy(_.reject(this.associations, { autoPopulate: false }), 'alias'))
});
.fetchAll({
withRelated: populate || _.keys(_.groupBy(_.reject(this.associations, { autoPopulate: false }), 'alias'))
});
return records ? records.toJSON() : records;

View File

@ -1,14 +1,78 @@
const _ = require('lodash');
module.exports = {
find: async function (params = {}, populate) {
return this
.find(params.where)
.limit(Number(params.limit))
.sort(params.sort)
.skip(Number(params.skip))
.populate(populate || this.associations.map(x => x.alias).join(' '))
.lean();
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;
},
count: async function (params = {}) {