Skip to content

@xtandard/lib/filters/memory

Evaluates the compiled filter AST in memory: a plain (row) => boolean predicate with the same semantics as the SQL adapters, a sort comparator, and synchronous offset/cursor pagination over arrays. Zero dependencies; safe for the frontend.

buildFilterPredicate / buildFilterNodePredicate compile and render in one step, toFilterPredicate renders a pre-compiled CompiledWhere, applyFiltersToArray filters an array, buildComparator turns a Sort into an Array.prototype.sort comparator, and createMemoryKeyset / createMemoryPaginator page through the result. Matching follows SQL: three-valued NULL logic (ne/notInArray/notIlike never match NULL, not keeps UNKNOWN), LIKE patterns with %/_/\ escapes, and case folding, NULL ordering and NULL arrays per dialect ("postgres", the default, or "sqlite"). A cross-adapter test checks it returns the same rows as the knex, kysely and drizzle adapters on SQLite.

import { applyFiltersToArray, buildComparator, createMemoryPaginator, textField, numberField } from "@xtandard/lib/filters/memory";
const spec = { name: textField({ path: "name" }), amount: numberField({ path: "amount" }) };
const matching = applyFiltersToArray(rows, { spec, filters, resolveDate });
const { compare } = buildComparator({ sort, paths: { name: "name", amount: "amount" } });
const sorted = [...matching].sort(compare);
// or page it: createMemoryPaginator({ rows: matching, sort: [{ key: "amount" }, { key: "id" }] })
import { … } from "@xtandard/lib/filters/memory";
Name Summary
applyFiltersToArray Compile filters with buildFilterPredicate and return the rows that match, as a new array in their original order. The array counterpart of applyFiltersToKnex.
buildComparator Build an Array.prototype.sort comparator from a Sort, allow-listed to paths (public field name → MemoryAccessor). Unknown fields are dropped; when nothing is left, defaultSort is used instead (and with no keys at all every pair compares equal). Keys apply in order; ties keep the input order, since Array.prototype.sort is stable.
buildFilterNodePredicate Compile a recursive and/or/not FilterNode tree into a row predicate. Same allow-list, resolveDate and dialect rules as buildFilterPredicate.
buildFilterPredicate Compile a flat, AND-combined list of column filters into a row predicate. Only fields in spec whose kind matches the filter’s kind are kept; the rest are silently dropped, and when nothing survives the predicate matches every row. A date preset filter needs resolveDate (it throws without one). dialect (default "postgres") picks the SQL semantics to copy (see MemoryDialect).
createMemoryKeyset 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.
createMemoryPaginator Create a fully synchronous paginator over an in-memory array (createPaginator from @xtandard/lib/pagination with array fetchers): page/offset requests slice the sorted rows (and report totalItems), cursor requests seek with createMemoryKeyset, forward or backward. paginate returns a plain Paginated<Row> (no await), so it works in a render path. The rows are copied and sorted once, at creation; filter them first (for example with applyFiltersToArray).
toFilterPredicate Render an already-compiled CompiledWhere AST (from compileFilters / compileFilterNode in @xtandard/lib/filters) to a row predicate, looking each field up in paths (public field name → MemoryAccessor). Conditions on fields missing from paths are dropped; null (or an AST that drops to nothing) gives a predicate that matches every row. Evaluation uses SQL’s three-valued logic: a comparison with NULL is UNKNOWN, not keeps UNKNOWN, and a row matches only when the whole tree is TRUE. Most code calls buildFilterPredicate, which compiles and renders in one step.
Name Summary
MemoryKeyset The keyset helper returned by createMemoryKeyset.
Name Summary
FilterPredicate A row predicate, as returned by toFilterPredicate and buildFilterPredicate.
MemoryAccessor What a field reads from a row: a property key or dot path ("name", "address.city", like the mongo adapter’s path), or a getter function (like a raw expression in the kysely adapter). Accessors are server-owned: never build one from request data.
MemoryDialect The SQL engine whose semantics the in-memory evaluator copies (default "postgres", like the kysely/knex adapters’ dialect). It decides three things:
MemoryFieldSpec One allow-listed field: its filter kind plus the MemoryAccessor it reads.
MemoryFilterSpec A resource’s filter allow-list: public field name → MemoryFieldSpec.
NullsOrder Where NULLs go in a sort, whatever the direction (SQL NULLS FIRST / NULLS LAST).
RowComparator A row comparator for Array.prototype.sort.
Name Summary
arrayField Allow-list a public field as an array filter over a row value read by path that holds an array. arrayContains is Postgres @> (the row has every value), arrayContained is <@ (every row element is a value) and arrayOverlaps is && (at least one in common).
booleanField Allow-list a public field as a boolean filter (eq/ne/null checks) over a row value read by path.
dateField Allow-list a public field as a date filter over a row value read by path (a key, a dot path, or a getter). The row value may be a Date, an ISO-8601 string, epoch milliseconds, or any object with a numeric epochMilliseconds (Temporal.Instant, Temporal.ZonedDateTime). A date preset needs the resolveDate you pass to buildFilterPredicate, and becomes a half-open [start, end) window.
enumField Allow-list a public field as an enum filter (eq/ne/inArray/notInArray/null checks) over a row value read by path. As in SQL, ne and notInArray never match a NULL.
numberField Allow-list a public field as a number filter over a row value read by path (a key, a dot path, or a getter). between matches >= from and <= to; a bigint row value compares numerically.
textField Allow-list a public field as a text filter over a row value read by path (a key, a dot path, or a getter). contains/startsWith/endsWith match the input literally and ignore case; like/ilike/notIlike take a SQL pattern (%, _, \ escapes). How case is folded follows dialect (see MemoryDialect).