#Possible to optionally filter an arg?

4 messages · Page 1 of 1 (latest)

civic trail
#

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()
  }
})

thanks!

#

maybe sth like the builder pattern from javaland would be cool?

obtuse dagger
#

You would write q.truthy() as true

#

Which can be slightly simplified into q => args.dateBegin === "" || q.gte(...))