Skip to content

Latest commit

 

History

History
66 lines (51 loc) · 1.15 KB

05-pagination.md

File metadata and controls

66 lines (51 loc) · 1.15 KB

Pagination

The examples use the following prisma schema:

model Post {
    id        String   @id @default(cuid())
    createdAt DateTime @default(now())
    updatedAt DateTime @updatedAt
    published Boolean
    title     String
    content   String?

    comments Comment[]
}

model Comment {
    id        String   @id @default(cuid())
    createdAt DateTime @default(now())
    content   String

    post   Post   @relation(fields: [postID], references: [id])
    postID String
}

Return the first 5 rows

selected, err := client.
    Post.
    FindMany().
    Take(5).
    Exec(ctx)

Return the first 5 rows and skip 2 rows

selected, err := client.
    Post.
    FindMany().
    Take(5).
    Skip(2).
    Exec(ctx)

Cursor-based pagination

Instead of using Skip, you can also provide a cursor:

selected, err := client.
    Post.
    FindMany().
    Take(5).
    Skip(2).
    Cursor(db.Post.ID.Cursor("abc")).
    Exec(ctx)

Also check out the order by docs to understand how you can combine cursor-based pagination with order by.

Next steps

Learn how to order queries.