Skip to content

@xtandard/lib/filters/kysely

Renders the compiled filter AST to a Kysely boolean expression (peer kysely). Validation-library-free.

Dialect-aware (dialect, default "postgres"): the array ops and ilike are spelled per dialect: PG native @>/<@/&& + ILIKE, MySQL JSON_CONTAINS/JSON_OVERLAPS + LIKE, SQLite json_each(…) subqueries + LIKE (see {@link SqlDialect}). On MySQL/SQLite, like gets an escape clause so the escaped %/_/\ in contains/startsWith/ endsWith match literally. String columns are passed to sql.ref (server-owned identifiers); computed values use the sql tag.

import { buildWhere, buildOrderBy, textField, dateField } from "@xtandard/lib/filters/kysely";
const spec = { name: textField({ column: "posts.name" }), createdAt: dateField({ column: "posts.created_at" }) };
const { where } = buildWhere({ spec, filters, resolveDate });
let q = db.selectFrom("posts").selectAll();
if (where) q = q.where(where);
for (const o of buildOrderBy({ sort, columns: { name: "posts.name" } }).orderBy) q = q.orderBy(o);
import { … } from "@xtandard/lib/filters/kysely";
Name Summary
buildFilterNode Compile a recursive and/or/not FilterNode tree into a Kysely boolean expression (or undefined). Same allow-list and dialect rules as buildWhere.
buildOrderBy Render a Sort to Kysely orderBy expressions, allow-listed to columns (public field name → KyselyTarget). Unknown fields are dropped; when nothing is left, defaultSort is used instead. Apply each expression with query.orderBy(expr).
buildWhere Compile a flat, AND-combined list of column filters into a Kysely boolean expression for .where(where). Only fields in spec whose kind matches the filter’s kind are kept; the rest are silently dropped. where is undefined when nothing survives (skip the .where call then). A date preset filter needs resolveDate (it throws without one). dialect (default "postgres") changes the array ops, ilike and the LIKE escape clause.
toKyselyWhere Render an already-compiled CompiledWhere AST (from compileFilters / compileFilterNode in @xtandard/lib/filters) to a Kysely boolean expression, looking each field up in columns. Conditions on fields missing from columns are dropped; null (or an AST that drops to nothing) gives undefined. Most code calls buildWhere, which compiles and renders in one step.
Name Summary
KyselyFieldSpec One allow-listed field: its filter kind plus the KyselyTarget it reads.
KyselyFilterSpec A resource’s filter allow-list: public field name → KyselyFieldSpec.
KyselyTarget What a field reads: a column reference string ("name", "posts.name", passed to sql.ref) or any Kysely raw expression (e.g. sql`lower(name)`).
Name Summary
arrayField Allow-list a public field as an array filter over a column given as a Kysely reference string ("posts.tags") or a raw expression. The SQL depends on dialect: a native array with @>/<@/&& on Postgres, a JSON column with json_contains/json_overlaps on MySQL, and json_each(…) subqueries on SQLite.
booleanField Allow-list a public field as a boolean filter (eq/ne/null checks) over a column given as a Kysely reference string ("tasks.done") or a raw expression.
dateField Allow-list a public field as a date filter over a timestamp column, given as a Kysely reference string ("posts.created_at") or a raw expression. A date preset needs the resolveDate you pass to buildWhere, 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 column given as a Kysely reference string ("tasks.status") or a raw expression.
numberField Allow-list a public field as a number filter over a numeric column, given as a Kysely reference string ("orders.total") or a raw expression. between renders as >= from and <= to.
textField Allow-list a public field as a text filter over a string column, given as a Kysely reference string ("users.name") or a raw expression such as sql`lower(users.name)`. contains/startsWith/endsWith render as ilike on Postgres and like on MySQL/SQLite, with the input’s %/_/\ escaped (plus an escape clause on MySQL/SQLite, so they match literally).