From aaf037fc5157e42e87bfe40b6ebb98e3f4c89c30 Mon Sep 17 00:00:00 2001 From: Laurent Chenay Date: Mon, 28 Jul 2014 15:17:36 +0200 Subject: [PATCH 1/2] Do not overwrite inclusion but scope them. Needed in relation hasManyThrought --- lib/scope.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index c1c90eba..f6384856 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -246,9 +246,15 @@ function mergeQuery(base, update) { } } - // Overwrite inclusion + // Merge inclusion if (update.include) { - base.include = update.include; + if (!base.include) { + base.include = update.include; + } else { + var saved = base.include; + base.include = {}; + base.include[update.include] = [saved]; + } } if (update.collect) { base.collect = update.collect; From b336166acc8260b4e8725b542072449d8c7f9296 Mon Sep 17 00:00:00 2001 From: Laurent Chenay Date: Tue, 29 Jul 2014 11:05:57 +0200 Subject: [PATCH 2/2] Add test to protect the use of include in related method --- test/relations.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/relations.test.js b/test/relations.test.js index f091486d..b74c414c 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -273,6 +273,34 @@ describe('relations', function () { } }); + it('should allow to use include syntax on related data', function (done) { + var Address = db.define('Address', {name: String}); + Patient.belongsTo(Address); + Physician.create(function (err, physician) { + physician.patients.create({name: 'a'}, function (err, patient) { + Address.create({name: 'z'}, function (err, address) { + patient.address(address); + patient.save(function() { + verify(physician); + }); + }); + }); + }); + function verify(physician) { + physician.patients({include: 'address'}, function (err, ch) { + should.not.exist(err); + should.exist(ch); + ch.should.have.lengthOf(1); + ch[0].addressId.should.equal(1); + var address = ch[0].address(); + should.exist(address); + address.should.be.an.instanceof(Address); + address.name.should.equal('z'); + done(); + }); + } + }); + it('should set targetClass on scope property', function() { should.equal(Physician.prototype.patients._targetClass, 'Patient'); });