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(query-builder): allow awaiting the QueryBuilder instance #2446

Merged
merged 1 commit into from
Nov 21, 2021

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Nov 21, 2021

Since v5 we can await the QueryBuilder instance, which will automatically execute the QB and return appropriate response. The QB instance is now typed based on usage of select/insert/update/delete/truncate methods to one of:

  • SelectQueryBuilder
    • awaiting yields array of entities (as qb.getResultList())
  • CountQueryBuilder
    • awaiting yields number (as qb.getCount())
  • InsertQueryBuilder (extends RunQueryBuilder)
    • awaiting yields QueryResult
  • UpdateQueryBuilder (extends RunQueryBuilder)
    • awaiting yields QueryResult
  • DeleteQueryBuilder (extends RunQueryBuilder)
    • awaiting yields QueryResult
  • TruncateQueryBuilder (extends RunQueryBuilder)
    • awaiting yields QueryResult
const res1 = await em.qb(Publisher).insert({
  name: 'p1',
  type: PublisherType.GLOBAL,
});
// res1 is of type `QueryResult<Publisher>`
console.log(res1.insertId);

const res2 = await em.qb(Publisher)
        .select('*')
        .where({ name: 'p1' })
        .limit(5);
// res2 is Publisher[]
console.log(res2.map(p => p.name));

const res3 = await em.qb(Publisher).count().where({ name: 'p1' });
// res3 is number
console.log(res3 > 0);

const res4 = await em.qb(Publisher)
        .update({ type: PublisherType.LOCAL })
        .where({ name: 'p1' });
// res4 is QueryResult<Publisher>
console.log(res4.affectedRows > 0);

const res5 = await em.qb(Publisher).delete().where({ name: 'p1' });
// res4 is QueryResult<Publisher>
console.log(res4.affectedRows > 0);
expect(res5.affectedRows > 0).toBe(true); // test the type

Since v5 we can await the `QueryBuilder` instance, which will automatically execute
the QB and return appropriate response. The QB instance is now typed based on usage
of `select/insert/update/delete/truncate` methods to one of:

- `SelectQueryBuilder`
  - awaiting yields array of entities (as `qb.getResultList()`)
- `CountQueryBuilder`
  - awaiting yields number (as `qb.getCount()`)
- `InsertQueryBuilder` (extends `RunQueryBuilder`)
  - awaiting yields `QueryResult`
- `UpdateQueryBuilder` (extends `RunQueryBuilder`)
  - awaiting yields `QueryResult`
- `DeleteQueryBuilder` (extends `RunQueryBuilder`)
  - awaiting yields `QueryResult`
- `TruncateQueryBuilder` (extends `RunQueryBuilder`)
  - awaiting yields `QueryResult`

```ts
const res1 = await em.qb(Publisher).insert({
  name: 'p1',
  type: PublisherType.GLOBAL,
});
// res1 is of type `QueryResult<Publisher>`
console.log(res1.insertId);

const res2 = await em.qb(Publisher)
        .select('*')
        .where({ name: 'p1' })
        .limit(5);
// res2 is Publisher[]
console.log(res2.map(p => p.name));

const res3 = await em.qb(Publisher).count().where({ name: 'p1' });
// res3 is number
console.log(res3 > 0);

const res4 = await em.qb(Publisher)
        .update({ type: PublisherType.LOCAL })
        .where({ name: 'p1' });
// res4 is QueryResult<Publisher>
console.log(res4.affectedRows > 0);

const res5 = await em.qb(Publisher).delete().where({ name: 'p1' });
// res4 is QueryResult<Publisher>
console.log(res4.affectedRows > 0);
expect(res5.affectedRows > 0).toBe(true); // test the type
```
@codecov-commenter
Copy link

codecov-commenter commented Nov 21, 2021

Codecov Report

Merging #2446 (ab4d359) into master (c7a75e0) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #2446   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          190       190           
  Lines        10768     10788   +20     
  Branches      2460      2468    +8     
=========================================
+ Hits         10768     10788   +20     
Impacted Files Coverage Δ
packages/knex/src/SqlEntityManager.ts 100.00% <100.00%> (ø)
packages/knex/src/SqlEntityRepository.ts 100.00% <100.00%> (ø)
packages/knex/src/query/QueryBuilder.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c7a75e0...ab4d359. Read the comment docs.

@B4nan B4nan merged commit c1c4d51 into master Nov 21, 2021
@B4nan B4nan deleted the awaitable-qb branch November 21, 2021 17:44
@B4nan B4nan mentioned this pull request Nov 21, 2021
48 tasks
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

2 participants