PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 2, 2022

[FIXED] How to specify cursor pagination using timestamp in Prisma?

 September 02, 2022     database, pagination, prisma     No comments   

Issue

I want to use createdAt as cursor, but prisma's type only allow id field:

post.findMany({
      take: 10,
      orderBy: { createdAt: 'desc' },
      cursor: { id }, // Prisma only provide id here
    });

And cursor is refering to the type "postWhereUniqueInput", it only provide id in it:

  export type postWhereUniqueInput = {
    id?: number
  }

Here's the data model:

model post {
  id             Int            @id() @default(autoincrement())
  createdAt      DateTime       @default(now()) @db.Timestamp(6)
  updatedAt      DateTime       @default(now()) @db.Timestamp(6)
  title          String         @db.VarChar
  content        String?        @db.VarChar
  userId         Int  
  viewCount      Int            @default(0)  
  votePoints     Int            @default(0)
  likePoints     Int            @default(0)
  confusedPoints Int            @default(0)
  laughPoints    Int            @default(0)
  user           user           @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: NoAction)
  interactions   interactions[]
  comments       comments[]
}

How can I use the timestamp as cursor? Thank you so much for the reply!


Solution

Solution

By adding @unique() attribute:

model post {
  id             Int            @id() @default(autoincrement())
  createdAt      DateTime       @unique() @default(now())
  updatedAt      DateTime       @unique() @updatedAt
  ...
}

Then I can use cursor createdAt:

prismaService.post.findMany({
      cursor: cursor ? { createdAt: cursor } : undefined,
      ...
    });


Answered By - Enfield li
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing