Add `findManyAndCount` to return count of queried items
See original GitHub issueIn order to properly perform pagination, one has to know the total count of items.
For example, in a list of 1000 users paged in groups of 100, you have to know that the last page is at skip 900.
This can easily be done in a separate query call using .count
However, when filtering the data, this counting becomes more difficult.
Say each user
has a profile
, and each profile has an address
, and I want to get all users living in city X.
This requires two joins and filtering by where
.
Count is still relevant here, because it may well be hundres of users in one city, still requiring pagination.
It’s not possible to use .length
on the data, because it is already shortened to 100 items by take
Using a separate .count() call adds a lot of repeated code and overhead since the query is basically performed twice.
But,
This can be fixed by returning the count object on findMany along with the data, such as TypeOrm does it:
const [users, count] = await this._userRepository.findAndCount({ query options })
Alternatives
Using the .count() function seperately, but as mentioned above, this is not very efficient.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:161
- Comments:23 (1 by maintainers)
Top GitHub Comments
I have a suggestion for the syntax that could be used to follow the same convention that prisma has followed since then:
Could you guys stop spamming this thread?
If you would support this feature, use reactions buttons, subscribe to this thread, give suggestions, or create a PR but spamming does not help at all.