Struggling to build common pattern with queries -- fetching with count + offset/limit pagination
Hi team! Love what you all are doing with Drizzle.
I have an extremely common pattern that I use in my application which is using Drizzle queries to create paginated results like so:
Now this works great for fetching rows -- the problem is for paginated results I want to also return the total count of rows, that the query returns. Not just the rows. I have managed to do this using the SQL API by creating a subquery, then fetching the count + the rows from that query and returning both. The problem is that when I use the query builder like above (which I prefer for complex nested objects) I don't know how to return the count or return the count before the limit and offset are applied. From what I've read in the docs, it seems like there is no way to do this with findMany and I have to refactor all my queries to use SQL syntax, am I correct?
Thanks!
4 Replies
👋 not sure if it's what you want but we can do that: https://drizzle.run/jm1gydsbyvvu9nbq1q4y16t1
@Raphaël M (@rphlmr) ⚡ this is perfect, you're amazing!
@Raphaël M (@rphlmr) ⚡ hey thanks for the tip -- I'm having a problem using this with Typescript. When adding the
with:
clause to a call to findMany
it automatically updates the Return type however the FindManyQueryConfig<typeof db.query.slots>
is defined as a slot return type so the with:
clause is ignored and thus the resulting type returned from the above getUsers
function is not typed correctly.
I see that it has to do with the <any, any> in but I'm not sure how to get that type dynamically.Hey 👋 thanks, I will fix it and report back!
Oh, when you define
with
in the baseConfig
.
You can try