Skip to content

@xtandard/lib/filters/prisma

Renders the compiled filter AST to a Prisma where object (no driver dependency).

Text ops map to Prisma’s case-insensitive contains/startsWith/endsWith; between (lowered upstream) → gte/lte; inArray → in; scalar-list array ops → hasEvery/hasSome. like/ilike/notIlike are reduced to contains/startsWith/endsWith when the pattern allows (%x%/x%/%x); only genuinely arbitrary patterns (internal %, any _), which Prisma’s where can’t express, throw (use $queryRaw). arrayContained (array ⊆ values) also has no Prisma operator.

import { buildWhere, textField, numberField } from "@xtandard/lib/filters/prisma";
const spec = { name: textField({ field: "name" }), amount: numberField({ field: "amount" }) };
const { where } = buildWhere({ spec, filters });
// where → { AND: [{ name: { contains: "ab", mode: "insensitive" } }, … ] }
await prisma.user.findMany({ where });
import { … } from "@xtandard/lib/filters/prisma";
Name Summary
buildFilterNode Compile a recursive and/or/not FilterNode tree into a Prisma where object (or undefined). Same allow-list rules as buildWhere.
buildWhere Compile a flat, AND-combined list of column filters into a Prisma where object. Only fields in spec whose kind matches the filter’s kind are kept; the rest are silently dropped. where is undefined when nothing survives, which Prisma treats as “no filter”. A date preset filter needs resolveDate (it throws without one).
toPrismaWhere Render an already-compiled CompiledWhere AST (from compileFilters / compileFilterNode in @xtandard/lib/filters) to a Prisma where object, looking each field up in fields. Conditions on fields missing from fields are dropped; null (or an AST that drops to nothing) gives undefined. and/or/not become AND/OR/NOT. Throws for arrayContained and for like/ilike patterns with an inner % or any _. Most code calls buildWhere, which compiles and renders in one step.
Name Summary
PrismaFieldSpec One allow-listed field: its filter kind plus the Prisma model field name it reads.
PrismaFilterSpec A resource’s filter allow-list: public field name → PrismaFieldSpec.
PrismaWhere A plain Prisma where object, ready for prisma.<model>.findMany({ where }).
Name Summary
arrayField Allow-list a public field as an array filter over a Prisma scalar-list model field (e.g. tags String[]), named by its model field name ("tags"). arrayContains → hasEvery, arrayOverlaps → hasSome; arrayContained has no Prisma operator and throws.
booleanField Allow-list a public field as a boolean filter (eq/ne/null checks) over a Prisma Boolean model field, named by its model field name ("done").
dateField Allow-list a public field as a date filter over a Prisma DateTime model field, named by its model field name ("createdAt"). 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 Prisma enum or String model field, named by its model field name ("status").
numberField Allow-list a public field as a number filter over a Prisma Int/Float model field, named by its model field name ("total"). between becomes gte + lte.
textField Allow-list a public field as a text filter over a Prisma String model field, named by its model field name ("name"). contains/startsWith/endsWith map to Prisma’s operators with mode: "insensitive"; like/ilike patterns that Prisma can’t express throw.