mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 14:14:10 +00:00
Update user-permissions query logic for both bookshelf/mongoose
This commit is contained in:
parent
ff0a87df7a
commit
f8d844a48a
@ -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;
|
||||
|
||||
@ -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 = {}) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user