convex-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseConvex Development
Convex开发
Best practices for robust, secure, performant, cost-effective Convex backends.
构建健壮、安全、高性能且具成本效益的Convex后端的最佳实践。
Core Principle
核心原则
Deep modules via the pattern. Most logic should be plain TypeScript; query/mutation wrappers should be thin.
convex/model/convex/
_generated/ # MUST commit to git!
model/ # Business logic (testable, reusable)
users.ts # Public API (thin wrappers)
schema.ts通过模式实现深度模块化。大部分逻辑应采用纯TypeScript编写;查询/变更包装器应尽可能精简。
convex/model/convex/
_generated/ # 必须提交至git!
model/ # 业务逻辑(可测试、可复用)
users.ts # 公开API(轻量包装器)
schema.tsCritical Rules
关键规则
- ALWAYS commit - Required for type-checking, CI/CD, team productivity
convex/_generated/ - Index what you query - not
.withIndex()for efficient queries.filter() - Compound indexes for multi-field filters - If querying , create index
userId + statusto filter at index level, not post-fetch["userId", "status"] - Paginate everything - Never unbounded on user-facing queries
.collect() - Trust only - Never user-provided auth data
ctx.auth
- 务必提交- 这是类型检查、CI/CD、团队协作效率的必要条件
convex/_generated/ - 为查询创建索引 - 使用而非
.withIndex()来实现高效查询.filter() - 为多字段过滤创建复合索引 - 如果查询,创建索引
userId + status以在索引层面完成过滤,而非在获取数据后再处理["userId", "status"] - 所有查询均需分页 - 面向用户的查询绝不能使用无限制的
.collect() - 仅信任- 绝不使用用户提供的认证数据
ctx.auth
Quick Reference
快速参考
| Need | Use |
|---|---|
| Read data reactively | |
| Write to database | |
| External APIs, vector search | |
| Scheduled tasks | |
| 需求 | 用法 |
|---|---|
| 响应式读取数据 | |
| 写入数据库 | |
| 外部API调用、向量搜索 | |
| 定时任务 | |
Anti-Patterns Scanner
反模式扫描器
Run to detect common issues.
scripts/anti_patterns_scanner.py ./convex运行以检测常见问题。
scripts/anti_patterns_scanner.py ./convexDetailed References
详细参考资料
For comprehensive guidance, see:
- - Bandwidth optimization, indexing, pagination
references/cost-mitigation.md - - Vector search patterns, co-location decisions
references/embeddings-vectors.md - - Compound indexes, query segmentation, caching
references/query-performance.md - - Auth patterns, RLS, RBAC, convex-helpers
references/security-access.md - - Expand/Contract pattern, environment management
references/schema-migrations.md - - File organization, state machines, naming
references/architectural-patterns.md
如需全面指导,请参阅:
- - 带宽优化、索引、分页
references/cost-mitigation.md - - 向量搜索模式、共置决策
references/embeddings-vectors.md - - 复合索引、查询拆分、缓存
references/query-performance.md - - 认证模式、RLS、RBAC、convex-helpers
references/security-access.md - - 扩展/收缩模式、环境管理
references/schema-migrations.md - - 文件组织、状态机、命名规范
references/architectural-patterns.md
Philosophy
设计理念
- Cost First: Bandwidth is often the largest cost. Index aggressively, paginate everything.
- Security First: Never trust client input. Always use .
ctx.auth - Reactivity is Power: Use for real-time updates; don't forfeit with one-off fetches.
useQuery - Type Safety End-to-End: Leverage Convex's full type chain from database to UI.
- 成本优先:带宽通常是最大的成本支出。积极创建索引,所有查询均需分页。
- 安全优先:绝不信任客户端输入。始终使用。
ctx.auth - 响应性即优势:使用实现实时更新;不要通过一次性获取数据放弃这一优势。
useQuery - 端到端类型安全:充分利用Convex从数据库到UI的完整类型链。