IssueRaiser

Add `findManyAndCount()` to return count of queried items

In 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.

Posted by julianflapper on Jun 9, 2021, 6:30 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

ux (E-commerce)

Add `findManyAndCount()` to return count of queried items

julianflapperJun 9, 2021, 6:30 AM

In 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.

Comments (0)

No comments yet. Start the discussion.

Comment on this issue