-
-
Notifications
You must be signed in to change notification settings - Fork 504
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
feat(query-builder): allow joining sub-queries #4747
Merged
Merged
Conversation
This file contains 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
Codecov ReportAttention:
📢 Thoughts on this report? Let us know!. |
004a8c0
to
ec9cb03
Compare
ec9cb03
to
441c171
Compare
This was referenced Sep 26, 2023
B4nan
added a commit
that referenced
this pull request
Sep 30, 2023
Sometimes you might want to join a relation, but want to have more control over the query. The ORM allows you to override the join target with a sub-query, while keeping the original metadata for hydration: ```ts // subquery can be a knex query builder as well const subquery = await em.createQueryBuilder(Book, 'b') .where({ ... }) .orderBy({ title: 'asc' }).limit(1); const authors = await em.createQueryBuilder(Author, 'a') .select('*') // pass in both the property path and the subquery into the first argument as a tuple .leftJoinAndSelect(['a.books', subquery], 'b') // you can join more relations on top of the subquery join .leftJoinAndSelect('b.tags', 't') .getResultList(); ``` This will produce query similar to the following: ```sql select `a`.*, `b`.`id` as `b__id`, `b`.`title` as `b__title`, `b`.`author_id` as `b__author_id`, `b`.`publisher_id` as `b__publisher_id`, `t`.`id` as `t__id`, `t`.`name` as `t__name` from `author` as `a` left join ( select `b`.*, `b`.price * 1.19 as `price_taxed` from `book` as `b` order by `b`.`title` asc limit 1 ) as `b` on `b`.`author_id` = `a`.`id` left join `book_tags` as `e1` on `b`.`uuid_pk` = `e1`.`book_uuid_pk` left join `book_tag` as `t` on `e1`.`book_tag_id` = `t`.`id` ``` Closes #4429 Closes #4549
B4nan
added a commit
that referenced
this pull request
Oct 2, 2023
Sometimes you might want to join a relation, but want to have more control over the query. The ORM allows you to override the join target with a sub-query, while keeping the original metadata for hydration: ```ts // subquery can be a knex query builder as well const subquery = await em.createQueryBuilder(Book, 'b') .where({ ... }) .orderBy({ title: 'asc' }).limit(1); const authors = await em.createQueryBuilder(Author, 'a') .select('*') // pass in both the property path and the subquery into the first argument as a tuple .leftJoinAndSelect(['a.books', subquery], 'b') // you can join more relations on top of the subquery join .leftJoinAndSelect('b.tags', 't') .getResultList(); ``` This will produce query similar to the following: ```sql select `a`.*, `b`.`id` as `b__id`, `b`.`title` as `b__title`, `b`.`author_id` as `b__author_id`, `b`.`publisher_id` as `b__publisher_id`, `t`.`id` as `t__id`, `t`.`name` as `t__name` from `author` as `a` left join ( select `b`.*, `b`.price * 1.19 as `price_taxed` from `book` as `b` order by `b`.`title` asc limit 1 ) as `b` on `b`.`author_id` = `a`.`id` left join `book_tags` as `e1` on `b`.`uuid_pk` = `e1`.`book_uuid_pk` left join `book_tag` as `t` on `e1`.`book_tag_id` = `t`.`id` ``` Closes #4429 Closes #4549
B4nan
added a commit
that referenced
this pull request
Oct 17, 2023
Sometimes you might want to join a relation, but want to have more control over the query. The ORM allows you to override the join target with a sub-query, while keeping the original metadata for hydration: ```ts // subquery can be a knex query builder as well const subquery = await em.createQueryBuilder(Book, 'b') .where({ ... }) .orderBy({ title: 'asc' }).limit(1); const authors = await em.createQueryBuilder(Author, 'a') .select('*') // pass in both the property path and the subquery into the first argument as a tuple .leftJoinAndSelect(['a.books', subquery], 'b') // you can join more relations on top of the subquery join .leftJoinAndSelect('b.tags', 't') .getResultList(); ``` This will produce query similar to the following: ```sql select `a`.*, `b`.`id` as `b__id`, `b`.`title` as `b__title`, `b`.`author_id` as `b__author_id`, `b`.`publisher_id` as `b__publisher_id`, `t`.`id` as `t__id`, `t`.`name` as `t__name` from `author` as `a` left join ( select `b`.*, `b`.price * 1.19 as `price_taxed` from `book` as `b` order by `b`.`title` asc limit 1 ) as `b` on `b`.`author_id` = `a`.`id` left join `book_tags` as `e1` on `b`.`uuid_pk` = `e1`.`book_uuid_pk` left join `book_tag` as `t` on `e1`.`book_tag_id` = `t`.`id` ``` Closes #4429 Closes #4549
B4nan
added a commit
that referenced
this pull request
Oct 21, 2023
Sometimes you might want to join a relation, but want to have more control over the query. The ORM allows you to override the join target with a sub-query, while keeping the original metadata for hydration: ```ts // subquery can be a knex query builder as well const subquery = await em.createQueryBuilder(Book, 'b') .where({ ... }) .orderBy({ title: 'asc' }).limit(1); const authors = await em.createQueryBuilder(Author, 'a') .select('*') // pass in both the property path and the subquery into the first argument as a tuple .leftJoinAndSelect(['a.books', subquery], 'b') // you can join more relations on top of the subquery join .leftJoinAndSelect('b.tags', 't') .getResultList(); ``` This will produce query similar to the following: ```sql select `a`.*, `b`.`id` as `b__id`, `b`.`title` as `b__title`, `b`.`author_id` as `b__author_id`, `b`.`publisher_id` as `b__publisher_id`, `t`.`id` as `t__id`, `t`.`name` as `t__name` from `author` as `a` left join ( select `b`.*, `b`.price * 1.19 as `price_taxed` from `book` as `b` order by `b`.`title` asc limit 1 ) as `b` on `b`.`author_id` = `a`.`id` left join `book_tags` as `e1` on `b`.`uuid_pk` = `e1`.`book_uuid_pk` left join `book_tag` as `t` on `e1`.`book_tag_id` = `t`.`id` ``` Closes #4429 Closes #4549
B4nan
added a commit
that referenced
this pull request
Oct 25, 2023
Sometimes you might want to join a relation, but want to have more control over the query. The ORM allows you to override the join target with a sub-query, while keeping the original metadata for hydration: ```ts // subquery can be a knex query builder as well const subquery = await em.createQueryBuilder(Book, 'b') .where({ ... }) .orderBy({ title: 'asc' }).limit(1); const authors = await em.createQueryBuilder(Author, 'a') .select('*') // pass in both the property path and the subquery into the first argument as a tuple .leftJoinAndSelect(['a.books', subquery], 'b') // you can join more relations on top of the subquery join .leftJoinAndSelect('b.tags', 't') .getResultList(); ``` This will produce query similar to the following: ```sql select `a`.*, `b`.`id` as `b__id`, `b`.`title` as `b__title`, `b`.`author_id` as `b__author_id`, `b`.`publisher_id` as `b__publisher_id`, `t`.`id` as `t__id`, `t`.`name` as `t__name` from `author` as `a` left join ( select `b`.*, `b`.price * 1.19 as `price_taxed` from `book` as `b` order by `b`.`title` asc limit 1 ) as `b` on `b`.`author_id` = `a`.`id` left join `book_tags` as `e1` on `b`.`uuid_pk` = `e1`.`book_uuid_pk` left join `book_tag` as `t` on `e1`.`book_tag_id` = `t`.`id` ``` Closes #4429 Closes #4549
B4nan
added a commit
that referenced
this pull request
Nov 2, 2023
Sometimes you might want to join a relation, but want to have more control over the query. The ORM allows you to override the join target with a sub-query, while keeping the original metadata for hydration: ```ts // subquery can be a knex query builder as well const subquery = await em.createQueryBuilder(Book, 'b') .where({ ... }) .orderBy({ title: 'asc' }).limit(1); const authors = await em.createQueryBuilder(Author, 'a') .select('*') // pass in both the property path and the subquery into the first argument as a tuple .leftJoinAndSelect(['a.books', subquery], 'b') // you can join more relations on top of the subquery join .leftJoinAndSelect('b.tags', 't') .getResultList(); ``` This will produce query similar to the following: ```sql select `a`.*, `b`.`id` as `b__id`, `b`.`title` as `b__title`, `b`.`author_id` as `b__author_id`, `b`.`publisher_id` as `b__publisher_id`, `t`.`id` as `t__id`, `t`.`name` as `t__name` from `author` as `a` left join ( select `b`.*, `b`.price * 1.19 as `price_taxed` from `book` as `b` order by `b`.`title` asc limit 1 ) as `b` on `b`.`author_id` = `a`.`id` left join `book_tags` as `e1` on `b`.`uuid_pk` = `e1`.`book_uuid_pk` left join `book_tag` as `t` on `e1`.`book_tag_id` = `t`.`id` ``` Closes #4429 Closes #4549
B4nan
added a commit
that referenced
this pull request
Nov 5, 2023
Sometimes you might want to join a relation, but want to have more control over the query. The ORM allows you to override the join target with a sub-query, while keeping the original metadata for hydration: ```ts // subquery can be a knex query builder as well const subquery = await em.createQueryBuilder(Book, 'b') .where({ ... }) .orderBy({ title: 'asc' }).limit(1); const authors = await em.createQueryBuilder(Author, 'a') .select('*') // pass in both the property path and the subquery into the first argument as a tuple .leftJoinAndSelect(['a.books', subquery], 'b') // you can join more relations on top of the subquery join .leftJoinAndSelect('b.tags', 't') .getResultList(); ``` This will produce query similar to the following: ```sql select `a`.*, `b`.`id` as `b__id`, `b`.`title` as `b__title`, `b`.`author_id` as `b__author_id`, `b`.`publisher_id` as `b__publisher_id`, `t`.`id` as `t__id`, `t`.`name` as `t__name` from `author` as `a` left join ( select `b`.*, `b`.price * 1.19 as `price_taxed` from `book` as `b` order by `b`.`title` asc limit 1 ) as `b` on `b`.`author_id` = `a`.`id` left join `book_tags` as `e1` on `b`.`uuid_pk` = `e1`.`book_uuid_pk` left join `book_tag` as `t` on `e1`.`book_tag_id` = `t`.`id` ``` Closes #4429 Closes #4549
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Joining sub-queries
Sometimes you might want to join a relation, but want to have more control over the query. The ORM allows you to override the join target with a sub-query, while keeping the original metadata for hydration:
This will produce query similar to the following:
Closes #4429
Closes #4549