ashuvssut (ashu)
ashuvssut (ashu)16mo ago

how to call a function (mutation) from another function (mutation)?

question in the title
4 Replies
ashuvssut (ashu)
ashuvssut (ashu)OP16mo ago
snippet for editing
export const remove = mutation({
args: { bmId: v.id("bookmarks") },
handler: async (ctx, { bmId }) => {

},
});
export const remove = mutation({
args: { bmId: v.id("bookmarks") },
handler: async (ctx, { bmId }) => {

},
});
erquhart
erquhart16mo ago
3 approaches I use: 1. Ideally, see if there's a way to avoid this situation, often by splitting reusable logic out into separate functions, so you can do everything you need to in one mutation. There are a lot of approaches for this covered in Convex Stack and their example repos on GitHub. 2. If you must call a mutation from another mutation, use the scheduler arg to schedule it (you can schedule with a 0 wait in this case). 3. Last option is using an action instead of the initial mutation. This is not ideal as actions don't have the same transactional/atomicity guarantees as mutations and won't be retried in case of failure.
ashuvssut (ashu)
ashuvssut (ashu)OP16mo ago
thanks! Btw I am really enjoying convex Its awesome Perfect DX
erquhart
erquhart16mo ago
Nice!! I'm totally not a convex team member, just a fellow avid user lol. But agree, and same!

Did you find this page helpful?