Infinite scroll on Drizzle ORM not working
Please I haven't gotten the solution on how to solve the Cursor issue on Drizzle ORM. I was able to use a custom limit by using the Slice method on Javascript, however for the cursor scroll I still dont know how to add a cursor here:
If it were to be Prisma I'd have done it this way:
However, cursor and Pagination feel's strange on Drizzle ORM, here is full code below:
3 Replies
First off, you don’t want to use slice to apply the limit - that still means everything is returned from your DB, which you don’t want
The way to approach this is to have a where condition like
gt(_message.id, cursor)
and then use .limit
on the query builder
Or well, that would give you the cursor if you sort by increasing ID
If you’re supporting other sorts, you’d either need to construct different where clauses or you need to count how many items are before your last ID in your ordered query and then filter on that - you should be able to do that with a subquery
If you want to see how Prisma does it, you could try running the relevant Prisma query with sql query logging enabledThank you very much sir
Good evening @Luxaritas , I am very sorry to disturb you sir. I tried following your instruction yesterday however it went reloading the same data that was on the page instead of fetching a new 10 data, please help me see what I am doing wrong. Thanks so much!
Here is the redefined code:
Maybe it's because you're ordering by createdAt, but filtering by finding items with IDs greater than your cursor?
Your ordering and cusror filter should be the same
Though now that I'm looking at it even if that wasn't an issue, your filter is probably backwards - the first page you load should be the newest, but the IDs you're requesting are newer than your current page
If ID ordering and created ordering are the same, that means you eg return items 20 through 10, then request IDs greater than 10... which is still 20 through 10