winsoroaks
winsoroaks•16mo ago

Possible to optionally filter an arg?

I've skimmed thru search.convex.dev but couldnt find any answer. is it possible to optionally filter an arg?
export const listTasks = query({
args: {
dateBegin: v.optional(v.string()),
},

handler: async (ctx, args) => {
const tasks = await ctx.db
.query(TASK_DB)
.filter((q) =>
q.and(
args.dateBegin !== ""
? q.gte(q.field("scheduledTime"), Number(args.dateBegin) * 1000)
: q.truthy(), // is this available?
...
)
)
.collect()
}
})
export const listTasks = query({
args: {
dateBegin: v.optional(v.string()),
},

handler: async (ctx, args) => {
const tasks = await ctx.db
.query(TASK_DB)
.filter((q) =>
q.and(
args.dateBegin !== ""
? q.gte(q.field("scheduledTime"), Number(args.dateBegin) * 1000)
: q.truthy(), // is this available?
...
)
)
.collect()
}
})
thanks!
3 Replies
winsoroaks
winsoroaksOP•16mo ago
maybe sth like the builder pattern from javaland would be cool?
lee
lee•16mo ago
You would write q.truthy() as true Which can be slightly simplified into q => args.dateBegin === "" || q.gte(...))
winsoroaks
winsoroaksOP•16mo ago
it worked! thank you 🙂

Did you find this page helpful?