Skip to content

@xtandard/lib/filters/knex

Renders the compiled filter AST to raw parameterized SQL for Knex (no driver dependency). applyFiltersToKnex applies it via whereRaw; buildWhereSql returns the { sql, bindings }.

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, and MySQL identifiers are quoted with backticks. There is no knex import: any object with whereRaw(sql, bindings) works, so Knex stays your app’s dependency. Column identifiers are server-owned and validated; values are bound (?).

import { applyFiltersToKnex, textField, numberField } from "@xtandard/lib/filters/knex";
const spec = { name: textField({ column: "name" }), amount: numberField({ column: "amount" }) };
const rows = await applyFiltersToKnex(knex("tasks"), { spec, filters, resolveDate });
// or: const { sql, bindings } = buildWhereSql({ spec, filters }) ?? {}; query.whereRaw(sql, bindings);
import { … } from "@xtandard/lib/filters/knex";
Name Summary
applyFiltersToKnex Compile filters with buildWhereSql and add the result to a Knex query builder with query.whereRaw(sql, bindings). When no filter survives the allow-list, whereRaw is not called. Returns the same query for chaining. Works with any object that has a whereRaw(sql, bindings) method, so this module never imports knex.
buildFilterNodeSql Compile a recursive and/or/not FilterNode tree into a parameterized { sql, bindings } fragment (or null). Same allow-list and dialect rules as buildWhereSql.
buildWhereSql Compile a flat, AND-combined list of column filters into a parameterized { sql, bindings } fragment for Knex’s whereRaw (or any driver that takes ? placeholders). Only fields in spec whose kind matches the filter’s kind are kept; the rest are silently dropped. Returns null when nothing survives. A date preset filter needs resolveDate (it throws without one). dialect (default "postgres") changes the array ops, ilike, the LIKE escape clause and identifier quoting (backticks on MySQL).
toFilterWhereSql Render an already-compiled CompiledWhere AST (from compileFilters / compileFilterNode in @xtandard/lib/filters) to a parameterized { sql, bindings } fragment, looking each field up in columns (column strings, validated and quoted). Conditions on fields missing from columns are dropped; null (or an AST that drops to nothing) gives null. Most code calls buildWhereSql, which compiles and renders in one step.
Name Summary
Fragment A rendered SQL fragment with its positional ? bindings.
KnexFieldSpec One allow-listed field: its filter kind plus the SQL column (col or table.col) it reads.
KnexFilterQuery Minimal structural Knex query (no knex import).
KnexFilterSpec A resource’s filter allow-list: public field name → KnexFieldSpec.
Name Summary
arrayField Allow-list a public field as an array filter over a column named by a column string ("tags" or "posts.tags"). 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 named by a column string ("done" or "tasks.done").
dateField Allow-list a public field as a date filter over a timestamp column, named by a column string ("created_at" or "posts.created_at", quoted per part; anything else throws). A date preset needs the resolveDate you pass to buildWhereSql, 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 named by a column string ("status" or "tasks.status").
numberField Allow-list a public field as a number filter over a numeric column, named by a column string ("total" or "orders.total"). between renders as >= ? AND <= ?.
textField Allow-list a public field as a text filter over a string column, named by a column string ("name" or "users.name", quoted per part; anything else throws). 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).