@xtandard/lib/filters/drizzle-rqb
Renders the compiled filter AST to a Drizzle relational-query where object for db.query (no runtime dependency).
That is what db.query.<table>.findMany accepts, not a SQL for db.select().where()
(that’s the sibling @xtandard/lib/filters/drizzle). Two flavors:
- RQB v2 (drizzle v1,
db.query):whereis a plain object:buildWhere/buildFilterNode/toDrizzleRqbWhere. Columns are referenced by their schema property key, so drizzle resolves them against the (aliased) root table itself, sidestepping the “invalid reference to FROM-clause entry” footgun of raw column SQL. - RQB v1 (legacy,
db._query/ drizzle 0.xdb.query):whereis a callback(fields, operators) => SQL:buildRqbV1Where/buildRqbV1FilterNode/toDrizzleRqbV1Callback. It builds with theoperatorsandfieldsdrizzle passes in, so column refs resolve against the RQB’s aliased table too. On a SQLite or MySQL column (detected from the column), text matches render aslike … escapeinstead ofilike; thearrayops are Postgres@>/<@/&&built with drizzle’ssqltag.
Neither imports drizzle-orm (no peer). Operator mapping (1:1 with RQB v2):
eq/ne/gt/gte/lt/lte; set → in/notIn; text match → like/ilike/notIlike
(contains/startsWith/endsWith lower to ilike with an escaped % pattern);
between/notBetween are lowered upstream to gte+lte / lt+gt;
isNull/isNotNull → { isNull: true }/{ isNotNull: true }; scalar array →
arrayContains/arrayContained/arrayOverlaps; tree → AND/OR/NOT.
The RQB v2 object’s text matches are Postgres-only: a v2 column filter takes a bare
pattern (no ESCAPE), and drizzle renders its ilike key as ilike, which SQLite and
MySQL don’t have. On those databases use the v1 callback or @xtandard/lib/filters/drizzle.
NoInfer).
import { buildWhere, textField, dateField } from "@xtandard/lib/filters/drizzle-rqb";
// `column` is the RQB v2 schema PROPERTY key (e.g. "createdAt"), not the SQL column name.const spec = { name: textField({ column: "name" }), createdAt: dateField({ column: "createdAt" }) };const { where } = buildWhere({ spec, filters, resolveDate });const rows = await db.query.tasks.findMany({ where, limit: 20 });import { … } from "@xtandard/lib/filters/drizzle-rqb";Functions
Section titled “Functions”| Name | Summary |
|---|---|
buildFilterNode |
Compile a recursive and/or/not FilterNode tree into an RQB v2 where object (or undefined). Same allow-list rules as buildWhere. |
buildRqbV1FilterNode |
Compile a recursive and/or/not FilterNode tree into an RQB v1 where callback. Same allow-list rules as buildRqbV1Where. |
buildRqbV1Where |
Compile a flat, AND-combined list of column filters into an RQB v1 where callback for the legacy relational API (db._query in drizzle v1, db.query in drizzle 0.x). The callback builds the SQL from the fields and operators drizzle passes in, so columns resolve against the query’s aliased table. Same allow-list rules as buildWhere; when no filter survives, the callback returns undefined. array filters render as Postgres @>/<@/&& (see RqbV1Operators). |
buildWhere |
Compile a flat, AND-combined list of column filters into an RQB v2 where object for db.query.<table>.findMany({ where }) (drizzle v1). Only fields in spec whose kind matches the filter’s kind are kept; the rest are silently dropped. where is undefined when nothing survives. A date preset filter needs resolveDate (it throws without one). |
toDrizzleRqbV1Callback |
Render an already-compiled CompiledWhere AST to an RQB v1 where callback (fields, operators) => SQL | undefined. columns maps each field to a schema property key, which the callback looks up in the fields drizzle passes in. Conditions on unknown fields are dropped; when nothing is left the callback returns undefined. Most code calls buildRqbV1Where, which compiles and renders in one step. |
toDrizzleRqbWhere |
Render an already-compiled CompiledWhere AST (from compileFilters / compileFilterNode in @xtandard/lib/filters) to an RQB v2 where object, mapping each field to a schema property key through 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. |
Interfaces
Section titled “Interfaces”| Name | Summary |
|---|---|
RqbV1Operators |
The drizzle operators the RQB v1 where callback receives as its second argument ((fields, operators) => …, drizzle’s getOperators()). Structural + generic over the SQL type R so this adapter imports no drizzle-orm. and/or may return undefined (no conditions), matching drizzle. |
RqbV1Sql |
The sql tag the RQB v1 operators carry: a tagged template that builds the SQL type R, plus sql.param(value, encoder) to bind a value through a column’s driver encoding. |
Type aliases
Section titled “Type aliases”| Name | Summary |
|---|---|
DrizzleRqbFieldSpec |
One allow-listed field: its filter kind plus the schema property key ("createdAt", not the SQL column name) it reads. |
DrizzleRqbFilterSpec |
A resource’s filter allow-list: public field name → DrizzleRqbFieldSpec. |
DrizzleRqbWhere |
A portable subset of the RQBv2 where shape this adapter emits: a single column→condition object, or an AND/OR/NOT node. |
RqbCondition |
A single column’s RQBv2 condition (the inner { eq, gt, ilike, … } object). |
RqbV1WhereCallback |
An RQB v1 where callback: (fields, operators) => SQL | undefined. fields is the (aliased) table the RQB passes; keys are schema property names. |
Variables
Section titled “Variables”| Name | Summary |
|---|---|
arrayField |
Allow-list a public field as an array filter over a Postgres array column, named by its Drizzle schema property key ("tags"). The ops map 1:1 to RQB’s arrayContains/ arrayContained/arrayOverlaps. |
booleanField |
Allow-list a public field as a boolean filter (eq/ne/null checks) over a column named by its Drizzle schema property key ("done"). |
dateField |
Allow-list a public field as a date filter over a timestamp column, named by its Drizzle schema property key ("createdAt", not the SQL name created_at). A date preset needs the resolveDate you pass to buildWhere, and becomes a half-open gte/lt window. |
enumField |
Allow-list a public field as an enum filter (eq/ne/inArray/notInArray/null checks) over a column named by its Drizzle schema property key ("status"). |
numberField |
Allow-list a public field as a number filter over a numeric column, named by its Drizzle schema property key ("total"). between becomes gte + lte. |
textField |
Allow-list a public field as a text filter over a string column, named by its Drizzle schema property key ("name"). contains/startsWith/endsWith become ilike with the input’s %/_/\ escaped. That is Postgres-only in the RQB v2 object (no ESCAPE, and SQLite/MySQL have no ilike); the v1 callback renders like … escape on those columns. |