conditional filter
Is there a way to conditionally filter a query? It doesn't seem possible to break up the chaining conditionally, and it also doesn't seem possible to exit early from the
.filter(...)
function. Id rather not write out the entire query chain multiple times for each condition, as this is the only part of the query that changes.15 Replies
.filter(q => true)
is a no-op filter. but also note you can do whatever filters and post-processing you want in javascript after getting results, and it's very similar under the hood (because the js code is running on the server, just like the filter
), so i would recommend that over complicated filters
to answer your question though, i think the syntax for a the conditional filter would be like .filter((q) => { if (conditional) { return q.eq(...filter...) } else { return true }})
@Lee its a paginated query, so doing js based filtering is not ideal.
Thanks for the escape noop syntax.
You can also break it up like:
let query = db.query(…)
If (condition) {
query = query.filter(…)
}
return query.paginate(…)
Let me know if it gives you more trouble
@ian this was my first approach and I got an error around chaining
Type error or runtime?
runtime
Odd. If you were doing (a||b).paginate you might need to .bind ?
I can try to repro later
the following code produces the attached error.

Ah you need to be calling query.paginate on the return from query.filter
how is this different from your example above?
query = query.filter
i see
We can work on that error message
I suppose if
db.query
was a constructor this would work, but makes sense why it doesnt with my chaining approach. Just some patterns from working with MikroORM I need to shift on.for posterity, there's now an easier way to write completely arbitrary filters, and it works with pagination: https://stack.convex.dev/complex-filters-in-convex
Using TypeScript to Write Complex Query Filters
There’s a new Convex helper to perform generic TypeScript filters, with the same performance as built-in Convex filters, and unlimited potential.