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

Get results count using query builder #2066

Closed
angelvega93 opened this issue Jul 26, 2021 · 3 comments
Closed

Get results count using query builder #2066

angelvega93 opened this issue Jul 26, 2021 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@angelvega93
Copy link
Contributor

It would be great if we could get results count using the query builder, currently it seems not to be possible

For example:

const qb = ctx.em.createQueryBuilder(...).where(...);

let result = qb.getResult();
let total = qb.getCount();

await Promise.all([results, total]);
@angelvega93 angelvega93 added the enhancement New feature or request label Jul 26, 2021
@B4nan
Copy link
Member

B4nan commented Jul 26, 2021

Just use qb.execute()

@angelvega93
Copy link
Contributor Author

angelvega93 commented Jul 26, 2021

Yes, currently im doing this:

const total = (await qb.select("count(*) as total").execute("get")).total;
const result = await qb.select(...).limit(limit).offset(offset).execute();

but it would be nice and cleaner if we could just use getCount like TypeORM

https://github.com/typeorm/typeorm/blob/7a0f7733f2cf704d60548a6b15d1fa2977a9e401/src/query-builder/SelectQueryBuilder.ts#L1141

@B4nan
Copy link
Member

B4nan commented Aug 2, 2021

I said you should use execute to read the results, there is already qb.count() method. Correct way is:

const res = await qb.count().where(...).execute(); // `res.count` is the count

But sure, we can have getCount() too in v5. Care to send a PR?

@B4nan B4nan added this to the 5.0 milestone Aug 2, 2021
@B4nan B4nan closed this as completed in f773736 Aug 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants