← 返回
开发者工具 中文

Drizzle

Build type-safe database queries with Drizzle ORM patterns.
使用 Drizzle ORM 模式构建类型安全的数据库查询。
ivangdavila
开发者工具 clawhub v1.0.0 1 版本 99754 Key: 无需
★ 2
Stars
📥 1,582
下载
💾 24
安装
1
版本
#latest

概述

Schema Definition

  • Export every table from schema file — queries fail silently if table isn't exported
  • Use $inferSelect for query return types, $inferInsert for insert input — they differ (select has defaults filled, insert has optionals)
  • Define relations() in a separate call, not inline with table — Drizzle separates schema from relations

Query Syntax Traps

  • Conditions use functions, not objects: where: eq(users.id, 5) not where: { id: 5 } — Prisma syntax doesn't work
  • Combine conditions with and() / or(): where: and(eq(users.active, true), gt(users.age, 18))
  • db.query.users.findMany() for relational queries with with:, db.select().from(users) for SQL-like — mixing them causes type errors

Migrations

  • drizzle-kit push is dev-only (destructive) — production needs drizzle-kit generate then drizzle-kit migrate
  • Schema changes require regenerating migrations — editing generated SQL breaks the migration hash
  • Set strict: true in drizzle.config.ts to catch schema drift before it hits production

Driver-Specific

  • PostgreSQL: use pgTable, imports from drizzle-orm/pg-core
  • MySQL: use mysqlTable, imports from drizzle-orm/mysql-core
  • SQLite: use sqliteTable, imports from drizzle-orm/sqlite-core
  • Mixing imports across drivers compiles but fails at runtime with cryptic errors

Performance

  • Wrap multi-query operations in db.transaction(async (tx) => {}) — Drizzle doesn't auto-batch
  • Use .prepare() for queries executed repeatedly — skips query building overhead
  • Add .limit() to every findMany() / select() — no default limit means full table scans

Common Mistakes

  • Forgetting await on queries returns a Promise, not results — TypeScript doesn't catch this if you ignore the return
  • returning() is required to get inserted/updated rows back — without it you get { rowCount } only
  • JSON columns: PostgreSQL uses jsonb(), MySQL uses json() — wrong function = wrong serialization

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-28 22:56 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

developer-tools

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 68 📥 180,139
ai-intelligence

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,358 📥 318,294
developer-tools

Gog

steipete
Google Workspace 命令行工具,支持 Gmail、日历、云端硬盘、通讯录、表格和文档。
★ 921 📥 185,790