From cb8bf2ba3eca79743a79284309047cf588c71af8 Mon Sep 17 00:00:00 2001 From: Fabian Off Date: Tue, 8 Dec 2015 21:36:40 +0100 Subject: [PATCH 1/2] Add breaking test --- test/default-scope.test.js | 56 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) 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(); }); }); From 19fe0dba28d517cfdef3dcb65324fa0b2cc71773 Mon Sep 17 00:00:00 2001 From: Fabian Off Date: Tue, 8 Dec 2015 21:37:35 +0100 Subject: [PATCH 2/2] Fix order in which updates are applied --- lib/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); } } }