Skip to content

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.

Parameter Type
options CreateKeysetSpecOptions

KeysetSpec

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 },
// ] },
// ] }

If sort is empty.