Cursor-Based Pagination

Cursor-based pagination provides a performant and reliable way to navigate large datasets, avoiding the limitations of offset-based pagination.

Understanding Cursor-Based Pagination

The cursor parameter enables efficient pagination by returning records that come after a specific unique or ordered field value. It is ideal for large datasets where traditional offset-based pagination may slow down.

  • Must be used together with orderBy to define sorting.
  • The cursor indicates the starting point for the next set of results.
  • Records with values greater than or equal to the cursor are returned.
  • skip defaults to 1 to avoid including the cursor item itself.

Example Usage: Cursor-Based Pagination

use Lib\Prisma\Classes\Prisma;

$prisma = Prisma::getInstance();
$users = $prisma->user->findMany([
    'orderBy' => [
        'id' => 'asc'
    ],
    'cursor' => [
        'id' => 100
    ],
    'take' => 10
]);

echo "<pre>";
print_r($users);
echo "</pre>";

    

Best Practices

  • Always pair cursor with orderBy for predictable ordering.
  • Prefer cursor pagination over offset-based when dealing with large datasets.
  • When paginating by non-unique fields, use a compound cursor (e.g., createdAt + id).