#call action from mutation
9 messages · Page 1 of 1 (latest)
Let me check the knowledge base for you. One moment! :books:
In Convex, you cannot directly call an action within a mutation due to the transactional nature of mutations. However, you can schedule an action to run after a mutation commits. This ensures that the action only runs if the mutation has successfully committed.
Here's an example of how you can schedule an action from a mutation:
await ctx.scheduler.runAfter(0, internal.sources.addEmbedding, {
source,
texts: chunks.map(({ text }) => text),
});
In this example, internal.sources.addEmbedding is the action being scheduled to run after the mutation. The action is only invoked if the mutation successfully commits, ensuring that you'll never have an action running for a source that doesn't exist.
For more information, you can refer to the Convex documentation on using Pinecone and Embeddings (https://stack.convex.dev/pinecone-and-embeddings#kick-off-a-background-action) and The Zen of Convex (https://docs.convex.dev/zen).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@hybrid wind kapa.ai is still learning and improving, please let us know how it did by reacting below
@jovial walrus how can I call a mutation from another mutation
Let me check the knowledge base for you. One moment! :books:
In Convex, you can't directly call a mutation from another mutation. However, you can structure your code in a way that allows you to reuse logic across different mutations.
For example, if you have a piece of logic that you want to use in multiple mutations, you can extract that logic into a separate function and then call that function from within your mutations.
Here's an example:
export const update = internalMutation({
args: { messageId: v.id("messages"), body: v.string() },
handler: async (ctx, { messageId, body }) => {
await ctx.db.patch(messageId, { body });
},
});
In this example, the update function is an internalMutation that updates the body of a message. This function can be called from within other mutations or actions.
For more information, you can refer to the Convex documentation on GPT Streaming With Persistent Reactivity (https://stack.convex.dev/gpt-streaming-with-persistent-reactivity#persisting-messages).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@errant skiff kapa.ai is still learning and improving, please let us know how it did by reacting below
@errant skiff I think the statement made by the AI "This function can be called from within other mutations or actions." is incorrect. From what I've found, you can't call an internalMutation from a mutation. Correct me if I'm wrong.