This repository was archived by the owner on Apr 17, 2020. It is now read-only.
WIP: feat: add support for many-to-many joins#33
Open
joelmukuthu wants to merge 6 commits intomasterfrom
Open
Conversation
ca9cb98 to
d328d58
Compare
Contributor
Author
|
Instead of a User.fetch({
leftJoin: GroupMembership.query
.asJoinQuery(true) // tells @knorm/relations to not include this join in the query results
.join(Group.query.as('groups'))
}); |
To allow the `via` option preparation code to re-use it
Instead of using `INNER JOIN` as was the case before
Useful for many-to-many joins:
```js
class User extends Model {}
User.table = 'user';
User.fields = {
id: 'integer'
};
class Group extends Model {}
Group.table = 'user';
Group.fields = {
id: 'integer'
};
class GroupMembership extends Model {}
GroupMembership.table = 'group_membership';
GroupMembership.fields = {
userId: { type: 'integer', references: User.fields.id },
groupId: { type: 'integer', references: Group.fields.id }
};
User.fetch({
leftJoin: Group.query
.via(GroupMembership)
.as('groups')
});
```
d328d58 to
0bb1233
Compare
Pull Request Test Coverage Report for Build 299
💛 - Coveralls |
boh1996
reviewed
Jan 2, 2019
| new User({ id: 1, groups: [new Group({ id: 1 })] }), | ||
| new User({ id: 2, groups: [new Group({ id: 2 })] }), | ||
| new User({ id: 3, groups: [new Group({ id: 2 })] }), | ||
| new User({ id: 4, groups: null }) |
Contributor
There was a problem hiding this comment.
Wouldn't it make sense to return an empty array here?
Contributor
Author
There was a problem hiding this comment.
Yeah, that's a bug: #8 I intend to fix it in v2 (breaking change)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Usage:
which yields SQL similar to:
and output such as:
Closes knorm/knorm#143