I m confused about the schema `s
I'm confused about the schema
s.optional
Does it produce undefined, null?
Seems like undefined should be acceptable, but why in this case I'm getting errors when I try to write underfined?
9 Replies
here is the relavant doc

however TypeScript complains if I try to use null.
s.optional will work if the property is missing on your document, but not if it is explicitly set to
undefined
I settled on
s.union(s.id("users"), s.null()),
but that's mighty confusing 😆Yeah, I can see how the current doc here can be misleading: https://docs.convex.dev/using/schemas#optional-fields. We should update it to be more clear that this works only with missing fields
Defining a Schema | Convex Developer Hub
End-to-end type safety requires typing your tables.
The union with null works too if you prefer setting the value explicitly!
Does this mean you have to use
delete document.myfield
to make a field go from present to missing?
And that there's no way to do that using db.patch
? Or is there some way I'm not thinking of?
It seems like undefined
should be treated the same as "not present", like JSON.parse and .stringify do.Ok I see. Yes, seems like an opportunity for better documentation as usual 🙂
that's correct, there's no way to do this from
db.patch
now, you have to db.replace
. This is annoying, but's safe to use
because the whole mutation is a transaction