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

Allow the default scope to be overridden #787

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function mergeQuery(base, update, spec) {
else{
//default behaviour of inclusion merge - merge inclusions at the same
//level. - https://github.com/strongloop/loopback-datasource-juggler/pull/569#issuecomment-95310874
base.include = mergeIncludes(base.include, update.include);
base.include = mergeIncludes(update.include, base.include);
}
}
}
Expand Down
56 changes: 53 additions & 3 deletions test/default-scope.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,19 @@ describe('default scope', function () {
});

Person = db.define('Person', { name: String }, {
scope: { include: 'things' }
scope: { include: 'things' },
scopes: {
withActiveThings: {
include: {
relation: 'things',
scope: {
where: {
active: true
}
}
}
}
}
});

// inst is only valid for instance methods
Expand Down Expand Up @@ -820,7 +832,9 @@ describe('default scope', function () {

before(function (done) {
Person.create({ id: 1, name: 'Person A' }, function(err, person) {
person.things.create({ name: 'Thing A' }, done);
person.things.create({ name: 'Thing A' }, function(err, thing) {
person.things.create({ name: 'Thing B', active: false }, done);
});
});
});

Expand All @@ -831,7 +845,43 @@ describe('default scope', function () {
var things = person.things();
should.exist(things);
things.should.be.an.instanceOf(Array);
things.should.have.length(1);
things.should.have.length(2);
done();
});
});

it('should find a scoped person with filtered relation - things', function(done) {
Person.find({include: {relation: 'things', scope: {where: {active: true}}}}, function(err, persons) {
should.not.exist(err);
should.exist(persons);
persons.should.be.an.instanceOf(Array);
persons.should.have.length(1);

var person = persons[0];
should.exist(person);
var activeThings = person.things();
should.exist(activeThings);
activeThings.should.be.an.instanceOf(Array);
activeThings.should.have.length(1);

done();
});
});

it('should find a scoped person with filtered relation - things', function(done) {
Person.withActiveThings(function(err, persons) {
should.not.exist(err);
should.exist(persons);
persons.should.be.an.instanceOf(Array);
persons.should.have.length(1);

var person = persons[0];
should.exist(person);
var activeThings = person.things();
should.exist(activeThings);
activeThings.should.be.an.instanceOf(Array);
activeThings.should.have.length(1);

done();
});
});
Expand Down