Understanding Cursor-Based Pagination
The cursor parameter enables efficient pagination by returning records that come after a specific unique or ordered field value. This is particularly useful for large datasets where traditional offset-based pagination can become slow or unreliable.
- Must be used in combination with
orderByto define the sorting field. - The
cursorarray should include the field(s) and their corresponding value(s) as the starting point. - By default, records with values greater than or equal to the cursor value will be returned.
- If
skipis not explicitly defined, it will default to 1 to avoid returning the cursor record 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
cursorwithorderByfor predictable results. - Prefer cursor-based pagination over offset-based (
skip) for large datasets. - If paginating by a non-unique field (e.g.,
createdAt), consider using a compound cursor with a unique fallback likeid.