Loading...
Loading...
This skill should be used when implementing Convex mutation functions. It provides comprehensive guidelines for defining, registering, calling, and scheduling mutations, including database operations, transactions, and scheduled job patterns.
npx skill4agent add sstobo/convex-skills convex-mutationsinsertpatchreplacedeletectx.scheduler.runAfterreferences/mutation-guidelines.mdmutationinternalMutationctx.runQueryctx.runMutationapiinternalctx.db.insert()ctx.db.patch()ctx.db.replace()ctx.scheduler.runAfter()references/mutation-guidelines.mdinsertpatchreplacectx.scheduler.runAfterctx.db.patch()ctx.db.replace()ctx.scheduler.runAfter()nullnullimport { mutation, internalAction } from "./_generated/server";
import { v } from "convex/values";
import { internal } from "./_generated/api";
export const sendMessage = mutation({
args: {
channelId: v.id("channels"),
authorId: v.id("users"),
content: v.string(),
},
handler: async (ctx, args) => {
// Validate channel and user exist
const channel = await ctx.db.get(args.channelId);
if (!channel) {
throw new Error("Channel not found");
}
// Insert message into database
await ctx.db.insert("messages", {
channelId: args.channelId,
authorId: args.authorId,
content: args.content,
});
// Schedule AI response generation (transactional)
await ctx.scheduler.runAfter(0, internal.functions.generateResponse, {
channelId: args.channelId,
});
return null;
},
});
export const updateUserStatus = mutation({
args: {
userId: v.id("users"),
status: v.string(),
},
handler: async (ctx, args) => {
// Patch updates an existing document with shallow merge
await ctx.db.patch(args.userId, {
status: args.status,
lastUpdated: Date.now(),
});
return null;
},
});