Merge pull request #674 from hotaru355/issue-673
fixes issue 673: Include hasMany of relation does not return empty array
This commit is contained in:
commit
fa570a9514
|
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue