Make sure relation scope is applied during include

This commit is contained in:
Raymond Feng 2015-05-16 11:49:02 -07:00
parent d19001a56e
commit eac74ad014
2 changed files with 28 additions and 3 deletions

View File

@ -350,6 +350,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[modelToIdName] = {
inq: targetIds
};
//make sure that the modelToIdName is included if fields are specified
if (Array.isArray(fields) && fields.indexOf(modelToIdName) === -1) {
fields.push(modelToIdName);
@ -430,7 +431,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[relation.keyTo] = {
inq: allTargetIds
};
relation.applyScope(null, filter);
/**
* Make the DB Call, fetch all target objects
*/
@ -494,6 +495,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[relation.keyTo] = {
inq: sourceIds
};
relation.applyScope(null, filter);
relation.modelTo.find(filter, targetFetchHandler);
/**
* Process fetched related objects
@ -571,7 +573,6 @@ Inclusion.include = function (objects, include, cb) {
typeFilter.where[relation.keyTo] = {
inq: targetIds
};
var app = relation.modelFrom.app;
var Model = lookupModel(relation.modelFrom.dataSource.modelBuilder.
models, modelType);
if (!Model) {
@ -579,6 +580,7 @@ Inclusion.include = function (objects, include, cb) {
' specified but no model exists with such name'));
return;
}
relation.applyScope(null, typeFilter);
Model.find(typeFilter, targetFetchHandler);
/**
* Process fetched related objects
@ -647,6 +649,7 @@ Inclusion.include = function (objects, include, cb) {
filter.where[relation.keyTo] = {
inq: targetIds
};
relation.applyScope(null, filter);
relation.modelTo.find(filter, targetFetchHandler);
/**
* Process fetched related objects

View File

@ -2890,7 +2890,17 @@ describe('relations', function () {
});
});
it('should find record that match scope', function (done) {
it('should include record that matches scope', function(done) {
Supplier.findById(supplierId, {include: 'account'}, function(err, supplier) {
should.exists(supplier.toJSON().account);
supplier.account(function(err, account) {
should.exists(account);
done();
});
});
});
it('should not find record that does not match scope', function (done) {
Account.updateAll({ block: true }, function (err) {
Supplier.findById(supplierId, function (err, supplier) {
supplier.account(function (err, account) {
@ -2901,6 +2911,18 @@ describe('relations', function () {
});
});
it('should not include record that does not match scope', function (done) {
Account.updateAll({ block: true }, function (err) {
Supplier.findById(supplierId, {include: 'account'}, function (err, supplier) {
should.not.exists(supplier.toJSON().account);
supplier.account(function (err, account) {
should.not.exists(account);
done();
});
});
});
});
it('can be used to query data with promises', function (done) {
db.automigrate(function () {
Supplier.create({name: 'Supplier 1'})