drizzle-orm-d1
| Type-safe ORM for Cloudflare D1 databases using Drizzle. Use when: building D1 database schemas, writing type-safe SQL queries, managing migrations with Drizzle Kit, defining table relations, implementing prepared statements, using D1 batch API, or encountering D1_ERROR, transaction errors, foreig
By secondsky · 373 installs
npx skills add secondsky/claude-skills --skill drizzle-orm-d1
Source repository · Upstream listing
Drizzle ORM for Cloudflare D1
Status : Production Ready ✅
Last Updated : 2025 12 14
Latest Version : drizzle orm@0.45.2, drizzle kit@0.31.10
Dependencies : cloudflare d1, cloudflare worker base
Quick Start (10 Minutes)
1. Install Drizzle
2. Configure Drizzle Kit
Create drizzle.config.ts :
drizzle kit 0.30/0.31 note : dialect and dbCredentials are now strictly
validated. A config that omits dialect or uses the old connectionString /
uri keys will fail validation. For D1 use dialect: 'sqlite' +
driver: 'd1 http' with accountId / databaseId / token (or wrangler based
credentials). The runtime migrate() signature in drizzle orm/<driver /migrator
is unchanged from 0.36 → 0.45 (it changes only in 1.0 beta, which is out of
scope for ^0.45 ).
3. Define Schema
Create src/db/schema.ts :
4. Generate & Apply Migrations
5. Query in Worker
Critical Rules
Always Do
Rule Why
Use drizzle kit generate for migrations Never write SQL manually
Test migrations locally first local before remote
Use .get() for single results Returns first row or undefined
Use db.batch() for transactions D1 doesn't support SQL BEGIN/COMMIT
Use integer with mode: 'timestamp' for dates D1 has no native date type
Use .$defaultFn() for dynamic defaults Not .default() for functions
Never Do
Rule Why
Use SQL BEGIN TRANSACTION D1 requires batch API (Error 1)
Mix drizzle kit migrate and wrangler apply Use Wrangler only
Use drizzle kit push for production Use generate + apply
Commit credentials in drizzle.config.ts Use env vars
Use .default() for function calls Use .$defaultFn() instead
Top 5 Critical Errors
Error Solution
1 D1 ERROR: Cannot use BEGIN TRANSACTION Use db.batch([...]) instead of db.transaction()
2 FOREIGN KEY constraint failed Define cascading: .references(() = users.id, { onDelete: 'cascade' })
3 env.DB is undefined Ensure binding in wrangler.jsonc matches env.DB
4 No such module "wrangler" Use import { drizzle } from 'drizzle orm/d1'
5 Type instantiation excessively deep Use InferSelectModel<typeof users for explicit types
See : references/error catalog.md for all 12 errors with complete solutions.
Common Patterns Summary
Pattern Use Case Template
CRUD Operations Basic database operations templates/basic queries.ts
Relations & Joins Nested queries, manual joins templates/relations queries.ts
Batch Operations Transactions (D1 batch API) templates/transactions.ts
Schema Design Naming, indexes, soft deletes references/schema patterns.md
Configuration Summary
File Purpose Template
drizzle.config.ts Drizzle Kit configuration templates/drizzle.config.ts
wrangler.jsonc D1 binding setup references/wrangler setup.md
package.json npm scripts for migrations templates/package.json
npm scripts:
Migration Workflow
Step Command Notes
1. Edit schema Edit src/db/schema.ts Make changes
2. Generate npm run db:generate Creates SQL migration
3. Test local npm run db:migrate:local Verify locally
4. Deploy code npm run deploy Push to Cloudflare
5. Apply prod npm run db:migrate:remote Apply migration
See : references/migration workflow.md for complete workflow.
TypeScript Type Inference
When to Load References
Reference Load When...
references/error catalog.md Debugging D1 errors, transaction failures, binding issues
references/schema patterns.md Designing schemas, naming conventions, indexes, soft deletes
references/migration workflow.md Setting up or troubleshooting migrations
references/query builder api.md Complex queries, operators, joins syntax
references/wrangler setup.md Configuring wrangler.jsonc for D1
references/common errors.md Quick error lookup
Bundled Resources
Templates : basic schema.ts , basic queries.ts , transactions.ts , relations queries.ts , prepared statements.ts , drizzle.config.ts , package.json
References : error catalog.md , schema patterns.md , migration workflow.md , query builder api.md , wrangler setup.md , common errors.md , links to official docs.md
Dependencies
Secure Installation
When installing Drizzle ORM and D1 driver packages, follow supply chain security best practices:
Block post install scripts — npm config set ignore scripts true (or Bun: disabled by default)
Cooldown period — Wait 7 days for new package versions to be vetted by the community
Audit before installing — Run socket package score npm <pkg or use socket npm install <pkg to check packages
Load the dependency upgrade skill for full security configuration including Socket CLI integration, cooldown setup, lockfile validation, and CI enforcement.
Official Documentation
Drizzle ORM : https://orm.drizzle.team/
Drizzle with D1 : https://orm.drizzle.team/docs/connect cloudflare d1
Drizzle Kit : https://orm.drizzle.team/docs/kit overview
GitHub : https://github.com/drizzle team/drizzle orm
Token Savings : ~65% (comprehensive patterns in references)
Error Prevention : 100% (all 12 documented issues)
Ready for production! ✅