From 11679beb14d4a78e2944e81b2c0dda71cb5399d6 Mon Sep 17 00:00:00 2001 From: Jaka Hudoklin Date: Mon, 21 Jul 2014 22:39:29 +0200 Subject: [PATCH] add support for disabling relationship includes Signed-off-by: Jaka Hudoklin --- lib/include.js | 6 ++++++ test/include.test.js | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/include.js b/lib/include.js index f276b3d9..342a2bde 100644 --- a/lib/include.js +++ b/lib/include.js @@ -101,6 +101,12 @@ Inclusion.include = function (objects, include, cb) { return; } + // Just skip if inclusion is disabled + if (relation.options.disableInclude) { + cb(); + return; + } + // Calling the relation method for each object async.each(objs, function (obj, callback) { if(relation.type === 'belongsTo') { diff --git a/test/include.test.js b/test/include.test.js index 72980903..394862a2 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -1,7 +1,7 @@ // This test written in mocha+should.js var should = require('./init.js'); -var db, User, Post, Passport, City, Street, Building, Assembly, Part; +var db, User, AccessToken, Post, Passport, City, Street, Building, Assembly, Part; describe('include', function () { @@ -141,6 +141,19 @@ describe('include', function () { }); }); + it('should not fetch User - AccessTokens', function (done) { + User.find({include: ['accesstokens']}, function (err, users) { + should.not.exist(err); + should.exist(users); + users.length.should.be.ok; + users.forEach(function (user) { + var userObj = user.toJSON(); + userObj.should.not.have.property('accesstokens'); + }); + done(); + }); + }); + it('should support hasAndBelongsToMany', function (done) { Assembly.destroyAll(function(err) { @@ -185,6 +198,9 @@ function setup(done) { name: String, age: Number }); + AccessToken = db.define('AccessToken', { + token: String + }); Passport = db.define('Passport', { number: String }); @@ -195,6 +211,10 @@ function setup(done) { Passport.belongsTo('owner', {model: User}); User.hasMany('passports', {foreignKey: 'ownerId'}); User.hasMany('posts', {foreignKey: 'userId'}); + User.hasMany('accesstokens', { + foreignKey: 'userId', + options: {disableInclude: true} + }); Post.belongsTo('author', {model: User, foreignKey: 'userId'}); Assembly = db.define('Assembly', { @@ -226,10 +246,22 @@ function setup(done) { function (items) { createdUsers = items; createPassports(); + createAccessTokens(); } ); } + function createAccessTokens() { + clearAndCreate( + AccessToken, + [ + {token: '1', userId: createdUsers[0].id}, + {token: '2', userId: createdUsers[1].id} + ], + function (items) {} + ); + } + function createPassports() { clearAndCreate( Passport,