This fixes the polymorphic hasMany inverse relation not working as expected in include filters.
This commit is contained in:
parent
da323964f5
commit
6d017c1e34
|
@ -18,5 +18,3 @@ Also install the appropriated connector, for example for mongodb:
|
||||||
npm install loopback-connector-mongodb
|
npm install loopback-connector-mongodb
|
||||||
|
|
||||||
See [StrongLoop Documentation](http://docs.strongloop.com/) for more information.
|
See [StrongLoop Documentation](http://docs.strongloop.com/) for more information.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -317,10 +317,14 @@ Inclusion.include = function(objects, include, options, cb) {
|
||||||
inq: uniq(sourceIds),
|
inq: uniq(sourceIds),
|
||||||
};
|
};
|
||||||
if (polymorphic) {
|
if (polymorphic) {
|
||||||
|
var throughModel = polymorphic.invert ?
|
||||||
|
relation.modelTo :
|
||||||
|
relation.modelFrom;
|
||||||
|
|
||||||
//handle polymorphic hasMany (reverse) in which case we need to filter
|
//handle polymorphic hasMany (reverse) in which case we need to filter
|
||||||
//by discriminator to filter other types
|
//by discriminator to filter other types
|
||||||
throughFilter.where[polymorphic.discriminator] =
|
throughFilter.where[polymorphic.discriminator] =
|
||||||
relation.modelFrom.definition.name;
|
throughModel.definition.name;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 1st DB Call of 2 step process. Get through model objects first
|
* 1st DB Call of 2 step process. Get through model objects first
|
||||||
|
|
|
@ -5609,4 +5609,32 @@ describe('relations', function() {
|
||||||
}).should.throw('Invalid relation name: trigger');
|
}).should.throw('Invalid relation name: trigger');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('polymorphic hasMany', function() {
|
||||||
|
before(function(done) {
|
||||||
|
Picture = db.define('Picture', {name: String});
|
||||||
|
Author = db.define('Author', {name: String});
|
||||||
|
PictureLink = db.define('PictureLink', {});
|
||||||
|
|
||||||
|
Author.hasMany(Picture, {through: PictureLink, polymorphic: 'imageable', invert: true});
|
||||||
|
Picture.hasMany(Author, {through: PictureLink, polymorphic: 'imageable'});
|
||||||
|
|
||||||
|
db.automigrate(['Picture', 'Author', 'PictureLink'], done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should properly query through an inverted relationship', function(done) {
|
||||||
|
Author.create({name: 'Steve'}, function(err, author) {
|
||||||
|
if (err) { return done(err); }
|
||||||
|
author.pictures.create({name: 'Steve pic 1'}, function(err, pic) {
|
||||||
|
if (err) { return done(err); }
|
||||||
|
Author.findOne({include: 'pictures'}, function(err, author) {
|
||||||
|
if (err) { return done(err); }
|
||||||
|
author.pictures().length.should.eql(1);
|
||||||
|
author.pictures()[0].name.should.eql('Steve pic 1');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue