ponder-sh / ponder

A backend framework for crypto apps
https://ponder.sh
MIT License
530 stars 74 forks source link

Add totalCount field to plural connection type #954

Open jaydenwindle opened 3 weeks ago

jaydenwindle commented 3 weeks ago

This PR adds an additional field totalCount to the connection type for all plural queries. If the totalCount field is included in a query, an additional database call is made to fetch the total number of unique records matching the currently applied where filters. If the totalCount field is not queried, no additional database calls are made.

0xOlias commented 2 weeks ago

Hi, thanks for opening. I'm open to this addition, but have some ideas for how to tweak it:

jaydenwindle commented 2 weeks ago

Thanks for considering this!

We can definitely move totalCount into the Connection type, that makes a lot more sense.

I agree that the performance considerations should be mentioned in the docs. It seems like there may be a way to use CTEs to include both queries in a single database call without too much performance overhead. Happy to investigate this if performance is a big concern.

jaydenwindle commented 2 weeks ago

@0xOlias I have refactored the totalCount query logic to use CTEs instead of a second database call in order to mitigate the performance concerns. I've also moved the totalCount field to the connection type, renamed the findMany argument to withTotalCount, and updated the documentation (with a warning about performance implications).

Let me know if there are any other changes you would like me to make!