Papa Johns
Papa Johns3mo ago

Default values for fields

export default defineSchema({
users: defineTable({
firstName: v.string(),
lastName: v.string(),
email: v.string(),
isEmailVerified: v.string(),
clerkUserId: v.string(),
isActive: v.boolean(),
isDeleted: v.boolean(),
createdAt: v.string(),
updatedAt: v.string(),
})
.index("by_clerk_user_id", ["clerkUserId"])
.index("by_email", ["email"])
.index("by_created_at", ["createdAt"]),
});
export default defineSchema({
users: defineTable({
firstName: v.string(),
lastName: v.string(),
email: v.string(),
isEmailVerified: v.string(),
clerkUserId: v.string(),
isActive: v.boolean(),
isDeleted: v.boolean(),
createdAt: v.string(),
updatedAt: v.string(),
})
.index("by_clerk_user_id", ["clerkUserId"])
.index("by_email", ["email"])
.index("by_created_at", ["createdAt"]),
});
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
Clever Tagline
Clever Tagline3mo ago
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
Papa Johns
Papa JohnsOP3mo ago
Thanks a lot for the insigts. I'll follow the above steps as provided.
Gustavo
Gustavo3mo ago
Side note: if isActive and isDeleted are mutually exclusive, consider using a single field, e.g.: status: 'active' | 'deleted'.

Did you find this page helpful?