Option filter parameters

Hi! Love using drizzle so far! Had a quick question (not a bug):
const res = await ctx.database
.select()
.from(worker)
.where(
and(
eq(worker.locationUid, input.location),
isNull(worker.deletedAt),
ilike(worker.name, "%{input.name}%")
)
)
const res = await ctx.database
.select()
.from(worker)
.where(
and(
eq(worker.locationUid, input.location),
isNull(worker.deletedAt),
ilike(worker.name, "%{input.name}%")
)
)
If I have a query like this, and input.name might be null (in which case I want to skip filtering by this), is there a short-hand for enabling that behavior? Right now I'm putting filters into an array like:
[..., input.name && ilike(worker.name, "%{input.name}%) : null].filter(v => v)
[..., input.name && ilike(worker.name, "%{input.name}%) : null].filter(v => v)
which is fine, just wondering if this is a good approach. thanks!
2 Replies
Andrii Sherman
Andrii Sherman13mo ago
Hi! here you go https://discord.com/channels/1043890932593987624/1109091751341342820/1109095820827037737 just small example I did for another thread but should work well for you we will also add this case to docs will work on examples section for different cases like this one all you need from this examples is that filter statements can be grouped in array and then used like this
.where(and(...filters))
.where(and(...filters))
vapor
vapor13mo ago
great thanks, looks good