Skip to content

Commit 2cf8269

Browse files
committed
Fix loopbackio#1795 - Count issue with related models using though model
1 parent 0b81a76 commit 2cf8269

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

lib/scope.js

+44-5
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
101101
var filter = params.include;
102102
// The filter applied on relatedModel
103103
var queryRelated = filter.scope;
104-
delete params.include.scope;
104+
delete params.include;
105105
};
106106

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

113113
if (scopeOnRelatedModel === true) {
114114
var relatedModel = targetModel.relations[filter.relation].modelTo;
115+
var keyFrom = targetModel.relations[filter.relation].keyFrom;
115116
var IdKey = idName(relatedModel);
116117

117118
// Merge queryRelated filter and targetId filter
118119
var buildWhere = function() {
119120
var IdKeyCondition = {};
120-
IdKeyCondition[IdKey] = collectTargetIds(data, IdKey);
121+
IdKeyCondition[IdKey] = collectTargetIds(data, keyFrom || IdKey);
121122
var mergedWhere = {
122123
and: [IdKeyCondition, queryRelated.where],
123124
};
@@ -127,7 +128,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
127128
queryRelated.where = buildWhere();
128129
} else {
129130
queryRelated.where = {};
130-
queryRelated.where[IdKey] = collectTargetIds(data, IdKey);
131+
queryRelated.where[IdKey] = collectTargetIds(data, keyFrom || IdKey);
131132
}
132133

133134
relatedModel.find(queryRelated, cb);
@@ -486,11 +487,49 @@ function defineScope(cls, targetClass, name, params, methods, options) {
486487
options = {};
487488
}
488489
options = options || {};
489-
490+
// If there is a through model
491+
// run another query to apply filter on relatedModel(targetModel)
492+
// see github.com/strongloop/loopback-datasource-juggler/issues/1795
493+
var scopeOnRelatedModel = false;
494+
if (this._scope && this._scope.collect &&
495+
where !== null && typeof where === 'object') {
496+
var queryRelated = {
497+
relation: this._scope.collect,
498+
scope: {
499+
where: where,
500+
},
501+
};
502+
where = {};
503+
scopeOnRelatedModel = true;
504+
}
490505
var targetModel = definition.targetModel(this._receiver);
491506
var scoped = (this._scope && this._scope.where) || {};
492507
var filter = mergeQuery({where: scoped}, {where: where || {}});
493-
return targetModel.count(filter.where, options, cb);
508+
if (!scopeOnRelatedModel) {
509+
return targetModel.count(filter.where, options, cb);
510+
}
511+
targetModel.find(filter, options, function(err, data) {
512+
var relatedModel = targetModel.relations[queryRelated.relation].modelTo;
513+
var keyFrom = targetModel.relations[queryRelated.relation].keyFrom;
514+
var IdKey = idName(relatedModel);
515+
516+
// Merge queryRelated filter and targetId filter
517+
var buildWhere = function() {
518+
var IdKeyCondition = {};
519+
IdKeyCondition[IdKey] = collectTargetIds(data, keyFrom || IdKey);
520+
var mergedWhere = {
521+
and: [IdKeyCondition, queryRelated.scope.where],
522+
};
523+
return mergedWhere;
524+
};
525+
if (queryRelated.scope.where !== undefined) {
526+
queryRelated.scope.where = buildWhere();
527+
} else {
528+
queryRelated.scope.where = {};
529+
queryRelated.scope.where[IdKey] = collectTargetIds(data, keyFrom || IdKey);
530+
}
531+
return relatedModel.count(queryRelated.scope.where, options, cb);
532+
});
494533
}
495534

496535
return definition;

0 commit comments

Comments
 (0)