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 30, 2019
1 parent 2cf6dba commit affa93a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
43 changes: 42 additions & 1 deletion lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,51 @@ 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
let scopeOnRelatedModel = false;
let queryRelated;
if (this._scope && this._scope.collect &&
where !== null && typeof where === 'object') {
queryRelated = {
relation: this._scope.collect,
scope: {
where: where,
},
};
where = {};
scopeOnRelatedModel = true;
}
const targetModel = definition.targetModel(this._receiver);
const scoped = (this._scope && this._scope.where) || {};
const filter = mergeQuery({where: scoped}, {where: where || {}});
return targetModel.updateAll(filter.where, data, options, cb);
if (!scopeOnRelatedModel) {
return targetModel.updateAll(filter.where, data, options, cb);
}
return targetModel.find(filter, options, function(err, findData) {
const relatedModel = targetModel.relations[queryRelated.relation].modelTo;
const keyFrom = targetModel.relations[queryRelated.relation].keyFrom;
const IdKey = idName(relatedModel);

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

function findById(id, filter, options, cb) {
Expand Down
38 changes: 38 additions & 0 deletions test/relations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,44 @@ describe('relations', function() {
}
});

it('should update all scoped record with promises based on related model properties', function(done) {
let id;
Physician.create()
.then(function(physician) {
return physician.patients.create({name: 'a'})
.then(function(ch) {
id = ch.id;
return physician.patients.create({name: 'z'});
})
.then(function() {
return physician.patients.create({name: 'c'});
})
.then(function() {
return verify(physician);
});
}).catch(done);

function verify(physician) {
return physician.patients.updateAll({
name: 'a',
}, {age: 5}, function(err, result) {
if (err) return done(err);
should.exist(result);
result.count.should.equal(1);
physician.patients.findOne({
where: {
name: 'a',
},
}, function(err, patient) {
if (err) return done(err);
should.exist(patient);
patient.age.should.equal(5);
done();
});
});
}
});

it('should build record on scope', function(done) {
Physician.create(function(err, physician) {
const patient = physician.patients.build();
Expand Down

0 comments on commit affa93a

Please sign in to comment.