Only run some queries if conditions changed.

I have these two queries that run one after another const facilitiesQuery = baseQuery.where(and(...conditions)); const dynamicQuery = facilitiesQuery.$dynamic(); const data = await withPagination(dynamicQuery, page, limit); const totalQuery = db.select({ total: count() }).from(facilities); const totalQueryConditions = totalQuery.where(and(...conditions)).$dynamic(); const total = await totalQueryConditions; first one gets paginated results and the second one is getting the total number of rows that match the condition (all the results, not paginated) My question, is there a way for me to skip the total count if no conditions have changed and retrurn the already calculated value. I was looking at the new cache() function by react, but would love to know the best approach to handle this? Thank you.
3 Replies
bhakta_jam
bhakta_jamOP2mo ago
also happy for any feedback that improves my implementation 🙏🏾
Winter
Winter2mo ago
It's a bit of a complex answer that is quite dependent on a myriad of different factors but generally what you'll notice in large scale applications is they don't show the total page options in their pagination instead they show something resembling First 1,2,3.. 10 Last where 10 is the page limit that it's recursed. If there's less than 10 it will be let's say 7. If you're wanting to go down this path, without introducing extra load on your servers, dependent on your architecture the generally accepted solution is to use Redis as your cache layer and through different caching patterns based on your needs you can solve it effectively.
bhakta_jam
bhakta_jamOP2mo ago
Thank you Winter, great input 👏🏻
Want results from more Discord servers?
Add your server