Code with AntonioC

Query both unset and false

Hi Everyone! I love using Convex so far, it is really easy and a lot of great features out of the box. Right now I am trying to figure out a best practice to implement a "soft delete".

I have defined a schema documents:

export default defineSchema({
  documents: defineTable({
    title: v.string(),
    userId: v.string(),
    isArchived: v.optional(v.boolean()),
  })
  .index("by_user_id", ["userId"])
  .index("by_title", ["title"])
});


The problem arrives when I try to only the non-archived documents, this is the filter I am using:
.filter((q) => q.eq("isArchived", undefined))
If I attempt to filter by false instead of undefined I get a typescript error.

And in my restore function, I patch the document by setting isArchived to false.

const document = await ctx.db.patch(args.id, {
  isArchived: false,
});


I assume it can work by me setting the isArchived: undefined instead of
false
But I feel like I am doing something wrong here?

Can I query an optional, boolean field by both undefined | false ?

P.S. My userId is a string because I dont have the need for users table atm, I just use Clerk's userId.
Was this page helpful?