Skip to content

Commit

Permalink
Fix loopbackio#1795 - Count issue with related models using though model
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Nov 24, 2019
1 parent 0b81a76 commit 2cf8269
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
var filter = params.include;
// The filter applied on relatedModel
var queryRelated = filter.scope;
delete params.include.scope;
delete params.include;
};

targetModel.find(params, options, function(err, data) {
Expand All @@ -112,12 +112,13 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres

if (scopeOnRelatedModel === true) {
var relatedModel = targetModel.relations[filter.relation].modelTo;
var keyFrom = targetModel.relations[filter.relation].keyFrom;
var IdKey = idName(relatedModel);

// Merge queryRelated filter and targetId filter
var buildWhere = function() {
var IdKeyCondition = {};
IdKeyCondition[IdKey] = collectTargetIds(data, IdKey);
IdKeyCondition[IdKey] = collectTargetIds(data, keyFrom || IdKey);
var mergedWhere = {
and: [IdKeyCondition, queryRelated.where],
};
Expand All @@ -127,7 +128,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
queryRelated.where = buildWhere();
} else {
queryRelated.where = {};
queryRelated.where[IdKey] = collectTargetIds(data, IdKey);
queryRelated.where[IdKey] = collectTargetIds(data, keyFrom || IdKey);
}

relatedModel.find(queryRelated, cb);
Expand Down Expand Up @@ -486,11 +487,49 @@ function defineScope(cls, targetClass, name, params, methods, options) {
options = {};
}
options = options || {};

// If there is a through model
// run another query to apply filter on relatedModel(targetModel)
// see github.com/strongloop/loopback-datasource-juggler/issues/1795
var scopeOnRelatedModel = false;
if (this._scope && this._scope.collect &&
where !== null && typeof where === 'object') {
var queryRelated = {
relation: this._scope.collect,
scope: {
where: where,
},
};
where = {};
scopeOnRelatedModel = true;
}
var targetModel = definition.targetModel(this._receiver);
var scoped = (this._scope && this._scope.where) || {};
var filter = mergeQuery({where: scoped}, {where: where || {}});
return targetModel.count(filter.where, options, cb);
if (!scopeOnRelatedModel) {
return targetModel.count(filter.where, options, cb);
}
targetModel.find(filter, options, function(err, data) {
var relatedModel = targetModel.relations[queryRelated.relation].modelTo;
var keyFrom = targetModel.relations[queryRelated.relation].keyFrom;
var IdKey = idName(relatedModel);

// Merge queryRelated filter and targetId filter
var buildWhere = function() {
var IdKeyCondition = {};
IdKeyCondition[IdKey] = collectTargetIds(data, keyFrom || IdKey);
var mergedWhere = {
and: [IdKeyCondition, queryRelated.scope.where],
};
return mergedWhere;
};
if (queryRelated.scope.where !== undefined) {
queryRelated.scope.where = buildWhere();
} else {
queryRelated.scope.where = {};
queryRelated.scope.where[IdKey] = collectTargetIds(data, keyFrom || IdKey);
}
return relatedModel.count(queryRelated.scope.where, options, cb);
});
}

return definition;
Expand Down

0 comments on commit 2cf8269

Please sign in to comment.