drizzle-orm-patterns
Provides comprehensive Drizzle ORM patterns for schema definition, CRUD operations, relations, queries, transactions, and migrations. Proactively use for any Drizzle ORM development including defining database schemas, writing type-safe queries, implementing relations, managing transactions, and setting up migrations with Drizzle Kit. Supports PostgreSQL, MySQL, SQLite, MSSQL, and CockroachDB.
Security Assessment
Detected risks:
About drizzle-orm-patterns
A comprehensive reference for building type-safe database applications with Drizzle ORM, covering schema definition, CRUD operations, relations, queries, transactions, and migrations across PostgreSQL, MySQL, SQLite, MSSQL, and CockroachDB. It is meant to be used proactively for any Drizzle development, from defining schemas with tables, columns, and constraints to writing type-safe queries, implementing relations, managing transactions, and setting up migrations with Drizzle Kit. Quick-reference tables map each database to its table function and import path (for example pgTable from drizzle-orm/pg-core) and list the core operations: db.insert(), db.select(), db.update(), db.delete(), and db.transaction().
The recommended workflow is to identify the database dialect, define the schema with the appropriate table function, set up relations using relations() or defineRelations(), initialize the Drizzle client with proper credentials, write queries with the type-safe query builder, wrap multi-step operations in transactions when needed, and configure Drizzle Kit for migrations. Worked examples show a basic schema with pgTable and a typed select, insert/update/delete with returning() and eq() filters, and a transaction that reads balances, calls tx.rollback() when funds are insufficient, and updates balances with a sql template expression.
Best practices emphasize leveraging $inferInsert and $inferSelect for type safety, defining relations for nested queries, using transactions for operations that must succeed together, choosing generate plus migrate in production versus push in development, indexing frequently queried columns and foreign keys, preferring soft deletes via a deletedAt timestamp, and using cursor-based pagination. Constraints and warnings note that references should be defined with arrow functions to avoid circular dependencies, that tx.rollback() throws an exception, that not all databases support returning(), that large batch inserts may hit database limits, and that migrations should be tested in staging first. Additional reference files cover schema definition, relations with v1 and v2 syntax, queries and joins, transactions, migrations, and common patterns such as upsert, full-text search, and audit trails.
FAQ
Which databases does this skill support?
It supports PostgreSQL, MySQL, SQLite, MSSQL, and CockroachDB. Each dialect uses its own table function and import, such as pgTable from drizzle-orm/pg-core or sqliteTable from drizzle-orm/sqlite-core.
How should I define foreign key references to avoid circular dependency issues?
Always define references using arrow functions, for example () => table.column. The document calls out that this avoids circular dependency issues between tables.
What is the recommended migration approach for production versus development?
Use generate plus migrate in production and push for development. The skill also advises always testing migrations in staging before applying them to production.
How does transaction rollback behave in Drizzle?
Calling tx.rollback() throws an exception, so you may need to wrap it in try/catch. The transaction example uses it to abort a balance transfer when funds are insufficient.
How do I keep my queries type-safe?
Use TypeScript and leverage the $inferInsert and $inferSelect helpers, and define relations with the relations() API for nested queries. The query builder methods like db.select().from().where(eq(...)) are type-safe by design.
All Files
10 filesInstall drizzle-orm-patterns
Quick Setup:
- Copy the skill folder to
.claude/skills/ - Claude will automatically detect and use the skill
Repository
giuseppe-trisciuoglio/developer-kit