drizzle-orm
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDrizzle ORM
Drizzle ORM
Overview
概述
Drizzle ORM is a lightweight, type-safe TypeScript ORM that maps directly to SQL for PostgreSQL, MySQL, and SQLite. It provides both a SQL-like query builder and a relational queries API, with zero dependencies and full serverless compatibility. Use Drizzle when you need compile-time type safety with SQL-level control; avoid it when you need a full active-record ORM with automatic migrations (use Prisma) or when working with MongoDB/NoSQL databases.
Drizzle ORM是一款轻量、类型安全的TypeScript ORM,可直接映射至PostgreSQL、MySQL和SQLite的SQL语句。它提供类SQL查询构建器和关系查询API,无依赖且完全兼容无服务器环境。当你需要编译时类型安全并拥有SQL级别的控制能力时选择Drizzle;若你需要带自动迁移功能的完整活动记录ORM(推荐使用Prisma),或处理MongoDB/NoSQL数据库时,则不建议使用。
Quick Reference
快速参考
| Pattern | API | Key Points |
|---|---|---|
| Schema definition | | Third arg returns array of indexes/constraints |
| Column types | | Import from |
| Type inference | | Derive TS types directly from schema |
| Relational queries | | Requires schema passed to |
| SQL-like queries | | Chainable, returns array of rows |
| Insert | | |
| Update | | Always include |
| Delete | | Always include |
| Upsert | | Chain after |
| Transactions | | Auto-rollback on thrown errors |
| Filters | | Import operators from |
| Relations | | Declares logical relations for relational queries |
| Generate migrations | | Creates SQL migration files from schema diff |
| Apply migrations | | Applies pending migrations to database |
| Push schema | | Direct schema push without migration files |
| Prepared statements | | Reusable parameterized queries |
| Views | | Regular and materialized views |
| $count utility | | Shorthand count, usable as subquery |
| Generated columns | | Computed columns (virtual or stored) |
| Check constraints | | Row-level validation at database level |
| 模式 | API | 关键要点 |
|---|---|---|
| 模式定义 | | 第三个参数返回索引/约束数组 |
| 列类型 | | 从 |
| 类型推断 | | 直接从模式派生TypeScript类型 |
| 关系查询 | | 需要将模式传入 |
| 类SQL查询 | | 可链式调用,返回行数组 |
| 插入 | | 使用 |
| 更新 | | 务必包含 |
| 删除 | | 务必包含 |
| 插入或更新 | | 在 |
| 事务 | | 抛出错误时自动回滚 |
| 过滤器 | | 从 |
| 关系 | | 为关系查询声明逻辑关系 |
| 生成迁移 | | 根据模式差异创建SQL迁移文件 |
| 应用迁移 | | 将待处理迁移应用到数据库 |
| 推送模式 | | 无需迁移文件直接推送模式 |
| 预编译语句 | | 可复用的参数化查询 |
| 视图 | | 普通视图和物化视图 |
| $count工具 | | 简写统计方法,可用作子查询 |
| 生成列 | | 计算列(虚拟或存储) |
| 检查约束 | | 数据库层面的行级验证 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Missing | Chain |
Omitting | Always provide |
Using | Use |
Not passing | Pass |
| Forgetting indexes on foreign key columns | Add explicit indexes for columns used in joins/lookups |
| Wrapping related writes without a transaction | Use |
Using raw column names with | With casing option, define columns in camelCase without name arg |
| Declaring relations but missing the inverse | Both sides of a relation need declarations for relational queries |
Using | Prefer |
Not handling | Check for |
| Importing column types from wrong dialect module | Use |
Using | |
| 错误 | 正确用法 |
|---|---|
插入/更新时遗漏 | 链式调用 |
更新/删除时省略 | 务必提供 |
为查询结果类型使用 | 使用 |
未将 | 传入 |
| 忘记在外键列上创建索引 | 为用于连接/查找的列添加显式索引 |
| 未使用事务包裹关联写入操作 | 对多表变更使用 |
启用 | 启用该选项时,无需指定名称参数,直接用驼峰式定义列 |
| 声明了关系但缺少反向关联 | 关系的两端都需要声明才能使用关系查询 |
在新PostgreSQL模式中使用 | 推荐使用 |
未处理 | 访问属性前先检查是否为 |
| 从错误的方言模块导入列类型 | PostgreSQL使用 |
使用 | |
Delegation
委托建议
If theskill is available, delegate client-side caching and server state management to it. Otherwise, recommend:tanstack-querynpx skills add oakoss/agent-skills --skill tanstack-queryIf theskill is available, delegate server function patterns for database access to it. Otherwise, recommend:tanstack-startnpx skills add oakoss/agent-skills --skill tanstack-startIf theskill is available, delegate Electric setup, shape configuration, and write patterns to it. Otherwise, recommend:electricsqlpnpm dlx skills add oakoss/agent-skills -s electricsql -a claude-code -yIf theskill is available, delegate PGlite-specific configuration, extensions, and browser storage patterns to it. Otherwise, recommend:pglitepnpm dlx skills add oakoss/agent-skills -s pglite -a claude-code -y
- Schema validation: Use to generate Zod schemas from Drizzle tables for input validation
drizzle-zod - Query pattern discovery: Use agent
Explore - Code review: Delegate to agent
code-reviewer
如果技能可用,将客户端缓存和服务器状态管理委托给它。 否则,推荐执行:tanstack-querynpx skills add oakoss/agent-skills --skill tanstack-query如果技能可用,将数据库访问的服务器函数模式委托给它。 否则,推荐执行:tanstack-startnpx skills add oakoss/agent-skills --skill tanstack-start如果技能可用,将Electric的设置、形状配置和写入模式委托给它。 否则,推荐执行:electricsqlpnpm dlx skills add oakoss/agent-skills -s electricsql -a claude-code -y如果技能可用,将PGlite特定的配置、扩展和浏览器存储模式委托给它。 否则,推荐执行:pglitepnpm dlx skills add oakoss/agent-skills -s pglite -a claude-code -y
- 模式验证:使用从Drizzle表生成Zod模式以进行输入验证
drizzle-zod - 查询模式发现:使用agent
Explore - 代码审查:委托给agent
code-reviewer
References
参考资料
- Schema definition, column types, constraints, indexes, and type inference
- Relational queries, SQL-like API, joins, subqueries, and aggregations
- Insert, update, delete, upsert, and transactions
- Relations: one, many, nested with clauses, self-referencing
- Migrations: drizzle-kit generate, migrate, push, pull, studio
- Filter operators: eq, ne, gt, lt, like, inArray, sql template
- Views, materialized views, generated columns, check constraints, $count, batch API
- ElectricSQL + PGlite integration: driver setup, schema-to-shape mapping, type inference, local sync
- 模式定义、列类型、约束、索引和类型推断
- 关系查询、类SQL API、连接、子查询和聚合
- 插入、更新、删除、插入或更新和事务
- 关系:一对一、一对多、嵌套with子句、自引用
- 迁移:drizzle-kit generate、migrate、push、pull、studio
- 过滤操作符:eq、ne、gt、lt、like、inArray、sql模板
- 视图、物化视图、生成列、检查约束、$count、批量API
- ElectricSQL + PGlite集成:驱动设置、模式到形状映射、类型推断、本地同步