不要怂,就是干,撸起袖子干!

Commit 7d5dce9d by Mick Hansen

Merge pull request #5479 from luin/feature/through-query

Support querying the through model
2 parents a157de4a 8e82462d
......@@ -449,6 +449,13 @@ BelongsToMany.prototype.injectGetter = function(obj) {
_.assign(throughWhere, through.scope);
}
//If a user pass a where on the options through options, make an "and" with the current throughWhere
if (options.through && options.through.where) {
throughWhere = {
$and: [throughWhere, options.through.where]
};
}
options.include = options.include || [];
options.include.push({
association: association.oneFromTarget,
......
......@@ -1643,6 +1643,26 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
});
});
});
describe('query with through.where', function () {
it('should support query the through model', function () {
return this.User.create().then(function (user) {
return Promise.all([
user.createProject({}, { status: 'active', data: 1 }),
user.createProject({}, { status: 'inactive', data: 2 }),
user.createProject({}, { status: 'inactive', data: 3 })
]).then(function () {
return Promise.all([
user.getProjects({ through: { where: { status: 'active' } } }),
user.countProjects({ through: { where: { status: 'inactive' } } }),
]);
});
}).spread(function (activeProjects, inactiveProjectCount) {
expect(activeProjects).to.have.lengthOf(1);
expect(inactiveProjectCount).to.eql(2);
});
});
});
});
describe('removing from the join table', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!