Filtering
where() adds a filter to the plan. Filters compile to WHERE clauses with
bound parameters. Values are never interpolated into SQL.
Comparators
Section titled “Comparators”db.from(users).where("age", ">", 18);db.from(users).where("age", ">=", 18);db.from(users).where("age", "<", 65);db.from(users).where("age", "<=", 65);db.from(users).where("email", "=", "a@b.c");db.from(users).where("email", "!=", "a@b.c");| Operator | Meaning |
|---|---|
= != |
equality / inequality |
> >= < <= |
numeric / date comparison |
like |
pattern match (% wildcard) |
in |
value in a list |
is is not |
null checks |
between |
range (inclusive) |
db.from(users).where("id", "in", [1, 2, 3]);// → WHERE "id" IN (?, ?, ?)db.from(users).where("email", "like", "ada%");IS NULL
Section titled “IS NULL”db.from(users).where("age", "is", null);db.from(users).where("age", "is not", null);BETWEEN
Section titled “BETWEEN”db.from(users).where("age", "between", [18, 65]);OR groups
Section titled “OR groups”Pass a function to or() to group alternatives:
db.from(users).or((b) => b.where("age", "<", 18).where("age", ">", 65),);// → WHERE ("age" < ? OR "age" > ?)Subqueries (EXISTS / IN)
Section titled “Subqueries (EXISTS / IN)”db.from(users).whereExists( db.from(posts).where("authorId", "=", /* column ref */ "users.id"),);FilterNode for RLS
Section titled “FilterNode for RLS”You can pass a raw FilterNode to where() — this is how RLS policies inject
filters:
import { applyPolicy } from "@mountsqli/auth";const scoped = applyPolicy(db.from(files), allowOwner("ownerId"), { userId });Best practices
Section titled “Best practices”- Chain
where()to AND multiple conditions. - Use
infor membership instead of multipleorequals. - Keep filters in the plan; don’t filter rows in app code.
Common mistakes
Section titled “Common mistakes”- Using
=withnull— useis nullinstead. - String-interpolating a value into a raw expression — bind it as a param.
Related
Section titled “Related”- Select — running filtered queries.
- Raw SQL & RLS Gate —
whereExpr. - RLS Policy DSL — policies as FilterNode[].
