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(core): add getResultAndCount() #3891

Merged
merged 1 commit into from
Jan 4, 2023
Merged

Conversation

erie0210
Copy link
Contributor

@erie0210 erie0210 commented Jan 3, 2023

Closes #3885

@@ -294,6 +294,21 @@ qb.select('*').limit(10, 20).where({ $and: [{ id: { $nin: [3, 4] } }, { id: { $g
const count = await qb.getCount();
```

## Paginate queries
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## 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).
Copy link
Member

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

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines 302 to 309
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines 667 to 671
const [results, count] = await Promise.all ([
this.getResultList(),
this.getCount(),
]);
return [results, count];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [results, count] = await Promise.all ([
this.getResultList(),
this.getCount(),
]);
return [results, count];
return Promise.all([
this.getResultList(),
this.getCount(),
]);

@erie0210 erie0210 force-pushed the issues/3885 branch 2 times, most recently from db647f4 to 62592fc Compare January 4, 2023 15:00
@erie0210
Copy link
Contributor Author

erie0210 commented Jan 4, 2023

PR fixed! Thanks for the review. 👾 @B4nan

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good, thanks!

@B4nan B4nan merged commit 11956c8 into mikro-orm:master Jan 4, 2023
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.

feat: add getResultAndCount
3 participants