createKeysetSpec
function createKeysetSpec(options): KeysetSpec;Defined in: keyset.ts:246
Build a portable keyset pagination spec from sort columns. Returns a
structured WHERE AST and ORDER BY keys that ORM adapters (Drizzle,
Kysely, Prisma, Knex, raw SQL) translate into native queries.
sort describes the natural order consumers should see. For backward
pagination, KeysetSpec.orderBy flips each sort direction so fetchers
can query in the opposite direction; createPaginator reverses those rows
back to natural order before returning them.
The last sort key must be unique (usually id) to avoid skipped or
duplicated rows at page boundaries.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options |
CreateKeysetSpecOptions |
Returns
Section titled “Returns”Example
Section titled “Example”const keyset = createKeysetSpec({ sort: [ { key: "createdAt", order: "asc" }, { key: "id", order: "asc" }, ],});
keyset.where({ createdAt: "2024-01-01", id: 42 }, "forward");// { or: [// { and: [{ key: "createdAt", op: "gt", value: "2024-01-01" }] },// { and: [// { key: "createdAt", op: "eq", value: "2024-01-01" },// { key: "id", op: "gt", value: 42 },// ] },// ] }Throws
Section titled “Throws”If sort is empty.