@xtandard/lib/filters
Filter model (types), portable compileFilters → CompiledWhere AST, sorting, URL codec and human-readable descriptions. Zero dependencies; safe for the frontend.
import { … } from "@xtandard/lib/filters";Functions
Section titled “Functions”| Name | Summary |
|---|---|
compactFilterNode |
Encode a recursive and/or/not tree to its compact (Rison-ready) form: kind is dropped, keys and operators are abbreviated, and connectives become a single key. Total — every valid FilterNode encodes. |
compactFilters |
Encode a flat AND list of column filters to compact leaves (kind dropped, keys and operators abbreviated). |
compileFilterNode |
Compile a recursive and/or/not tree into a CompiledWhere (or null). Non-allow-listed leaves are dropped the same way as in compileFilters; a group left with no children is dropped too, and a not around nothing disappears. |
compileFilters |
Compile a flat AND-combined list of column filters into a CompiledWhere (or null when nothing is allow-listed). spec is the per-field kind allow-list — a field not in spec, or whose filter kind doesn’t match, is dropped. A date preset needs resolveDate (it throws without one). |
createFilterUrlCodec |
Bind a field → kind allow-list once and get encode/decode pairs — sugar for composing with a URL-state codec (e.g. parseAsCodec(risonCodec(...))). |
describeColumnFilter |
<field> <description>, e.g. createdAt is between Jun 1 and Jun 30: the describeFieldFilter text prefixed by label (or the field name). |
describeFieldFilter |
A short, human description of a single field filter (no field name), for chips and summaries. A date preset uses describeDate when given; without it the label is just the preset’s operator (e.g. "is"). |
escapeLike |
Escape the LIKE/ILIKE wildcards (%, _) and the escape character itself (\) with a backslash, so a user’s text matches literally — used by the contains/startsWith/endsWith affordances. Backslash is Postgres’s default LIKE escape; the kysely/knex adapters add an ESCAPE clause on SQLite (no default escape) and MySQL (none under NO_BACKSLASH_ESCAPES). Using it yourself on SQLite, add ESCAPE '\' to your LIKE. |
expandFilterNode |
Decode a compact tree back to a FilterNode, restoring kind from kinds (a field → kind allow-list, e.g. from _metadata). Returns null if nothing allow-listed survives. |
expandFilters |
Decode compact leaves back to a flat FiltersRequest (non-allow-listed / malformed leaves are dropped), restoring kind from kinds. A non-array compact decodes to []. |
operatorFromCode |
Short URL code → model operator (inverse of OPERATOR_CODE), or null for an unknown code. |
parseSortParam |
Sort helpers (pure, no validation library). The Sort model lives in ./types.ts; the valibot schema is in @xtandard/lib/filters/valibot. Field names are public — the per-adapter column allow-list maps them to columns, so a client can never sort by a column that isn’t declared sortable. |
resolveFieldOptions |
Resolve FieldOptions to its FieldOption[] — calling the getter (with query) or returning the static list. Stays the color of its input: a static list or sync getter resolves synchronously (no microtask), an async getter yields a Promise. await works on either. |
serializeSort |
Serialize the model back to the compact query form (field:dir, comma separated) — the inverse of parseSortParam. |
sqlTextOp |
Lower a semantic text-match op to a SQL LIKE/ILIKE (op, pattern) — shared by the SQL adapters (drizzle/kysely/knex). contains/startsWith/ endsWith become ilike with an escaped % pattern; like/ilike/ notIlike pass the pattern through verbatim. |
unitFromCode |
Short URL code → date unit (inverse of UNIT_CODE), or null for an unknown code. |
Interfaces
Section titled “Interfaces”| Name | Summary |
|---|---|
FieldMetadata |
|
FieldOption |
|
OptionsQuery |
What an options getter receives when searching/resolving (a combobox). |
ResourceCrud |
|
ResourceMetadata |
Type aliases
Section titled “Type aliases”| Name | Summary |
|---|---|
AnyOperator |
Any filter operator, across every field kind (text, number, enum, boolean, date, array). |
ArrayFilter |
|
ArrayOperator |
|
BooleanFilter |
|
BooleanOperator |
|
ColumnFilter |
A FieldFilter bound to a public field name (the allow-list key). |
CompactLeaf |
A flattened, kind-less column filter: { f, o, …args }. |
CompactNode |
A compact and/or/not tree — connectives are a single key. |
CompiledCond |
|
CompiledOp |
|
CompiledWhere |
The portable WHERE AST: an and/or/not tree of normalized conditions. |
DateFilter |
|
DateFilterResolver |
Resolve a date-preset filter to half-open [start, end) instant bounds. |
DateOperator |
|
DatePreset |
The DST-aware period preset (resolved to a half-open instant window by an injected resolver — see the adapters’ resolveDate). anchor/end are canonical Temporal.PlainDateTime strings; timeZone an IANA id. |
DateUnit |
|
DescribeDate |
Render a date-preset filter to text. Supply via describeFieldFilter. |
EnumFilter |
|
EnumOperator |
|
FieldFilter |
One field filter — a discriminated union on kind (the column data kind), each kind a sub-union on operator (the argument shape). Narrow by kind then by an operator/property check ("value" in f, "from" in f, …). |
FieldKind |
|
FieldKindSpec |
|
FieldOptions |
A field’s options: a static list, or a (sync OR async) getter that receives an OptionsQuery — so options can come from a DB query/request. |
FilterNode |
Recursive and/or/not tree of column filters. |
FiltersRequest |
Flat AND-combined list of column filters (the common case). |
NumberFilter |
|
NumberOperator |
|
PaginationStyle |
A wire contract a backend can expose at …/_metadata and a FRONTEND can render from — no field defs hand-written on the client. The backend resource definition is the single source of truth; this is its serializable projection (FE-safe, no drizzle). |
ScalarValue |
|
Sort |
An ordered list of { field, dir } (applied in order; allow-listed per adapter). |
SortDirection |
|
SortItem |
|
SqlDialect |
SQL dialect for the SQL adapters (kysely/knex). Affects the ops with no portable spelling (the array ops and case-insensitive ilike), the LIKE ESCAPE clause, and knex’s identifier quoting (backticks on MySQL). postgres (default) uses native @>/<@/&& + ILIKE; mysql renders the array ops as JSON_CONTAINS/JSON_OVERLAPS over JSON columns and lowers ilike → LIKE (case-insensitive by collation); sqlite renders the array ops as json_each(…) EXISTS/NOT EXISTS subqueries and lowers ilike → LIKE (case-insensitive ASCII). MySQL needs JSON-typed columns (8.0.17+ for JSON_OVERLAPS); SQLite needs the JSON1 extension (bundled since 3.38). |
TextFilter |
|
TextMatchOp |
Text-matching ops kept SEMANTIC (not lowered) so each adapter renders them natively: SQL → ilike/escape, Mongo → $regex, Prisma → contains/mode. |
TextOperator |
|
UnaryOperator |
Variables
Section titled “Variables”| Name | Summary |
|---|---|
ARRAY_COL_OPERATORS |
The array-column operators — arrayContains / arrayContained / arrayOverlaps compare an array column against values[] (the array kind; Postgres @> / <@ / &&, or a SQL adapter’s dialect equivalent). |
ARRAY_OPERATORS |
Every operator the array kind accepts (array-column + unary), for a UI operator picker. Same list as OPERATORS_BY_KIND.array. |
BOOLEAN_OPERATORS |
Every operator the boolean kind accepts (equality + unary), for a UI operator picker. Same list as OPERATORS_BY_KIND.boolean. |
DATE_OPERATORS |
Every operator the date kind accepts (period presets + unary), for a UI operator picker. Same list as OPERATORS_BY_KIND.date. |
DATE_PRESET_OPERATORS |
The date kind’s period-preset operators. Each takes unit + timeZone + anchor (+ end for between) and is resolved to a half-open instant window by an injected resolveDate. |
ENUM_OPERATORS |
Every operator the enum kind accepts (equality + set + unary), for a UI operator picker. Same list as OPERATORS_BY_KIND.enum. |
EQUALITY_OPERATORS |
The equality operators eq / ne — the single-value operators of the enum and boolean kinds. |
NUMBER_OPERATORS |
Every operator the number kind accepts (scalar + range + set + unary), for a UI operator picker. Same list as OPERATORS_BY_KIND.number. |
NUMBER_SCALAR_OPERATORS |
The single-value comparison operators the number kind accepts. |
OPERATOR_CODE |
Model operator → short URL code (the inverse is operatorFromCode). The compact codec writes these into each leaf’s o key. |
OPERATORS_BY_KIND |
The operator vocabulary a field of each kind supports (for UI operator pickers and FieldMetadata.operators). Each entry is the matching *_OPERATORS constant (text → TEXT_OPERATORS, …). |
RANGE_OPERATORS |
The range operators — between / notBetween take from + to (the number kind only). compileFilters lowers them to gte AND lte / lt OR gt, never SQL BETWEEN. |
SET_OPERATORS |
The set operators — inArray / notInArray take a values[] list (text, number and enum kinds). |
TEXT_OPERATORS |
Every operator the text kind accepts (scalar + set + unary), for a UI operator picker. Same list as OPERATORS_BY_KIND.text. |
TEXT_SCALAR_OPERATORS |
The single-value operators the text kind accepts: equality plus the text matchers. contains/startsWith/endsWith take a plain string (the SQL adapters escape it); like/ilike/notIlike take a raw pattern. |
UNARY_OPERATORS |
The unary operators — isNull / isNotNull take no argument. Every kind accepts them. |
UNIT_CODE |
Date unit → short URL code (the inverse is unitFromCode). The compact codec writes these into a date-preset leaf’s u key. |