用途:为构建生产级 Convex 应用提供成体系的编码规范,涵盖函数组织、查询优化、校验、TypeScript 使用与错误处理。
主要能力:给出按领域组织函数(如 users.ts、tasks.ts)的做法;要求为参数与返回值都定义校验器;推荐用索引配合 withIndex 而非 filter 做高效查询;用 ConvexError 抛出面向用户的错误;通过幂等 mutation、直接 patch、Promise.all 并行更新来减少乐观并发控制下的写冲突;区分公开函数与 internal 内部函数;使用 Id、Doc 等 TypeScript 类型。
代码质量:所有模式符合 @convex-dev/eslint-plugin,可安装做构建期校验:npm i @convex-dev/eslint-plugin --save-dev。插件强制四条规则:no-old-registered-function-syntax(使用带 handler 的对象语法)、require-argument-validators(所有函数需 args: {})、explicit-table-ids(数据库操作需表名)、import-wrong-runtime(Convex 运行时禁止 Node 导入)。文档见 https://docs.convex.dev/eslint。
使用方式:实现前先查阅最新文档,主要来源 https://docs.convex.dev/understanding/best-practices/,错误处理 https://docs.convex.dev/functions/error-handling,写冲突 https://docs.convex.dev/error#1,整体上下文 https://docs.convex.dev/llms.txt。
适用场景:使用 Convex 做后端与实时数据同步的 TypeScript 项目。
限制与注意:除非明确指示,不要运行 npx convex deploy,也不要运行任何 git 命令;常见陷阱包括用 filter 代替 withIndex、缺少返回值校验器、mutation 非幂等、不必要地先读后写、未处理 null 返回。