Drizzle ORM
Drizzle is a TypeScript ORM and database toolkit consisting of two main packages: drizzle-orm (a type-safe ORM supporting PostgreSQL, MySQL, SQLite, SingleStore, and Gel/EdgeDB) and drizzle-kit (a CLI for schema migrations, introspection, and Drizzle Studio). It also includes schema validation packages (drizzle-zod, drizzle-valibot, drizzle-typebox, drizzle-arktype) and a database seeding tool (drizzle-seed).
Tech Stack
Key Features
- Multi-dialect SQL query builder
- SQL template tag and expression system
- Relational query API
- 15+ driver adapters
- Schema migration engine
- Database introspection (pull)
- Database push
- Drizzle Studio API
- Schema validation integrations
- Database seeding
- Query caching
- CLI with terminal UI
- ESLint plugin
- Knex and Kysely type integrations
Entry Points
Start here when working with this codebase
drizzle-orm/src/index.tsMain export barrel for all drizzle-orm public APIs including SQL primitives, column types, relations, and driver adapters
Start here when adding new core ORM features, understanding what's publicly exported, or tracing how users import drizzle-orm
drizzle-orm/src/pg-core/index.tsExports all PostgreSQL-specific column types, query builders, table definitions, policies, roles, and views
Start here when adding new PostgreSQL column types, query builder methods, or PG-specific features
drizzle-orm/src/mysql-core/index.tsExports all MySQL-specific column types, query builders, table definitions, and views
Start here when adding new MySQL column types, query builder methods, or MySQL-specific features
drizzle-orm/src/sqlite-core/index.tsExports all SQLite-specific column types, query builders, table definitions, and views
Start here when adding new SQLite column types, query builder methods, or SQLite-specific features
drizzle-orm/src/singlestore-core/index.tsExports all SingleStore-specific column types, query builders, and schema definitions
Start here when adding SingleStore-specific features or column types
drizzle-orm/src/gel-core/index.tsExports Gel (EdgeDB) dialect columns, query builders, policies, and views
Start here when working on Gel/EdgeDB dialect features
drizzle-orm/src/sql/index.tsCore SQL template tag, expressions, conditions, aggregate functions, and SQL primitives used by all dialects
Start here when modifying SQL generation, adding new SQL expressions, or changing how queries are built
drizzle-kit/src/cli/index.tsCLI command definitions and argument parsing for drizzle-kit commands (generate, migrate, push, pull, studio)
Start here when adding new CLI commands or modifying existing command behavior
drizzle-kit/src/serializer/index.tsSchema serialization/deserialization orchestration for converting Drizzle schemas to JSON snapshots
Start here when modifying how schemas are serialized for migration generation
drizzle-kit/src/api.tsProgrammatic API for drizzle-kit operations (generate, push, pull) without CLI
Start here when adding programmatic access to kit features or understanding the public kit API
drizzle-kit/src/serializer/studio.tsUnified POST endpoint handling all Drizzle Studio operations through type-discriminated requests
Start here when modifying Studio server behavior or adding new Studio operations
drizzle-orm/tsup.config.tstsup build configuration defining all entry points, output formats, and external dependencies for drizzle-orm
Start here when adding new subpath exports to drizzle-orm or changing build output
drizzle-kit/build.tsesbuild configuration for compiling and bundling drizzle-kit into distributable artifacts
Start here when modifying drizzle-kit build process or adding new bundled modules
drizzle-orm/src/cache/index.tsQuery caching abstraction with core cache interface and Upstash implementation
Start here when adding new cache providers or modifying caching behavior
How to Make Changes
Common modification patterns for this codebase
Add a new PostgreSQL column type
drizzle-orm/src/pg-core/columnsCreate a new column file (e.g., mytype.ts) defining the column builder and column classes
Pattern: Follow the pattern in drizzle-orm/src/pg-core/columns/integer.ts: export a builder class extending PgColumnBuilder, a column class extending PgColumn, and a factory function. The builder must implement build() returning the column instance, and the column must implement getSQLType() returning the SQL type string and mapFromDriverValue()/mapToDriverValue() if needed
drizzle-orm/src/pg-core/columns/index.tsRe-export the new column type from the columns barrel file
Pattern: Add an export line matching the existing pattern: export * from './mytype.ts' following the alphabetical ordering used in drizzle-orm/src/pg-core/columns/index.ts
drizzle-orm/src/pg-core/index.tsVerify the new column is transitively exported through the columns barrel
Pattern: The pg-core index.ts re-exports from './columns/index.ts' so no change needed if step 2 is done correctly
drizzle-kit/src/serializer/pgSerializer.tsAdd serialization support for the new column type so drizzle-kit can snapshot it
Pattern: Follow how existing column types are handled in the column serialization switch/if-else chain in pgSerializer.ts. Map the column class to its SQL type name and any type-specific parameters
drizzle-kit/src/serializer/pgSchema.tsIf the column type has special parameters, add them to the Zod schema for PG column snapshots
Pattern: Follow how enumerated types or array types add extra fields in the column schema definition in pgSchema.ts
drizzle-kit/src/introspect-pg.tsAdd introspection support so 'drizzle-kit pull' can generate code for this column type from an existing database
Pattern: Follow how existing types are mapped from database introspection results to Drizzle column builder calls in introspect-pg.ts
integration-tests/tests/pgAdd integration tests covering insert, select, update, and filtering with the new column type
Pattern: Follow the test structure in existing PG integration test files under integration-tests/tests/pg/ that define a table with the column, insert values, and assert round-trip correctness
Conventions to follow:
Add a new MySQL column type
drizzle-orm/src/mysql-core/columnsCreate a new column file defining builder and column classes
Pattern: Follow the pattern in drizzle-orm/src/mysql-core/columns/int.ts: export a builder class extending MySqlColumnBuilder, a column class extending MySqlColumn, and a factory function
drizzle-orm/src/mysql-core/columns/index.tsRe-export the new column type from the columns barrel file
Pattern: Add export * from './mytype.ts' matching the existing exports in drizzle-orm/src/mysql-core/columns/index.ts
drizzle-kit/src/serializer/mysqlSerializer.tsAdd serialization support for the new column type
Pattern: Follow how existing MySQL column types are serialized in mysqlSerializer.ts
drizzle-kit/src/serializer/mysqlSchema.tsAdd any type-specific parameters to the MySQL column Zod schema if needed
Pattern: Follow existing column parameter definitions in mysqlSchema.ts
drizzle-kit/src/introspect-mysql.tsAdd introspection mapping for the new column type
Pattern: Follow how existing MySQL types are mapped in introspect-mysql.ts
Conventions to follow:
Add a new SQLite column type
drizzle-orm/src/sqlite-core/columnsCreate a new column file defining builder and column classes
Pattern: Follow the pattern in drizzle-orm/src/sqlite-core/columns/integer.ts: export a builder class extending SQLiteColumnBuilder, a column class extending SQLiteColumn, and a factory function
drizzle-orm/src/sqlite-core/columns/index.tsRe-export the new column type
Pattern: Add export * from './mytype.ts' matching existing exports in drizzle-orm/src/sqlite-core/columns/index.ts
drizzle-kit/src/serializer/sqliteSerializer.tsAdd serialization support for the new column type
Pattern: Follow how existing SQLite column types are serialized in sqliteSerializer.ts
drizzle-kit/src/introspect-sqlite.tsAdd introspection mapping for the new column type
Pattern: Follow how existing SQLite types are mapped in introspect-sqlite.ts
Conventions to follow:
Add a new database driver adapter
drizzle-orm/srcCreate a new directory for the driver (e.g., drizzle-orm/src/my-driver/) with session.ts, driver.ts, and index.ts files
Pattern: Follow the structure of drizzle-orm/src/node-postgres/ which has session.ts (NodePgSession extending PgSession), driver.ts (NodePgDriver and drizzle() factory), and index.ts (barrel exports)
drizzle-orm/src/my-driver/session.tsCreate a session class that extends the dialect's base session and implements execute(), all(), and transaction methods
Pattern: Follow drizzle-orm/src/node-postgres/session.ts: NodePgSession extends PgSession, implements execute() that calls the underlying driver client, wraps results in proper format, and handles prepared statements
drizzle-orm/src/my-driver/driver.tsCreate a driver class and the public drizzle() factory function that users call to create a database instance
Pattern: Follow drizzle-orm/src/node-postgres/driver.ts: export a drizzle() function that accepts the driver client and optional config, creates a session, and returns a PgDatabase (or MySqlDatabase/BaseSQLiteDatabase) instance
drizzle-orm/src/my-driver/index.tsCreate barrel exports for the driver module
Pattern: Follow drizzle-orm/src/node-postgres/index.ts: re-export session, driver, and the drizzle factory function
drizzle-orm/tsup.config.tsAdd the new driver as an entry point in the tsup build configuration
Pattern: Add 'src/my-driver/index.ts' to the entry array in drizzle-orm/tsup.config.ts, following how 'src/node-postgres/index.ts' and other drivers are listed
drizzle-orm/package.jsonAdd subpath exports for the new driver module and add the underlying driver package as an optional peerDependency
Pattern: Follow how './node-postgres' is defined in the exports map and peerDependencies/peerDependenciesMeta in drizzle-orm/package.json
Conventions to follow:
Add a new query builder method (e.g., new clause like RETURNING, ON CONFLICT)
drizzle-orm/src/pg-core/query-buildersAdd the new method to the appropriate query builder class (select.ts, insert.ts, update.ts, or delete.ts)
Pattern: Follow how .where() is implemented in drizzle-orm/src/pg-core/query-builders/select.ts: add a method that stores the clause data in the builder's config object and returns 'this' for chaining. The method should accept SQL expressions from drizzle-orm/src/sql/index.ts
drizzle-orm/src/pg-core/dialect.tsModify the dialect's SQL generation method to include the new clause in the final SQL output
Pattern: Follow how PgDialect.buildSelectQuery() in drizzle-orm/src/pg-core/dialect.ts reads config properties and appends SQL chunks. Add your clause generation in the correct position within the SQL statement
drizzle-orm/src/mysql-core/query-buildersIf the clause applies to MySQL, add the equivalent method to MySQL query builders
Pattern: Follow the same pattern as step 1 but in drizzle-orm/src/mysql-core/query-builders/
drizzle-orm/src/mysql-core/dialect.tsIf applicable, modify MySQL dialect SQL generation
Pattern: Follow drizzle-orm/src/mysql-core/dialect.ts buildSelectQuery() or equivalent method
drizzle-orm/src/sqlite-core/query-buildersIf the clause applies to SQLite, add the equivalent method to SQLite query builders
Pattern: Follow the same pattern in drizzle-orm/src/sqlite-core/query-builders/
drizzle-orm/src/sqlite-core/dialect.tsIf applicable, modify SQLite dialect SQL generation
Pattern: Follow drizzle-orm/src/sqlite-core/dialect.ts buildSelectQuery() or equivalent method
Conventions to follow:
Add a new SQL function or expression
drizzle-orm/src/sql/expressions/index.tsAdd the new SQL function as an exported function that returns a SQL expression
Pattern: Follow how existing functions like eq(), and(), or(), sql.raw() are defined in drizzle-orm/src/sql/expressions/index.ts. Return a new SQL instance using the sql template tag or SQL constructor from drizzle-orm/src/sql/sql.ts
drizzle-orm/src/sql/index.tsRe-export the new function from the SQL barrel file if not already covered by the expressions re-export
Pattern: Check drizzle-orm/src/sql/index.ts to see how expressions are re-exported
drizzle-orm/src/index.tsEnsure the new function is exported from the main drizzle-orm entry point
Pattern: Check that drizzle-orm/src/index.ts re-exports from './sql/index.ts' which should transitively include the new function
Conventions to follow:
Coding Conventions
Standards and patterns used in this codebase
Validation/integration packages use Rollup with consistent plugin chain (typescript2, resolve, terser) outputting both ESM (.mjs) and CJS (.cjs) formats, while the core ORM uses tsup for building.
export default defineConfig([{ input: 'src/index.ts', output: [{ format: 'esm', file: 'dist/index.mjs' }, { format: 'cjs', file: 'dist/index.cjs' }], plugins: [typescript2(), resolve(), terser()] }])All packages use Vitest as the test runner with a consistent vitest.config.ts pattern using defineConfig. Test timeouts are set generously (typically 100000ms) for database integration tests.
import { defineConfig } from 'vitest/config';
export default defineConfig({ test: { include: ['tests/**/*.test.ts'], testTimeout: 100000 } });External dependencies (including the parent drizzle-orm package and validation libraries) are explicitly listed in the Rollup external array to prevent bundling peer dependencies.
external: ['drizzle-orm', 'drizzle-orm/pg-core', 'drizzle-orm/mysql-core', 'drizzle-orm/sqlite-core', 'drizzle-orm/singlestore-core', 'drizzle-orm/gel-core', 'drizzle-orm/column', 'zod']Rollup configs consistently use @rollup/plugin-typescript2 (not the standard @rollup/plugin-typescript) with tsconfig set to 'tsconfig.build.json', separating build config from development config.
typescript2({ tsconfig: 'tsconfig.build.json' })Test files follow the naming pattern *.test.ts and are located in a top-level 'tests/' directory. Vitest configs use glob patterns like 'tests/**/*.test.ts' for test discovery.
test: { include: ['tests/**/*.test.ts'] }The drizzle-seed package supports multiple entry points in its Rollup config to handle different database dialect exports, each producing separate ESM and CJS output files.
defineConfig([{ input: 'src/index.ts', output: [{ format: 'esm', file: 'dist/index.mjs' }, { format: 'cjs', file: 'dist/index.cjs' }] }])The monorepo follows a flat package structure where each package (drizzle-orm, drizzle-kit, drizzle-zod, drizzle-valibot, drizzle-typebox, drizzle-arktype, drizzle-seed, eslint-plugin-drizzle) is a top-level directory with its own build and test configuration.
drizzle-orm/, drizzle-kit/, drizzle-zod/, drizzle-valibot/, drizzle-typebox/, drizzle-arktype/, drizzle-seed/TypeScript configuration is split into separate tsconfig files: a base tsconfig.json for development/IDE and a tsconfig.build.json for production builds with stricter or different settings.
typescript2({ tsconfig: 'tsconfig.build.json' })