buildFilter
function buildFilter(input): object;Defined in: filters/mongo.ts:274
Compile a flat, AND-combined list of column filters into a Mongo filter object. Only fields
in spec whose kind matches the filter’s kind are kept; the rest are silently dropped.
filter is undefined when nothing survives, so pass filter ?? {} to find. A date
preset filter needs resolveDate (it throws without one).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
input |
{ filters: ColumnFilter[]; resolveDate?: DateFilterResolver; spec: MongoFilterSpec; } |
input.filters |
ColumnFilter[] |
input.resolveDate? |
DateFilterResolver |
input.spec |
MongoFilterSpec |
Returns
Section titled “Returns”object
| Name | Type | Defined in |
|---|---|---|
filter |
| MongoFilter | undefined |
filters/mongo.ts:278 |
Example
Section titled “Example”import { buildFilter, numberField, textField } from "@xtandard/lib/filters/mongo";
const spec = { name: textField({ path: "name" }), city: textField({ path: "address.city" }), amount: numberField({ path: "amount" }) };const { filter } = buildFilter({ spec, filters: [ { field: "name", filter: { kind: "text", operator: "startsWith", value: "Re" } }, { field: "city", filter: { kind: "text", operator: "eq", value: "Lima" } }, { field: "amount", filter: { kind: "number", operator: "gte", value: 100 } },] });// filter → { $and: [{ name: { $regex: "^Re", $options: "i" } }, { "address.city": { $eq: "Lima" } }, { amount: { $gte: 100 } }] }// in an app: const docs = await collection.find(filter ?? {}).toArray();