Add support for multiple includes that use relation syntax

This commit is contained in:
Raymond Feng 2014-11-04 20:45:25 -08:00
parent 5cd9e88ab2
commit 14dcfb61be
2 changed files with 49 additions and 3 deletions

View File

@ -9,24 +9,31 @@ var defineCachedRelations = utils.defineCachedRelations;
* @returns {*} * @returns {*}
*/ */
function normalizeInclude(include) { function normalizeInclude(include) {
var newInclude;
if (typeof include === 'string') { if (typeof include === 'string') {
return [include]; return [include];
} else if (isPlainObject(include)) { } else if (isPlainObject(include)) {
// Build an array of key/value pairs // Build an array of key/value pairs
var newInclude = []; newInclude = [];
var rel = include.rel || include.relation; var rel = include.rel || include.relation;
var obj = {};
if (typeof rel === 'string') { if (typeof rel === 'string') {
var obj = {};
obj[rel] = new IncludeScope(include.scope); obj[rel] = new IncludeScope(include.scope);
newInclude.push(obj); newInclude.push(obj);
} else { } else {
for (var key in include) { for (var key in include) {
var obj = {};
obj[key] = include[key]; obj[key] = include[key];
newInclude.push(obj); newInclude.push(obj);
} }
} }
return newInclude; 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 { } else {
return include; return include;
} }

View File

@ -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) { it('should not fetch User - AccessTokens', function (done) {
User.find({include: ['accesstokens']}, function (err, users) { User.find({include: ['accesstokens']}, function (err, users) {
should.not.exist(err); should.not.exist(err);