Skip to content

Commit

Permalink
Fix bug loopbackio#1795 Count issues related models
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Nov 30, 2019
1 parent b693a57 commit 022029d
Showing 1 changed file with 130 additions and 50 deletions.
180 changes: 130 additions & 50 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,21 +462,41 @@ function defineScope(cls, targetClass, name, params, methods, options) {
return targetModel.destroyAll(filter.where, options, cb);
}
return targetModel.find(filter, options, function(err, findData) {
// Merge queryRelated filter and targetId filter
const buildWhere = function() {
return {
// return {inq: [1,2,3]}}
const smartMerge = function(idCollection, qWhere) {
if (!qWhere[IdKey]) return idCollection;
let merged = {};

const idsA = idCollection.inq;
const idsB = qWhere[IdKey].inq ? qWhere[IdKey].inq : [qWhere[IdKey]];

const intersect = _.intersectionWith(idsA, idsB, _.isEqual);
if (intersect.length === 1) merged = intersect[0];
if (intersect.length > 1) merged = {inq: intersect};

return merged;
};

if (queryRelated.where !== undefined) {
// Merge queryRelated filter and targetId filter
const IdKeyCondition = {};
IdKeyCondition[IdKey] = smartMerge(collectTargetIds(findData, keyFrom),
queryRelated.where);

// if the id in filter doesn't exist after the merge,
// return empty result
if (_.isObject(IdKeyCondition[IdKey]) && _.isEmpty(IdKeyCondition[IdKey])) return cb(null, []);

const mergedWhere = {
and: [
{
[IdKey]: collectTargetIds(findData, keyFrom),
},
queryRelated.scope.where],
IdKeyCondition,
_.omit(queryRelated.where, IdKey),
],
};
};
if (queryRelated.scope.where !== undefined) {
queryRelated.scope.where = buildWhere();
queryRelated.where = mergedWhere;
} else {
queryRelated.scope.where = {};
queryRelated.scope.where[IdKey] = collectTargetIds(findData, keyFrom);
queryRelated.where = {};
queryRelated.where[IdKey] = collectTargetIds(findData, keyFrom);
}
return relatedModel.destroyAll(queryRelated.scope.where, options, cb);
});
Expand Down Expand Up @@ -527,21 +547,41 @@ function defineScope(cls, targetClass, name, params, methods, options) {
return targetModel.updateAll(filter.where, data, options, cb);
}
return targetModel.find(filter, options, function(err, findData) {
// Merge queryRelated filter and targetId filter
const buildWhere = function() {
return {
// return {inq: [1,2,3]}}
const smartMerge = function(idCollection, qWhere) {
if (!qWhere[IdKey]) return idCollection;
let merged = {};

const idsA = idCollection.inq;
const idsB = qWhere[IdKey].inq ? qWhere[IdKey].inq : [qWhere[IdKey]];

const intersect = _.intersectionWith(idsA, idsB, _.isEqual);
if (intersect.length === 1) merged = intersect[0];
if (intersect.length > 1) merged = {inq: intersect};

return merged;
};

if (queryRelated.where !== undefined) {
// Merge queryRelated filter and targetId filter
const IdKeyCondition = {};
IdKeyCondition[IdKey] = smartMerge(collectTargetIds(findData, keyFrom),
queryRelated.where);

// if the id in filter doesn't exist after the merge,
// return empty result
if (_.isObject(IdKeyCondition[IdKey]) && _.isEmpty(IdKeyCondition[IdKey])) return cb(null, []);

const mergedWhere = {
and: [
{
[IdKey]: collectTargetIds(findData, keyFrom),
},
queryRelated.scope.where],
IdKeyCondition,
_.omit(queryRelated.where, IdKey),
],
};
};
if (queryRelated.scope.where !== undefined) {
queryRelated.scope.where = buildWhere();
queryRelated.where = mergedWhere;
} else {
queryRelated.scope.where = {};
queryRelated.scope.where[IdKey] = collectTargetIds(findData, keyFrom);
queryRelated.where = {};
queryRelated.where[IdKey] = collectTargetIds(findData, keyFrom);
}
return relatedModel.updateAll(queryRelated.scope.where, data, options, cb);
});
Expand Down Expand Up @@ -617,22 +657,42 @@ function defineScope(cls, targetClass, name, params, methods, options) {
if (!scopeOnRelatedModel) {
return targetModel.findOne(filter, options, cb);
}
return targetModel.find(filter, options, function(err, data) {
// Merge queryRelated filter and targetId filter
const buildWhere = function() {
return {
return targetModel.find(filter, options, function(err, findData) {
// return {inq: [1,2,3]}}
const smartMerge = function(idCollection, qWhere) {
if (!qWhere[IdKey]) return idCollection;
let merged = {};

const idsA = idCollection.inq;
const idsB = qWhere[IdKey].inq ? qWhere[IdKey].inq : [qWhere[IdKey]];

const intersect = _.intersectionWith(idsA, idsB, _.isEqual);
if (intersect.length === 1) merged = intersect[0];
if (intersect.length > 1) merged = {inq: intersect};

return merged;
};

if (queryRelated.where !== undefined) {
// Merge queryRelated filter and targetId filter
const IdKeyCondition = {};
IdKeyCondition[IdKey] = smartMerge(collectTargetIds(findData, keyFrom),
queryRelated.where);

// if the id in filter doesn't exist after the merge,
// return empty result
if (_.isObject(IdKeyCondition[IdKey]) && _.isEmpty(IdKeyCondition[IdKey])) return cb(null, []);

const mergedWhere = {
and: [
{
[IdKey]: collectTargetIds(data, keyFrom),
},
queryRelated.scope.where],
IdKeyCondition,
_.omit(queryRelated.where, IdKey),
],
};
};
if (queryRelated.scope.where !== undefined) {
queryRelated.scope.where = buildWhere();
queryRelated.where = mergedWhere;
} else {
queryRelated.scope.where = {};
queryRelated.scope.where[IdKey] = collectTargetIds(data, keyFrom);
queryRelated.where = {};
queryRelated.where[IdKey] = collectTargetIds(findData, keyFrom);
}
return relatedModel.findOne(queryRelated.scope, options, cb);
});
Expand Down Expand Up @@ -679,22 +739,42 @@ function defineScope(cls, targetClass, name, params, methods, options) {
if (!scopeOnRelatedModel) {
return targetModel.count(filter.where, options, cb);
}
return targetModel.find(filter, options, function(err, data) {
// Merge queryRelated filter and targetId filter
const buildWhere = function() {
return {
return targetModel.find(filter, options, function(err, findData) {
// return {inq: [1,2,3]}}
const smartMerge = function(idCollection, qWhere) {
if (!qWhere[IdKey]) return idCollection;
let merged = {};

const idsA = idCollection.inq;
const idsB = qWhere[IdKey].inq ? qWhere[IdKey].inq : [qWhere[IdKey]];

const intersect = _.intersectionWith(idsA, idsB, _.isEqual);
if (intersect.length === 1) merged = intersect[0];
if (intersect.length > 1) merged = {inq: intersect};

return merged;
};

if (queryRelated.where !== undefined) {
// Merge queryRelated filter and targetId filter
const IdKeyCondition = {};
IdKeyCondition[IdKey] = smartMerge(collectTargetIds(findData, keyFrom),
queryRelated.where);

// if the id in filter doesn't exist after the merge,
// return empty result
if (_.isObject(IdKeyCondition[IdKey]) && _.isEmpty(IdKeyCondition[IdKey])) return cb(null, []);

const mergedWhere = {
and: [
{
[IdKey]: collectTargetIds(data, keyFrom),
},
queryRelated.scope.where],
IdKeyCondition,
_.omit(queryRelated.where, IdKey),
],
};
};
if (queryRelated.scope.where !== undefined) {
queryRelated.scope.where = buildWhere();
queryRelated.where = mergedWhere;
} else {
queryRelated.scope.where = {};
queryRelated.scope.where[IdKey] = collectTargetIds(data, keyFrom);
queryRelated.where = {};
queryRelated.where[IdKey] = collectTargetIds(findData, keyFrom);
}
return relatedModel.count(queryRelated.scope.where, options, cb);
});
Expand Down

0 comments on commit 022029d

Please sign in to comment.