-
-
Notifications
You must be signed in to change notification settings - Fork 499
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(core): add getResultAndCount() #3891
Conversation
docs/docs/query-builder.md
Outdated
@@ -294,6 +294,21 @@ qb.select('*').limit(10, 20).where({ $and: [{ id: { $nin: [3, 4] } }, { id: { $g | |||
const count = await qb.getCount(); | |||
``` | |||
|
|||
## Paginate queries |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## Paginate queries | |
## Pagination |
console.log(authors.length); // based on limit parameter, e.g. 10 | ||
console.log(count); // total count, e.g. 1327 | ||
``` | ||
|
||
This will also remove any existing limit and offset from the query (the QB will be cloned under the hood, so calling `getCount()` does not mutate the original QB state). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this belongs to the paragraph above
docs/docs/query-builder.md
Outdated
@@ -294,6 +294,21 @@ qb.select('*').limit(10, 20).where({ $and: [{ id: { $nin: [3, 4] } }, { id: { $g | |||
const count = await qb.getCount(); | |||
``` | |||
|
|||
## Paginate queries | |||
|
|||
If we want to paginate results, we can use `qb.getResultAndCount` method. It returns both array of results and total count query without offset and limit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to paginate results, we can use `qb.getResultAndCount` method. It returns both array of results and total count query without offset and limit | |
If we want to paginate the results of a QueryBuilder, we can use `qb.getResultAndCount()` method. It returns an ordered tuple, the first item being an array of results, and the second one being the total count of items, discarding the limit and offset clause. |
docs/docs/query-builder.md
Outdated
const qb = orm.em.createQueryBuilder(FooBar); | ||
qb.select('*') | ||
.where({ name: 'fb 1' }) | ||
.limit(10); | ||
const [results, count] = qp.getResultAndCount(); | ||
|
||
console.log(authors.length); // based on limit parameter, e.g. 10 | ||
console.log(count); // total count, e.g. 1327 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const qb = orm.em.createQueryBuilder(FooBar); | |
qb.select('*') | |
.where({ name: 'fb 1' }) | |
.limit(10); | |
const [results, count] = qp.getResultAndCount(); | |
console.log(authors.length); // based on limit parameter, e.g. 10 | |
console.log(count); // total count, e.g. 1327 | |
const qb = em.createQueryBuilder(User); | |
.select('*') | |
.where({ age: 18 }) | |
.limit(10); | |
const [results, count] = await qb.getResultAndCount(); | |
console.log(results.length); // max 10, as we used the limit clause | |
console.log(count); // total count regardless limit and offset, e.g. 1327 |
const [results, count] = await Promise.all ([ | ||
this.getResultList(), | ||
this.getCount(), | ||
]); | ||
return [results, count]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const [results, count] = await Promise.all ([ | |
this.getResultList(), | |
this.getCount(), | |
]); | |
return [results, count]; | |
return Promise.all([ | |
this.getResultList(), | |
this.getCount(), | |
]); |
db647f4
to
62592fc
Compare
PR fixed! Thanks for the review. 👾 @B4nan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking good, thanks!
Closes #3885