
Drizzle Orm Patterns
Define type-safe Drizzle schemas, relations, queries, transactions, and Drizzle Kit migrations across supported SQL databases.
Overview
Drizzle ORM Patterns is an agent skill for the Build phase that teaches type-safe Drizzle schema, query, transaction, and migration patterns across major SQL databases.
Install
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill drizzle-orm-patternsWhat is this skill?
- Schema patterns for PostgreSQL, MySQL, SQLite, MSSQL, and CockroachDB via pgTable/mysqlTable/sqliteTable/mssqlTable
- CRUD, relations (1:1, 1:N, N:M), joins, and aggregations with typed query builders
- Transaction patterns with rollback guidance
- Drizzle Kit migration setup and workflow
- Quick-reference table for per-database imports and common operations
- Supports 5 database dialects: PostgreSQL, MySQL, SQLite, MSSQL, CockroachDB
Adoption & trust: 1.5k installs on skills.sh; 271 GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
You are building a TypeScript backend and need consistent, type-safe Drizzle patterns for schemas, relations, and migrations without re-reading scattered docs.
Who is it for?
Indie SaaS and API builders standardizing on Drizzle ORM in TypeScript with one or more supported SQL engines.
Skip if: ORM-free raw SQL-only stacks, NoSQL-only products, or teams standardized on Prisma/TypeORM who will not adopt Drizzle.
When should I use this skill?
Any Drizzle ORM work: schemas, relations, type-safe queries, transactions, or Drizzle Kit migrations.
What do I get? / Deliverables
You get dialect-correct schema definitions, CRUD and relation queries, transaction blocks, and Drizzle Kit migration steps aligned to your database.
- Schema and relation definitions using dialect-correct table helpers
- Query and transaction snippets
- Migration configuration aligned with Drizzle Kit
Recommended Skills
Journey fit
Build backend is the canonical home for ORM schema design, CRUD, joins, and migration setup while shipping the product data layer. Backend subphase matches database modeling and type-safe data access—not frontend UI or distribution work.
How it compares
Procedural Drizzle recipe skill—not an MCP database server and not a one-click schema generator outside your repo.
Common Questions / FAQ
Who is drizzle-orm-patterns for?
Solo developers and small teams writing TypeScript backends who want the agent to apply Drizzle ORM conventions while coding schemas and queries.
When should I use drizzle-orm-patterns?
During Build when defining tables, wiring relations, writing type-safe queries, or setting up Drizzle Kit migrations for PostgreSQL, MySQL, SQLite, MSSQL, or CockroachDB.
Is drizzle-orm-patterns safe to install?
It may edit and write project files via allowed tools; review the Security Audits panel on this page and keep migrations in version control before applying to production.
SKILL.md
READMESKILL.md - Drizzle Orm Patterns
# Drizzle ORM Patterns ## Overview Expert guide for building type-safe database applications with Drizzle ORM. Covers schema definition, relations, queries, transactions, and migrations for all supported databases. ## When to Use - Defining database schemas with tables, columns, and constraints - Creating relations between tables (one-to-one, one-to-many, many-to-many) - Writing type-safe CRUD queries - Implementing complex joins and aggregations - Managing database transactions with rollback - Setting up migrations with Drizzle Kit - Working with PostgreSQL, MySQL, SQLite, MSSQL, or CockroachDB ## Quick Reference | Database | Table Function | Import | |----------|---------------|--------| | PostgreSQL | `pgTable()` | `drizzle-orm/pg-core` | | MySQL | `mysqlTable()` | `drizzle-orm/mysql-core` | | SQLite | `sqliteTable()` | `drizzle-orm/sqlite-core` | | MSSQL | `mssqlTable()` | `drizzle-orm/mssql-core` | | Operation | Method | Example | |-----------|--------|---------| | Insert | `db.insert()` | `db.insert(users).values({...})` | | Select | `db.select()` | `db.select().from(users).where(eq(...))` | | Update | `db.update()` | `db.update(users).set({...}).where(...)` | | Delete | `db.delete()` | `db.delete(users).where(...)` | | Transaction | `db.transaction()` | `db.transaction(async (tx) => {...})` | ## Instructions 1. **Identify your database dialect** - Choose PostgreSQL, MySQL, SQLite, MSSQL, or CockroachDB 2. **Define your schema** - Use the appropriate table function (pgTable, mysqlTable, etc.) 3. **Set up relations** - Define relations using `relations()` or `defineRelations()` 4. **Initialize the database client** - Create your Drizzle client with proper credentials 5. **Write queries** - Use the query builder for type-safe CRUD operations 6. **Handle transactions** - Wrap multi-step operations in transactions when needed 7. **Set up migrations** - Configure Drizzle Kit for schema management ## Examples ### Example 1: Basic Schema and Query ```typescript import { pgTable, serial, text } from 'drizzle-orm/pg-core'; import { drizzle } from 'drizzle-orm/node-postgres'; import { eq } from 'drizzle-orm'; export const users = pgTable('users', { id: serial('id').primaryKey(), name: text('name').notNull(), email: text('email').notNull().unique(), }); const db = drizzle(process.env.DATABASE_URL); const [user] = await db.select().from(users).where(eq(users.id, 1)); ``` ### Example 2: CRUD Operations ```typescript import { eq } from 'drizzle-orm'; // Insert const [newUser] = await db.insert(users).values({ name: 'John', email: 'john@example.com', }).returning(); // Update await db.update(users) .set({ name: 'John Updated' }) .where(eq(users.id, 1)); // Delete await db.delete(users).where(eq(users.id, 1)); ``` ### Example 3: Transaction with Rollback ```typescript await db.transaction(async (tx) => { const [from] = await tx.select().from(accounts) .where(eq(accounts.userId, fromId)); if (from.balance < amount) { tx.rollback(); } await tx.update(accounts) .set({ balance: sql`${accounts.balance} - ${amount}` }) .where(eq(accounts.userId, fromId)); }); ``` See [references/transactions.md](references/transactions.md) for advanced transaction patterns. ## Best Practices 1. **Type Safety**: Always use TypeScript and leverage `$inferInsert` / `$inferSelect` 2. **Relations**: Define relations using the relations() API for nested queries 3. **Transactions**: Use transactions for multi-step