How to mimic the prisma cursor option for use with useInfiniteQuery

How to mimic the cursor option in drizzle with MySQL? This is the Prisma documentation: https://www.prisma.io/docs/concepts/components/prisma-client/pagination#cursor-based-pagination
1 Reply
Angelelz
Angelelz14mo ago
Cursors are just queries with a limit You'll have to keep track of the last seen ID, because that'll need to provide in subsecuent queries to get the next batch of results for example:
const cursorUsers = db
.select()
.from(users)
.where(and([
sql`1=1`,
cursorId ? gt(users.id, cursorId) : undefined. // <-- If cursorId is provided, get all the users with a id higher than that
]))
.limit(10)

const cursorId = cursorUsers.at(-1).id <-- this would be the last id seen from the query
const cursorUsers = db
.select()
.from(users)
.where(and([
sql`1=1`,
cursorId ? gt(users.id, cursorId) : undefined. // <-- If cursorId is provided, get all the users with a id higher than that
]))
.limit(10)

const cursorId = cursorUsers.at(-1).id <-- this would be the last id seen from the query
Want results from more Discord servers?
Add your server