diff --git a/lib/include.js b/lib/include.js index 5e62523d..284e0168 100644 --- a/lib/include.js +++ b/lib/include.js @@ -9,24 +9,31 @@ var defineCachedRelations = utils.defineCachedRelations; * @returns {*} */ function normalizeInclude(include) { + var newInclude; if (typeof include === 'string') { return [include]; } else if (isPlainObject(include)) { // Build an array of key/value pairs - var newInclude = []; + newInclude = []; var rel = include.rel || include.relation; + var obj = {}; if (typeof rel === 'string') { - var obj = {}; obj[rel] = new IncludeScope(include.scope); newInclude.push(obj); } else { for (var key in include) { - var obj = {}; obj[key] = include[key]; newInclude.push(obj); } } return newInclude; + } else if (Array.isArray(include)) { + newInclude = []; + for (var i = 0, n = include.length; i < n; i++) { + var subIncludes = normalizeInclude(include[i]); + newInclude = newInclude.concat(subIncludes); + } + return newInclude; } else { return include; } diff --git a/test/include.test.js b/test/include.test.js index 7eced3ae..a2f522bb 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -256,6 +256,45 @@ describe('include', function () { }); }); + it('should fetch User - Posts AND Passports in relation syntax', + function(done) { + User.find({include: [ + {relation: 'posts', scope: { + where: {title: 'Post A'} + }}, + 'passports' + ]}, function(err, users) { + should.not.exist(err); + should.exist(users); + users.length.should.be.ok; + users.forEach(function(user) { + // The relation should be promoted as the 'owner' property + user.should.have.property('posts'); + user.should.have.property('passports'); + + var userObj = user.toJSON(); + userObj.should.have.property('posts'); + userObj.should.have.property('passports'); + userObj.posts.should.be.an.instanceOf(Array); + userObj.passports.should.be.an.instanceOf(Array); + + // The __cachedRelations should be removed from json output + userObj.should.not.have.property('__cachedRelations'); + + user.__cachedRelations.should.have.property('posts'); + user.__cachedRelations.should.have.property('passports'); + user.__cachedRelations.posts.forEach(function(p) { + p.userId.should.equal(user.id); + p.title.should.be.equal('Post A'); + }); + user.__cachedRelations.passports.forEach(function(pp) { + pp.ownerId.should.equal(user.id); + }); + }); + done(); + }); + }); + it('should not fetch User - AccessTokens', function (done) { User.find({include: ['accesstokens']}, function (err, users) { should.not.exist(err);