Skip to content

createPostgrestKeyset

function createPostgrestKeyset(input): PostgrestKeyset;

Defined in: filters/postgrest.ts:686

Build a keyset (cursor) helper from a sort spec + the PostgREST column each logical key maps to. The last sort column MUST be unique (a tiebreaker like id) so the cursor is stable. orderBy(direction) gives the order terms ("backward" flips every column) and where(cursor, direction) the lexicographic seek as PostgREST params ([] for the first page). Sort keys without a column in columns are skipped. Combine with buildParams output (PostgREST ANDs every param) and limit=pageSize + 1.

Parameter Type
input { columns: Record<string, string>; sort: KeysetSortColumn[]; }
input.columns Record<string, string>
input.sort KeysetSortColumn[]

PostgrestKeyset

import { createPostgrestKeyset, formatOrder } from "@xtandard/lib/filters/postgrest";
const keyset = createPostgrestKeyset({
sort: [{ key: "createdAt", order: "desc" }, { key: "id", order: "desc" }], // unique tiebreaker last
columns: { createdAt: "created_at", id: "id" },
});
formatOrder(keyset.orderBy()); // → "created_at.desc,id.desc"
keyset.where(null); // → [] (first page)
keyset.where({ createdAt: new Date("2026-06-18T00:00:00Z"), id: 9 });
// → [["or", '(created_at.lt."2026-06-18T00:00:00.000Z",and(created_at.eq."2026-06-18T00:00:00.000Z",id.lt.9))']]