Theo's Typesafe CultTTC
Theo's Typesafe Cult4y ago
66 replies
amanuel

How to implement cursor-based infinite scrolling with prisma + useInfinitequery

How to implement cursor based infinite scrolling in this scenario?
Imagine I have this schema:

model MockUser {
  id         String   @id @default(cuid())
  name       String?
  hourlyRate Decimal?
  rating     Decimal?
}


I want users to have random ID's for other purposes. Cursor based infinite scrolling requires a sequential and unique id of some sort in table. What is the best way to go about this? It feels a bit weird to add an extra column for that, but I seen no other way.
For example:

model MockUser {
  id         String   @id @default(cuid())
  name       String?
  hourlyRate Decimal?
  rating     Decimal?
  myCursor   Int    @id @default(autoincrement())
}

Only one id can be used though, so what should I do here?

Possible solution?
model MockData {
  id           String        @id @default(cuid())
  name         String?
  hourlyRate   Decimal?
  rating       Decimal?
  MockDataList MockDataList?
}

model MockDataList {
  id         Int      @id @default(autoincrement())
  mockDataId String   @unique
  mockData   MockData @relation(fields: [mockDataId], references: [id])

  @@index([mockDataId])
}
Was this page helpful?