Merge pull request #329 from fabien/feature/include-scope
Enable include scope for belongsTo
This commit is contained in:
commit
5662d9f0c2
|
@ -167,7 +167,7 @@ Inclusion.include = function (objects, include, cb) {
|
||||||
|
|
||||||
var related; // relation accessor function
|
var related; // relation accessor function
|
||||||
|
|
||||||
if (relation.multiple && scope) {
|
if ((relation.multiple || relation.type === 'belongsTo') && scope) {
|
||||||
var includeScope = {};
|
var includeScope = {};
|
||||||
var filter = scope.conditions();
|
var filter = scope.conditions();
|
||||||
|
|
||||||
|
@ -183,6 +183,7 @@ Inclusion.include = function (objects, include, cb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.mergeQuery(filter, includeScope, {fields: false});
|
utils.mergeQuery(filter, includeScope, {fields: false});
|
||||||
|
|
||||||
related = inst[relationName].bind(inst, filter);
|
related = inst[relationName].bind(inst, filter);
|
||||||
} else {
|
} else {
|
||||||
related = inst[relationName].bind(inst);
|
related = inst[relationName].bind(inst);
|
||||||
|
|
|
@ -1250,10 +1250,14 @@ BelongsTo.prototype.related = function (refresh, params) {
|
||||||
var fk = this.definition.keyFrom;
|
var fk = this.definition.keyFrom;
|
||||||
var modelInstance = this.modelInstance;
|
var modelInstance = this.modelInstance;
|
||||||
var discriminator;
|
var discriminator;
|
||||||
|
var scopeQuery = null;
|
||||||
|
|
||||||
if (arguments.length === 1) {
|
if (arguments.length === 1) {
|
||||||
params = refresh;
|
params = refresh;
|
||||||
refresh = false;
|
refresh = false;
|
||||||
|
} else if (arguments.length === 2
|
||||||
|
&& typeof refresh === 'object' && typeof params === 'function') {
|
||||||
|
scopeQuery = refresh;
|
||||||
} else if (arguments.length > 2) {
|
} else if (arguments.length > 2) {
|
||||||
throw new Error('Method can\'t be called with more than two arguments');
|
throw new Error('Method can\'t be called with more than two arguments');
|
||||||
}
|
}
|
||||||
|
@ -1303,6 +1307,12 @@ BelongsTo.prototype.related = function (refresh, params) {
|
||||||
|
|
||||||
this.definition.applyScope(modelInstance, query);
|
this.definition.applyScope(modelInstance, query);
|
||||||
|
|
||||||
|
if (scopeQuery) mergeQuery(query, scopeQuery);
|
||||||
|
|
||||||
|
if (Array.isArray(query.fields) && query.fields.indexOf(pk) === -1) {
|
||||||
|
query.fields.push(pk); // always include the pk
|
||||||
|
}
|
||||||
|
|
||||||
modelTo.findOne(query, function (err, inst) {
|
modelTo.findOne(query, function (err, inst) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
return cb(err);
|
||||||
|
|
|
@ -156,7 +156,24 @@ describe('include', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fetch Users with include scope on Posts', function (done) {
|
it('should fetch Users with include scope on Posts - belongsTo', function (done) {
|
||||||
|
Post.find({
|
||||||
|
include: { relation: 'author', scope:{ fields: ['name'] }}
|
||||||
|
}, function (err, posts) {
|
||||||
|
should.not.exist(err);
|
||||||
|
should.exist(posts);
|
||||||
|
posts.length.should.equal(5);
|
||||||
|
|
||||||
|
var author = posts[0].author();
|
||||||
|
author.name.should.equal('User A');
|
||||||
|
author.should.have.property('id');
|
||||||
|
author.should.not.have.property('age');
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fetch Users with include scope on Posts - hasMany', function (done) {
|
||||||
User.find({
|
User.find({
|
||||||
include: {relation: 'posts', scope:{
|
include: {relation: 'posts', scope:{
|
||||||
order: 'title DESC'
|
order: 'title DESC'
|
||||||
|
@ -186,7 +203,7 @@ describe('include', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fetch Users with include scope on Passports', function (done) {
|
it('should fetch Users with include scope on Passports - hasMany', function (done) {
|
||||||
User.find({
|
User.find({
|
||||||
include: {relation: 'passports', scope:{
|
include: {relation: 'passports', scope:{
|
||||||
where: { number: '2' }
|
where: { number: '2' }
|
||||||
|
@ -253,10 +270,6 @@ describe('include', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support hasAndBelongsToMany', function (done) {
|
it('should support hasAndBelongsToMany', function (done) {
|
||||||
|
|
||||||
Assembly.destroyAll(function(err) {
|
|
||||||
Part.destroyAll(function(err) {
|
|
||||||
Assembly.relations.parts.modelThrough.destroyAll(function(err) {
|
|
||||||
Assembly.create({name: 'car'}, function (err, assembly) {
|
Assembly.create({name: 'car'}, function (err, assembly) {
|
||||||
Part.create({partNumber: 'engine'}, function (err, part) {
|
Part.create({partNumber: 'engine'}, function (err, part) {
|
||||||
assembly.parts.add(part, function (err, data) {
|
assembly.parts.add(part, function (err, data) {
|
||||||
|
@ -271,7 +284,7 @@ describe('include', function () {
|
||||||
|
|
||||||
Assembly.find({include: 'parts'}, function (err, assemblies) {
|
Assembly.find({include: 'parts'}, function (err, assemblies) {
|
||||||
assemblies.length.should.equal(1);
|
assemblies.length.should.equal(1);
|
||||||
assemblies[0].parts.length.should.equal(2);
|
assemblies[0].parts().length.should.equal(2);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -281,9 +294,20 @@ describe('include', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
// Not implemented correctly, see: loopback-datasource-juggler/issues/166
|
||||||
});
|
//
|
||||||
|
// it('should support include scope on hasAndBelongsToMany', function (done) {
|
||||||
|
// Assembly.find({include: { relation: 'parts', scope: {
|
||||||
|
// where: { partNumber: 'engine' }
|
||||||
|
// }}}, function (err, assemblies) {
|
||||||
|
// assemblies.length.should.equal(1);
|
||||||
|
// var parts = assemblies[0].parts();
|
||||||
|
// parts.should.have.length(1);
|
||||||
|
// parts[0].partNumber.should.equal('engine');
|
||||||
|
// done();
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue