Drizzle ORM
Drizzle ORM cheat sheet with schema definition, queries, relations, migrations, and TypeScript-first database management examples.
Database Setup
Connect Drizzle ORM to PostgreSQL, MySQL, or SQLite using the unified drizzle() API.
PostgreSQL Setup
Connect to PostgreSQL using node-postgres or postgres.js drivers.
MySQL Setup
Connect to MySQL using mysql2 driver.
SQLite Setup
Connect to SQLite using better-sqlite3 or libsql drivers.
Drizzle Config File
Configure drizzle-kit for migrations and studio.
Schema Definition
Define tables, columns, types, and constraints using Drizzle's type-safe schema builders.
Basic Table & Column Types
Define a table with common column types and constraints.
Enums & Custom Types
Define PostgreSQL enums and use them in table schemas.
Indexes & Constraints
Add indexes, unique constraints, and composite keys to tables.
Foreign Keys
Define foreign key references between tables.
Type Inference from Schema
Extract TypeScript types from your Drizzle schema definitions.
🔗 Relations
Define relationships between tables for Drizzle's relational query builder.
One-to-Many Relation
Define a one-to-many relationship between users and posts.
One-to-One Relation
Define a one-to-one relationship between users and profiles.
Many-to-Many Relation
Define a many-to-many relationship using a junction table.
Querying with Relations
Use the relational query builder to fetch nested data.
Select Queries
Build type-safe SELECT queries with filters, ordering, and pagination.
Basic Select
Select all or specific columns from a table.
Where Clauses & Operators
Filter queries with comparison and logical operators.
Ordering, Limit & Offset
Sort results and implement pagination with limit/offset.
✏️ Insert / Update / Delete
Mutate data with type-safe insert, update, and delete operations.
Insert Rows
Insert single or multiple rows with optional returning clause.
Upsert (On Conflict)
Insert or update on conflict using onConflictDoUpdate or onConflictDoNothing.
Update Rows
Update existing rows with type-safe set and where clauses.
Delete Rows
Delete rows from tables with type-safe conditions.
🔗 Joins
Combine data from multiple tables using SQL-level joins.
Inner Join
Return rows that have matching values in both tables.
Left / Right / Full Join
Return all rows from one or both tables, even without matches.
📊 Aggregations
Perform aggregate calculations with groupBy and having clauses.
Aggregate Functions
Use count, sum, avg, min, and max for data aggregation.
Group By & Having
Group results and filter aggregated data.
🔒 Transactions
Execute multiple operations atomically with transaction support.
Basic Transaction
Wrap multiple queries in an ACID transaction that auto-rolls back on error.
Nested Transactions (Savepoints)
Use nested transactions that create savepoints for partial rollback.
Prepared Statements & Raw SQL
Optimize query performance with prepared statements and raw SQL expressions.
Prepared Statements
Pre-compile queries for repeated execution with different parameters.
Raw SQL & Dynamic Queries
Use raw SQL expressions and build dynamic queries conditionally.
📦 Migrations
Manage database schema changes with drizzle-kit CLI commands.
Generate & Run Migrations
Generate SQL migration files from schema changes and apply them.
Migration Workflow
Typical development workflow with schema changes and migrations.
Programmatic Migrations
Run migrations from your application code at startup.