diff --git a/lib/include.js b/lib/include.js index 0e125095..a88a2458 100644 --- a/lib/include.js +++ b/lib/include.js @@ -528,6 +528,12 @@ Inclusion.include = function (objects, include, options, cb) { //process each target object tasks.push(targetLinkingTask); function targetLinkingTask(next) { + if (targets.length === 0) { + return async.each(objs, function(obj, next) { + processTargetObj(obj, next); + }, next); + } + async.each(targets, linkManyToOne, next); function linkManyToOne(target, next) { //fix for bug in hasMany with referencesMany @@ -537,7 +543,20 @@ Inclusion.include = function (objects, include, options, cb) { if (!obj) return next(); obj.__cachedRelations[relationName].push(target); processTargetObj(obj, next); - }, next); + }, function(err, processedTargets) { + if (err) { + return next(err); + } + + var objsWithEmptyRelation = objs.filter(function(obj) { + return obj.__cachedRelations[relationName].length === 0; + }); + async.each(objsWithEmptyRelation, function(obj, next) { + processTargetObj(obj, next); + }, function(err) { + next(err, processedTargets); + }); + }); } } diff --git a/test/include.test.js b/test/include.test.js index 9c1504fb..c9b82bee 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -75,6 +75,7 @@ describe('include', function () { user.id.should.eql(p.ownerId); user.__cachedRelations.should.have.property('posts'); user.should.have.property('posts'); + user.toJSON().should.have.property('posts').and.be.an.Array; user.__cachedRelations.posts.forEach(function (pp) { pp.userId.should.eql(user.id); }); @@ -84,6 +85,27 @@ describe('include', function () { }); }); + it('should fetch Passport - Owner - empty Posts', function (done) { + Passport.findOne({where: {number: '4'}, include: {owner: 'posts'}}, function (err, passport) { + should.not.exist(err); + should.exist(passport); + passport.__cachedRelations.should.have.property('owner'); + + // The relation should be promoted as the 'owner' property + passport.should.have.property('owner'); + // The __cachedRelations should be removed from json output + passport.toJSON().should.not.have.property('__cachedRelations'); + + var user = passport.__cachedRelations.owner; + should.exist(user); + user.id.should.eql(passport.ownerId); + user.__cachedRelations.should.have.property('posts'); + user.should.have.property('posts'); + user.toJSON().should.have.property('posts').and.be.an.Array.with.lengthOf(0); + done(); + }); + }); + it('should fetch Passport - Owner - Posts - alternate syntax', function (done) { Passport.find({include: {owner: {relation: 'posts'}}}, function (err, passports) { should.not.exist(err); @@ -134,7 +156,7 @@ describe('include', function () { }, function (err, passports) { should.not.exist(err); should.exist(passports); - passports.length.should.equal(3); + passports.length.should.equal(4); var passport = passports[0]; passport.number.should.equal('1'); @@ -654,7 +676,8 @@ function setup(done) { [ {number: '1', ownerId: createdUsers[0].id}, {number: '2', ownerId: createdUsers[1].id}, - {number: '3'} + {number: '3'}, + {number: '4', ownerId: createdUsers[2].id}, ], function (items) { createdPassports = items;