Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure relation scope is applied during include #595

Merged
merged 1 commit into from May 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/include.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -571,14 +573,14 @@ 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) {
callback(new Error('Discriminator type "' + modelType +
' specified but no model exists with such name'));
return;
}
relation.applyScope(null, typeFilter);
Model.find(typeFilter, targetFetchHandler);
/**
* Process fetched related objects
Expand Down Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion test/relations.test.js
Expand Up @@ -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) {
Expand All @@ -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'})
Expand Down