question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `findManyAndCount` to return count of queried items

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:161
  • Comments:23 (1 by maintainers)

github_iconTop GitHub Comments

45reactions
felinto-devcommented, May 16, 2022

I have a suggestion for the syntax that could be used to follow the same convention that prisma has followed since then:

const users = await prisma.user.findMany({
    _count: true,
})

28reactions
felinto-devcommented, May 5, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma find many and count in one request - Stack Overflow
I have a pagination in my category service, and I have to return obj with total count of categories and data.
Read more >
Aggregation, grouping, and summarizing (Concepts) - Prisma
Prisma Client allows you to count records, aggregate number fields, and select distinct field ... The following query returns the average age of...
Read more >
How does one implement Prisma's features like count ...
I tried using db.posts.count() and it worked once I made changes to my ... a query that returns a list of posts that...
Read more >
Getting Counts on Relations with Prisma Client - YouTube
New at Prisma 2.20 is the ability to get the counts of related fields. This is useful for cases where you'd like to...
Read more >
A technique to count anything in Power Query and ... - YouTube
In todays video I will show you a technique to count any text in a cell in Power query and DAX.Here you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found