How to add dynamic Where queries in objection.js?
Objections allows you to modify the query:
let filtersArr = [.. build your filters]
await YourModel
.query()
.modify((queryBuilder) => {
if (hasFilters) {
filtersArr.forEach(({criteria, value}) => {
queryBuilder.where(criteria, value)
})
}
if (hasQ) {
queryBuilder.where('name', 'ilike', `%${q}%`)
}
})
.page(page, paginate)
this can be extended to include relations, or sort and it doesn't have to be exclusive for one at the time, you can filter, sort and search all within the same query