Optional filter param, coalesce to true?

Is there a way to achieve this coalescing technique, but in valid drizzle sql?
const filterByBool: boolean | undefined | null = undefined

await db
.select()
.from(test)
.where(test.bool == filterByBool ?? true)
const filterByBool: boolean | undefined | null = undefined

await db
.select()
.from(test)
.where(test.bool == filterByBool ?? true)
3 Replies
rphlmr âš¡
rphlmr ⚡•2y ago
const filterByBool: boolean | undefined | null = undefined

await db
.select()
.from(test)
.where(eq(test.bool,filterByBool ?? true))
const filterByBool: boolean | undefined | null = undefined

await db
.select()
.from(test)
.where(eq(test.bool,filterByBool ?? true))
should works eq comes from drizzle import 😉
Andrii Sherman
Andrii Sherman•2y ago
For some advanced filters you can use this example https://discord.com/channels/1043890932593987624/1109091751341342820/1109095820827037737 all you need from this examples is that filter statements can be grouped in array And then used in where + and(…filters)
mcgrealife
mcgrealifeOP•2y ago
Thanks to both! Being able to use javascript inside a query builder is delightful. 🙌
Want results from more Discord servers?
Add your server