Upsert feature in convex?
Currently this is how I'm approaching it:
export const upsertTask = mutation({
args: {
text: v.string(),
},
handler: async (ctx, args) => {
const existingItem = await ctx.db
.query("tasks")
.filter((q) => q.eq(q.field("task_name"), args.text))
.first();
if (existingItem !== null) {
await ctx.db.patch(existingItem._id, {
task_name: args.text,
});
} else {
await ctx.db.insert("tasks", {
task_name: args.text,
isCompleted: false,
});
}
},
});
Which achieves the purporse, but was thinking if there was a better/shorter approach to this?
Thank you
