Default values for fields
I have this schema for my clerk authenticated user. I want to keep some default values like isActive, isDeleted, createdAt and updatedAt. Any suggestions how can I use that.
3 Replies
The
createdAt
field isn't necessary. Convex already handles that for you: https://docs.convex.dev/database/types#system-fields
As for setting default values for the rest, I don't know a way to do that in the schema itself. The best I can think of would be to set those values in whatever mutation you use to create the document. Pass in all the other values as arguments to the mutation, and the mutation adds the default field values for the rest. That way they're only specified in one place in the code.
FWIW, I recommend starting a thread in #support-community , as that's a more appropriate place to get wider input on stuff like this. Threads here can scroll out of view pretty quickly.Data Types | Convex Developer Hub
All Convex documents are defined as Javascript objects. These objects can have
Thanks a lot for the insigts. I'll follow the above steps as provided.
Side note: if
isActive
and isDeleted
are mutually exclusive, consider using a single field, e.g.: status: 'active' | 'deleted'
.