Convex reactive backend

From findscripter/everything-skills · View interactive version

Convex reactive backend is an AI agent skill from the GitHub repository findscripter/everything-skills. 当用 Convex 搭建/接入响应式后端,需设计 Schema、编写 TS 函数(query/mutation/action)、做实时订阅、鉴权、文件存储或定时任务时使用;产出可运行的 convex/ 函数、Schema、索引与客户端订阅代码;不适用于传统 SQL/ORM 或非 Convex 的 BaaS。触发词:Convex、响应式后端、实时订阅、useQuery

How to use this skill

Skill file

何时使用

不该用边界:

步骤

  1. 初始化:npm install convex 后运行 npx convex dev。它会引导 GitHub 登录、创建项目与部署、生成 convex/ 目录、实时同步函数,并写入 .env.local(CONVEX_DEPLOYMENT、NEXT_PUBLIC_CONVEX_URL)。convex/_generated/ 为自动生成,禁止手改。
  2. 定 Schema:在 convex/schema.ts 用 defineSchema / defineTable 和验证器 v 描述表、字段与索引(普通索引 .index、全文 .searchIndex、向量 .vectorIndex)。
  3. 写函数:query 读、mutation 写(ACID 事务、可串行化隔离)、action 调外部 API。给每个函数加 args 验证器。
  4. 接客户端:用 ConvexProvider 包裹应用,组件内用 useQuery / useMutation / usePaginatedQuery 订阅与写入。
  5. 进阶:按需加鉴权、文件存储、ctx.scheduler 定时任务与 crons.ts。
  6. 部署:npx convex deploy;用 npx convex env set 配置环境变量。

指令

npx convex dev          # 开发:监听改动并同步到 dev 部署
npx convex deploy       # 部署到生产
npx convex run tasks:list           # CLI 直接调函数
npx convex import --table tasks data.jsonl
npx convex export --path ./backup
npx convex logs / dashboard
npx convex env set OPENAI_API_KEY sk-...   # 仅 action 内可读 process.env
npx convex env list / unset OPENAI_API_KEY

函数能力对照(关键约束):

| 类型 | 用途 | 读库 | 写库 | 调外部 API | 缓存/响应式 |

| :-- | :-- | :-: | :-: | :-: | :-: |

| Query | 读数据 | ✅ | ❌ | ❌ | ✅ |

| Mutation | 写数据(ACID) | ✅ | ✅ | ❌ | ❌ |

| Action | 副作用 | 经 runQuery | 经 runMutation | ✅ | ❌ |

| HTTP Action | Webhook/端点 | 经 runQuery | 经 runMutation | ✅ | ❌ |

示例

Schema 与索引(convex/schema.ts):

import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";

export default defineSchema({
  messages: defineTable({
    authorId: v.id("users"),
    channelId: v.id("channels"),
    body: v.string(),
    attachmentId: v.optional(v.id("_storage")),
  })
    .index("by_channel", ["channelId"])
    .searchIndex("search_body", { searchField: "body", filterFields: ["channelId"] }),
});
// 复合索引:等值字段在前、范围字段在后
// 向量索引:.vectorIndex("by_embedding", { vectorField: "embedding", dimensions: 1536 })

响应式 query + 用索引(convex/messages.ts):

import { query } from "./_generated/server";
import { v } from "convex/values";

export const getByChannel = query({
  args: { channelId: v.id("channels") },
  handler: async (ctx, args) =>
    ctx.db.query("messages")
      .withIndex("by_channel", (q) => q.eq("channelId", args.channelId))
      .order("desc").take(50),   // 用 take/paginate,勿对大表裸 collect
});

Mutation(多文档自动原子):

export const transferCredits = mutation({
  args: { fromUserId: v.id("users"), toUserId: v.id("users"), amount: v.number() },
  handler: async (ctx, args) => {
    const from = await ctx.db.get(args.fromUserId);
    const to = await ctx.db.get(args.toUserId);
    if (!from || !to) throw new Error("User not found");
    if (from.credits < args.amount) throw new Error("Insufficient credits");
    await ctx.db.patch(args.fromUserId, { credits: from.credits - args.amount });
    await ctx.db.patch(args.toUserId,   { credits: to.credits   + args.amount });
  },
});

Action 调外部 API,再经 mutation 回写(action 内不能直接 ctx.db):

export const sendEmail = action({
  args: { to: v.string(), subject: v.string(), body: v.string() },
  handler: async (ctx, args) => {
    const res = await fetch("https://api.sendgrid.com/v3/mail/send", {
      method: "POST",
      headers: { Authorization: `Bearer ${process.env.SENDGRID_API_KEY}`, "Content-Type": "application/json" },
      body: JSON.stringify({ /* ... */ }),
    });
    if (!res.ok) throw new Error("Failed to send email");
    await ctx.runMutation(api.emails.recordSent, { to: args.to, sentAt: Date.now() });
  },
});

客户端订阅(React/Next.js,useQuery 自动随数据更新):

"use client";
import { useQuery, useMutation } from "convex/react";
import { api } from "@/convex/_generated/api";

export function TaskList() {
  const tasks = useQuery(api.tasks.list);            // undefined = 加载中(非空数据)
  const addTask = useMutation(api.tasks.create);
  if (tasks === undefined) return <p>Loading...</p>;
  return <button onClick={() => addTask({ text: "New task", isCompleted: false })}>Add</button>;
}

分页用 usePaginatedQuery(api.x, {}, { initialNumItems: 20 }),配合 .paginate(paginationOpts)。

鉴权(原生 @convex-dev/auth,含 Magic Link/密码/80+ OAuth):用 ConvexAuthProvider 包裹,客户端 const { signIn } = useAuthActions() → signIn("github");query 内 await ctx.auth.getUserIdentity() 判断身份,未登录返回 null。第三方可用 ConvexProviderWithClerk。

定时与文件存储:

// 一次性:ctx.scheduler.runAfter(delayMs, api.notifications.send, {...})
// convex/crons.ts
import { cronJobs } from "convex/server";
const crons = cronJobs();
crons.interval("clear old logs", { hours: 24 }, api.logs.clearOld);
crons.cron("weekly digest", "0 9 * * 1", api.emails.sendWeeklyDigest);
export default crons;

// 文件:mutation 里 ctx.storage.generateUploadUrl() 取上传 URL,
// 存 storageId(v.id("_storage")),query 里 ctx.storage.getUrl(storageId) 读取。

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.