Overview

Prisma Client supports both offset pagination and cursor-based pagination.

Offset pagination

Offset pagination uses skip and take to skip a certain number of results and select a limited range. The following query skips the first 3 Post records and returns records 4 - 7:

1const results = await prisma.post.findMany({
2 skip: 3,
3 take: 4,
4})

offset skip take

To implement pages of results, you would just skip the number of pages multiplied by the number of results you show per page.

✔ Pros of offset pagination

  • You can jump to any page immediately. For example, you can skip 200 records and take 10 , which simulates jumping straight to page 21 of the result set (the underlying SQL uses OFFSET). This is not possible with cursor-based pagination.
  • You can paginate the same result set in any sort order. For example, you can jump to page 21 of a list of User records sorted by first name. This is not possible with cursor-based pagination, which requires sorting by a unique, sequential column.

✘ Cons of offset pagination

  • Offset pagination does not scale at a database level. For example, if you skip 200,000 records and take the first 10, the database still has to traverse the first 200,000 records before returning the 10 that you asked for - this negatively affects performance.

Use cases for offset pagination

  • Shallow pagination of a small result set. For example, a blog interface that allows you to filter Post records by author and paginate the results.

Example: Filtering and offset pagination

The following query returns all records where the email field contains prisma.io. The query skips the first 40 records and returns records 41 - 50.

1const results = await prisma.post.findMany({
2 skip: 40,
3 take: 10,
4 where: {
5 email: {
6 contains: 'prisma.io',
7 },
8 },
9})

Example: Sorting and offset pagination

The following query returns all records where the email field contains Prisma, and sorts the result by the title field. The query skips the first 200 records and returns records 201 - 220.

1const results = prisma.post.findMany({
2 skip: 200,
3 take: 20,
4 where: {
5 title: {
6 contains: 'Prisma',
7 },
8 },
9 orderBy: {
10 title: 'desc',
11 },
12})

Cursor-based pagination

Cursor-based pagination uses cursor and take to return a limited set of results before or after a given cursor. A cursor bookmarks your location in a result set and must be a unique, sequential column - such as an ID or a timestamp.

The following example returns the first 4 Post records that contain the word "Prisma" and saves the ID of the last record as myCursor:

Note: Since this is the first query, there is no cursor to pass in.

1const firstQueryResults = prisma.post.findMany({
2 take: 4,
3 where: {
4 title: {
5 contains: 'Prisma' /* Optional filter */,
6 },
7 },
8 orderBy: {
9 id: 'asc',
10 },
11})
12
13// Bookmark your location in the result set - in this
14// case, the ID of the last post in the list of 4.
15
16const lastPostInResults = firstQueryResults[3] // Remember: zero-based index! :)
17const myCursor = lastPostInResults.id // Example: 29

The following diagram shows the IDs of the first 4 results - or page 1. The cursor for the next query is 29:

cursor 1

The second query returns the first 4 Post records that contain the word "Prisma" after the supplied cursor (in other words - IDs that are larger than 29):

1const secondQueryResults = prisma.post.findMany({
2 take: 4,
3 skip: 1, // Skip the cursor
4 cursor: {
5 id: myCursor,
6 },
7 where: {
8 title: {
9 contains: 'Prisma' /* Optional filter */,
10 },
11 },
12 orderBy: {
13 id: 'asc',
14 },
15})
16
17const lastPostInResults = secondQueryResults[3] // Remember: zero-based index! :)
18const myCursor = lastPostInResults.id // Example: 52

The following diagram shows the first 4 Post records after the record with ID 29. In this example, the new cursor is 52:

cursor 2

FAQ

Do I always have to skip: 1?

If you do not skip: 1, your result set will include your previous cursor. The first query returns four results and the cursor is 29:

cursor 1

Without skip: 1, the second query returns 4 results after (and including) the cursor:

cursor 3

If you skip: 1, the cursor is not included:

cursor 2

You can choose to skip: 1 or not depending on the pagination behavior that you want.

Can I guess the value of the cursor?

If you guess the value of the next cursor, you will page to an unknown location in your result set. Although IDs are sequential, you cannot predict the rate of increment (2, 20, 32 is more likely than 1, 2 3, particularly in a filtered result set).

Does cursor-based pagination use the concept of a cursor in the underlying database?

No, cursor pagination does not use cursors in the underlying database (e.g. PostgreSQL).

✔ Pros of cursor-based pagination

  • Cursor-based pagination scales. The underlying SQL does not use OFFSET, but instead queries all Post records with an ID greater than the value of cursor.

✘ Cons of cursor-based pagination

  • You must sort by your cursor, which has to be a unique, sequential column.
  • You cannot jump to a specific page using only a cursor. For example, you cannot accurately predict which cursor represents the start of page 400 (page size 20) without first requesting pages 1 - 399.

Use cases for cursor-based pagination

  • Infinite scroll - for example, sort blog posts by date/time descending and request 10 blog posts at a time.
  • Paging through an entire result set in batches - for example, as part of a long-running data export.

Example: Filtering and cursor-based pagination

1const secondQuery = prisma.post.findMany({
2 take: 4,
3 cursor: {
4 id: myCursor,
5 },
6 where: {
7 title: {
8 contains: 'Prisma' /* Optional filter */,
9 },
10 },
11 orderBy: {
12 id: 'asc',
13 },
14})

Sorting and cursor-based pagination

Cursor-based pagination requires you to sort by a sequential, unique column such as an ID or a timestamp. This value - known as a cursor - bookmarks your place in the result set and allows you to request the next set.

Example: Paging backwards with cusor-based pagination

To page backwards, set take to a negative value. The following query returns 4 Post records with an id of less than 200, excluding the cursor:

1const myOldCursor = 200
2
3const firstQueryResults = prisma.post.findMany({
4 take: -4,
5 skip: 1,
6 cursor: {
7 id: myOldCursor,
8 },
9 where: {
10 title: {
11 contains: 'Prisma' /* Optional filter */,
12 },
13 },
14})
Edit this page on Github