diff --git a/lib/utils.js b/lib/utils.js index 15852f1bc..d24dc4cc7 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -180,7 +180,7 @@ function mergeQuery(base, update, spec) { else{ //default behaviour of inclusion merge - merge inclusions at the same //level. - https://github.com/strongloop/loopback-datasource-juggler/pull/569#issuecomment-95310874 - base.include = mergeIncludes(base.include, update.include); + base.include = mergeIncludes(update.include, base.include); } } } diff --git a/test/default-scope.test.js b/test/default-scope.test.js index ca6c8a9b4..55b6eb5d0 100644 --- a/test/default-scope.test.js +++ b/test/default-scope.test.js @@ -88,7 +88,19 @@ describe('default scope', function () { }); Person = db.define('Person', { name: String }, { - scope: { include: 'things' } + scope: { include: 'things' }, + scopes: { + withActiveThings: { + include: { + relation: 'things', + scope: { + where: { + active: true + } + } + } + } + } }); // inst is only valid for instance methods @@ -820,7 +832,9 @@ describe('default scope', function () { before(function (done) { Person.create({ id: 1, name: 'Person A' }, function(err, person) { - person.things.create({ name: 'Thing A' }, done); + person.things.create({ name: 'Thing A' }, function(err, thing) { + person.things.create({ name: 'Thing B', active: false }, done); + }); }); }); @@ -831,7 +845,43 @@ describe('default scope', function () { var things = person.things(); should.exist(things); things.should.be.an.instanceOf(Array); - things.should.have.length(1); + things.should.have.length(2); + done(); + }); + }); + + it('should find a scoped person with filtered relation - things', function(done) { + Person.find({include: {relation: 'things', scope: {where: {active: true}}}}, function(err, persons) { + should.not.exist(err); + should.exist(persons); + persons.should.be.an.instanceOf(Array); + persons.should.have.length(1); + + var person = persons[0]; + should.exist(person); + var activeThings = person.things(); + should.exist(activeThings); + activeThings.should.be.an.instanceOf(Array); + activeThings.should.have.length(1); + + done(); + }); + }); + + it('should find a scoped person with filtered relation - things', function(done) { + Person.withActiveThings(function(err, persons) { + should.not.exist(err); + should.exist(persons); + persons.should.be.an.instanceOf(Array); + persons.should.have.length(1); + + var person = persons[0]; + should.exist(person); + var activeThings = person.things(); + should.exist(activeThings); + activeThings.should.be.an.instanceOf(Array); + activeThings.should.have.length(1); + done(); }); });