const { from, to } = req.query;const where = { createdAt: { ...(from && { gte: new Date(from) }), ...(to && { lte: new Date(to) }) }};
5. Implementing Fuzzy Search
Example: Postgres trigram
CREATE EXTENSION IF NOT EXISTS pg_trgm;CREATE INDEX products_name_trgm ON products USING GIN (name gin_trgm_ops);SELECT *, similarity(name, $1) AS scoreFROM productsWHERE name % $1ORDER BY score DESCLIMIT 20;
Engine
Tool
Postgres
pg_trgm, tsvector
MongoDB
Atlas Search, $text
External
OpenSearch, Meilisearch, Typesense
6. Creating Complex Query Builders
Example: Reusable builder
function buildProductFilter(q) { const where = {}; if (q.search) where.OR = [{ name: { contains: q.search } }, { sku: q.search }]; if (q.category) where.category = q.category; if (q.inStock === "true") where.stock = { gt: 0 }; if (q.minPrice || q.maxPrice) { where.price = {}; if (q.minPrice) where.price.gte = Number(q.minPrice); if (q.maxPrice) where.price.lte = Number(q.maxPrice); } return where;}