Skip to content
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(sql): add support for sub-queries #525

Merged
merged 1 commit into from Apr 29, 2020
Merged

feat(sql): add support for sub-queries #525

merged 1 commit into from Apr 29, 2020

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Apr 29, 2020

You can use sub-queries in selects or in where conditions. To select subquery, use qb.as(alias) method:

const knex = orm.em.getKnex();
const qb1 = orm.em.createQueryBuilder(Book2, 'b').count('b.uuid', true).where({ author: knex.ref('a.id') }).as('Author2.booksTotal');
const qb2 = orm.em.createQueryBuilder(Author2, 'a');
qb2.select(['*', qb1]).orderBy({ booksTotal: 'desc' });

console.log(qb2.getQuery());
// select `a`.*, (select count(distinct `b`.`uuid_pk`) as `count` from `book2` as `b` where `b`.`author_id` = `a`.`id`) as `books_total` from `author2` as `a` order by `books_total` desc

When you want to filter by sub-query, you will need to register it first via qb.withSubquery():

The dynamic property (booksTotal) needs to be defined at the entity level (as persist: false).
You always need to use prefix in the qb.withSchema() (so a.booksTotal).

const knex = orm.em.getKnex();
const qb1 = orm.em.createQueryBuilder(Book2, 'b').count('b.uuid', true).where({ author: knex.ref('a.id') }).getKnexQuery();
const qb2 = orm.em.createQueryBuilder(Author2, 'a');
qb2.select('*').withSubQuery(qb1, 'a.booksTotal').where({ 'a.booksTotal': { $in: [1, 2, 3] } });

console.log(qb2.getQuery());
// select `a`.* from `author2` as `a` where (select count(distinct `b`.`uuid_pk`) as `count` from `book2` as `b` where `b`.`author_id` = `a`.`id`) in (?, ?, ?)

You can use sub-queries in selects or in where conditions. To select subquery, use `qb.as(alias)` method:

```typescript
const knex = orm.em.getKnex();
const qb1 = orm.em.createQueryBuilder(Book2, 'b').count('b.uuid', true).where({ author: knex.ref('a.id') }).as('Author2.booksTotal');
const qb2 = orm.em.createQueryBuilder(Author2, 'a');
qb2.select(['*', qb1]).orderBy({ booksTotal: 'desc' });

console.log(qb2.getQuery());
// select `a`.*, (select count(distinct `b`.`uuid_pk`) as `count` from `book2` as `b` where `b`.`author_id` = `a`.`id`) as `books_total` from `author2` as `a` order by `books_total` desc
```

When you want to filter by sub-query, you will need to register it first via `qb.withSubquery()`:

> The dynamic property (`booksTotal`) needs to be defined at the entity level (as `persist: false`).
> You always need to use prefix in the `qb.withSchema()` (so `a.booksTotal`).

```typescript
const knex = orm.em.getKnex();
const qb1 = orm.em.createQueryBuilder(Book2, 'b').count('b.uuid', true).where({ author: knex.ref('a.id') }).getKnexQuery();
const qb2 = orm.em.createQueryBuilder(Author2, 'a');
qb2.select('*').withSubQuery(qb1, 'a.booksTotal').where({ 'a.booksTotal': { $in: [1, 2, 3] } });

console.log(qb2.getQuery());
// select `a`.* from `author2` as `a` where (select count(distinct `b`.`uuid_pk`) as `count` from `book2` as `b` where `b`.`author_id` = `a`.`id`) in (?, ?, ?)
```
@B4nan B4nan mentioned this pull request Apr 29, 2020
@B4nan B4nan merged commit 0afc778 into dev Apr 29, 2020
@B4nan B4nan deleted the subqueries branch April 29, 2020 18:58
@B4nan B4nan mentioned this pull request Apr 30, 2020
46 tasks
B4nan added a commit that referenced this pull request May 3, 2020
B4nan added a commit that referenced this pull request May 21, 2020
B4nan added a commit that referenced this pull request Jun 1, 2020
B4nan added a commit that referenced this pull request Jun 5, 2020
B4nan added a commit that referenced this pull request Jun 16, 2020
B4nan added a commit that referenced this pull request Aug 2, 2020
B4nan added a commit that referenced this pull request Aug 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant