shadow_ayaS
Convex Community3y ago
2 replies
shadow_aya

Optional arguments and patch

I read that setting a field to undefined with
db.patch
unsets it. now I'm a bit afraid of implementing an "update" function that can update any values if you provide a value for them:
export const update = mutation({
    args: {
        id: v.id("users"),
        discordId: v.optional(v.string()),
        name: v.optional(v.string()),
        email: v.optional(v.string()),
        emailVerified: v.optional(v.number()),
        image: v.optional(v.string()),
    },
    async handler(ctx, args) {
        return await ctx.db.patch(args.id, {
            discordId: args.discordId,
            name: args.name,
            email: args.email,
            emailVerified: args.emailVerified,
            image: args.image,
        });
    },
});

is this safe if I make the args optional?
Was this page helpful?