一键导入
source-command-convex-function-creator
Create Convex functions (queries, mutations, actions) with proper validation, auth, and patterns for this project
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Create Convex functions (queries, mutations, actions) with proper validation, auth, and patterns for this project
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Set up Convex authentication with proper user management, identity mapping, and access control patterns. Use when implementing auth flows.
Guide to using Convex components for feature encapsulation. Learn about sibling components, creating your own, and when to use components vs monolithic code.
Discover and use convex-helpers utilities for relationships, filtering, sessions, custom functions, and more. Use when you need pre-built Convex patterns.
Create Convex queries, mutations, and actions with proper validation, authentication, and error handling. Use when implementing new API endpoints.
Plan and execute Convex schema migrations safely, including adding fields, creating tables, and data transformations. Use when schema changes affect existing data.
Design and generate Convex database schemas with proper validation, indexes, and relationships. Use when creating schema.ts or modifying table definitions.
| name | source-command-convex-function-creator |
| description | Create Convex functions (queries, mutations, actions) with proper validation, auth, and patterns for this project |
Use this skill when the user asks to run the migrated source command convex-function-creator.
Create Convex functions with proper validation, authentication, and error handling for this project.
| Type | Purpose | Database Access | External APIs |
|---|---|---|---|
query | Read data | Yes (ctx.db) | No |
mutation | Write data | Yes (ctx.db) | No |
action | Side effects | No (use ctx.runQuery/ctx.runMutation) | Yes |
import { v } from "convex/values";
import { query } from "./_generated/server";
import { authComponent } from "./auth";
export const list = query({
args: {
// Define input validators
},
returns: v.array(
v.object({
// Define return type
}),
),
handler: async (ctx, args) => {
const user = await authComponent.getAuthUser(ctx);
if (!user) throw new Error("Not authenticated");
return await ctx.db
.query("tableName")
.withIndex("by_userId", (q) => q.eq("userId", user._id))
.collect();
},
});
import { v } from "convex/values";
import { mutation } from "./_generated/server";
import { authComponent } from "./auth";
export const create = mutation({
args: {
title: v.string(),
},
returns: v.id("tableName"),
handler: async (ctx, args) => {
const user = await authComponent.getAuthUser(ctx);
if (!user) throw new Error("Not authenticated");
return await ctx.db.insert("tableName", {
userId: user._id,
title: args.title,
createdAt: Date.now(),
});
},
});
"use node";
import { v } from "convex/values";
import { action } from "./_generated/server";
import { authComponent } from "./auth";
export const processExternal = action({
args: {
itemId: v.id("tableName"),
},
returns: v.null(),
handler: async (ctx, args) => {
const user = await authComponent.getAuthUser(ctx);
if (!user) throw new Error("Not authenticated");
// Call external API
const response = await fetch("https://api.example.com/...");
const data = await response.json();
// Write results via mutation
await ctx.runMutation(internal.tableName.saveResult, {
itemId: args.itemId,
result: data,
});
return null;
},
});
Internal functions are only callable by other Convex functions (not from clients):
import { v } from "convex/values";
import { internalMutation, internalQuery } from "./_generated/server";
export const processItem = internalMutation({
args: { itemId: v.id("tableName"), data: v.string() },
returns: v.null(),
handler: async (ctx, args) => {
await ctx.db.patch(args.itemId, { processedData: args.data });
return null;
},
});
Every function MUST have:
args validator - defines and validates input typesreturns validator - defines return typeauthComponent.getAuthUser(ctx) for user-facing functionsv.string(); // string
v.number(); // number (use for timestamps)
v.boolean(); // boolean
v.int64(); // 64-bit integer (NOT v.bigint())
v.null(); // null
v.id("tableName"); // Document ID
v.array(v.string()); // Array
v.object({ key: v.string() }); // Object
v.optional(v.string()); // Optional field
v.union(v.literal("a"), v.literal("b")); // Enum/union
v.record(v.string(), v.number()); // Dynamic keys
import { paginationOptsValidator } from "convex/server";
export const listPaginated = query({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, args) => {
return await ctx.db
.query("tableName")
.order("desc")
.paginate(args.paginationOpts);
},
});
// Always schedule internal functions, never api functions
await ctx.scheduler.runAfter(0, internal.tasks.processItem, { itemId });
await ctx.scheduler.runAt(futureTimestamp, internal.tasks.cleanup, {});
import { asyncMap } from "convex-helpers";
const results = await asyncMap(ids, async (id) => {
return await ctx.db.get(id);
});
Date.now() in queries (breaks reactivity).filter() on db.query() - use .withIndex() insteadawait every promise (ctx.db.patch, ctx.scheduler.runAfter, etc.)"use node"; directive to action files using Node.js APIsctx.db directly - use ctx.runQuery() / ctx.runMutation()