I have a Mutation, updateViewCount, that is as follows:
export const updateViewCount = mutation({
args: { id: v.id("intakeForms") },
handler: async (ctx, { id }) => {
const intakeForm = await ctx.db.get(id);
if (!intakeForm) {
return null;
}
await ctx.db.patch(id, { views: (intakeForm.views ?? 0) + 1 });
const updated = await ctx.db.get(id);
return updated;
},
});
This is on a public facing webpage, think of it as a page view counter. This runs without auth check as anyone should be able to access this page and thus make a call to the Mutation. In my Convex Dashboard I get the error: Retried due to write conflicts in table. Which sounds like a race condition. How can I avoid this? How can I add 1 to the field without caring what it's current value is?