Skip to content

createMemoryKeyset

function createMemoryKeyset<Row>(input): MemoryKeyset<Row>;

Defined in: filters/memory.ts:792

Build a keyset (cursor) helper over in-memory rows from a sort spec (the same { key, order }[] as createKeysetSpec, whose seek AST it evaluates). Each key reads paths[key], or the key itself as a path. The last key MUST be unique (a tiebreaker like id) so the cursor is stable.

The seek compares in the comparator’s own total order, NULLs included (ranked per dialect or nulls, as in buildComparator), so rows with a NULL sort key page correctly — unlike SQL’s >, which never matches NULL.

Type Parameter Default type
Row any
Parameter Type
input { dialect?: MemoryDialect; nulls?: NullsOrder; paths?: Record<string, MemoryAccessor<Row>>; sort: KeysetSortColumn[]; }
input.dialect? MemoryDialect
input.nulls? NullsOrder
input.paths? Record<string, MemoryAccessor<Row>>
input.sort KeysetSortColumn[]

MemoryKeyset<Row>

import { createMemoryKeyset } from "@xtandard/lib/filters/memory";
const rows = [{ id: 3, at: 10 }, { id: 1, at: 20 }, { id: 2, at: 10 }];
const keyset = createMemoryKeyset({ sort: [{ key: "at", order: "desc" }, { key: "id", order: "asc" }] });
const sorted = [...rows].sort(keyset.compare());
sorted.map((r) => r.id); // → [1, 2, 3]
const after = keyset.where(keyset.fromItem(sorted[0]!))!; // seek past the first row
sorted.filter(after).map((r) => r.id); // → [2, 3]
keyset.where(null); // → undefined (first page)