Add support for multiple includes that use relation syntax
This commit is contained in:
parent
5cd9e88ab2
commit
14dcfb61be
|
@ -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;
|
||||
if (typeof rel === 'string') {
|
||||
var obj = {};
|
||||
if (typeof rel === 'string') {
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue