src/content/docs/drizzle-config-file.mdx
import CodeTab from "@mdx/CodeTab.astro"; import CodeTabs from "@mdx/CodeTabs.astro"; import Section from "@mdx/Section.astro"; import Tab from "@mdx/Tab.astro"; import Tabs from "@mdx/Tabs.astro"; import Callout from "@mdx/Callout.astro"; import SchemaFilePaths from "@mdx/SchemaFilePaths.mdx" import Prerequisites from "@mdx/Prerequisites.astro" import Dialects from "@mdx/Dialects.mdx" import Drivers from "@mdx/Drivers.mdx" import DriversExamples from "@mdx/DriversExamples.mdx" import Npx from "@mdx/Npx.astro"
Drizzle Kit lets you declare configuration options in TypeScript or JavaScript configuration files.
export default defineConfig({ dialect: "postgresql", schema: "./src/schema.ts", out: "./drizzle", });
</CodeTab>
<CodeTab>
```js
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./src/schema.ts",
out: "./drizzle",
});
Example of an extended config file
import { defineConfig } from "drizzle-kit";
export default defineConfig({
out: "./drizzle",
dialect: "postgresql",
schema: "./src/schema.ts",
driver: "pglite",
dbCredentials: {
url: "./database/",
},
extensionsFilters: ["postgis"],
schemaFilter: "public",
tablesFilter: "*",
introspect: {
casing: "camel",
},
migrations: {
prefix: "timestamp",
table: "__drizzle_migrations__",
schema: "public",
},
entities: {
roles: {
provider: '',
exclude: [],
include: []
}
},
breakpoints: true,
strict: true,
verbose: true,
});
You can have multiple config files in the project, it's very useful when you have multiple database stages or multiple databases or different databases on the same project: <Npx> drizzle-kit generate --config=drizzle-dev.config.ts drizzle-kit generate --config=drizzle-prod.config.ts </Npx>
š¦ <project root>
ā š drizzle
ā š src
ā š .env
ā š drizzle-dev.config.ts
ā š drizzle-prod.config.ts
ā š package.json
ā š tsconfig.json
out param lets you define folder for your migrations, it's optional and drizzle by default.
It's very useful since you can have many separate schemas for different databases in the same project
and have different migration folders for them.
Migration folder contains folders with .sql migration files which is used by drizzle-kit
export default defineConfig({ dialect: "postgresql", // "mysql" | "sqlite" | "postgresql" | "turso" | "singlestore" | "mssql" schema: "./src/schema/*", out: "./drizzle", });
</Section>
## ---
### `dialect`
<rem025/>
Dialect of the database you're using
| | |
| :------------ | :----------------------------------- |
| type | <Dialects/> |
| default | -- |
| commands | `generate` `migrate` `push` `pull` `check` `up` |
<rem025/>
```ts {4}
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "mysql",
});
schemaglob
based path to drizzle schema file(s) or folder(s) contaning schema files.
| type | string string[] |
| default | -- |
| commands | generate push |
outDefines output folder of your SQL migration files, json snapshots of your schema and schema.ts from drizzle-kit pull command.
| type | string string[] |
| default | drizzle |
| commands | generate migrate push pull check up |
export default defineConfig({ out: "./drizzle", });
### `driver`
<rem025/>
Drizzle Kit automatically picks available database driver from your current project based on the provided `dialect`,
yet some vendor specific databases require a different subset of connection params.
`driver` option let's you explicitely pick those exceptions drivers.
| | |
| :------------ | :----------------- |
| type | <Drivers/> |
| default | -- |
| commands | `migrate` `push` `pull` |
<rem025/>
<DriversExamples/>
## ---
### `dbCredentials`
<rem025/>
Database connection credentials in a form of `url`,
`user:password@host:port/db` params or exceptions drivers(<Drivers/>) specific connection options.
| | |
| :------------ | :----------------- |
| type | union of drivers connection options |
| default | -- |
| commands | `migrate` `push` `pull` |
<rem025/>
<CodeTabs items={["PostgreSQL", "MySQL", "SQLite", "Turso", "Cloudflare D1", "AWS Data API", "PGLite"]}>
<Section>
```ts
import { defineConfig } from 'drizzle-kit'
export default defineConfig({
dialect: "postgresql",
dbCredentials: {
url: "postgres://user:password@host:port/db",
}
});
import { defineConfig } from 'drizzle-kit'
// via connection params
export default defineConfig({
dialect: "postgresql",
dbCredentials: {
host: "host",
port: 5432,
user: "user",
password: "password",
database: "dbname",
ssl: true, // can be boolean | "require" | "allow" | "prefer" | "verify-full" | options from node:tls
}
});
export default defineConfig({ dialect: "mysql", dbCredentials: { url: "mysql://user:password@host:port/db", } });
```ts
import { defineConfig } from 'drizzle-kit'
// via connection params
export default defineConfig({
dialect: "mysql",
dbCredentials: {
host: "host",
port: 5432,
user: "user",
password: "password",
database: "dbname",
ssl: "...", // can be: string | SslOptions (ssl options from mysql2 package)
}
});
export default defineConfig({ dialect: "sqlite", dbCredentials: { url: ":memory:", // inmemory database // or url: "sqlite.db", // or url: "file:sqlite.db" // file: prefix is required by libsql } });
```ts
import { defineConfig } from 'drizzle-kit'
export default defineConfig({
dialect: "turso",
dbCredentials: {
url: "libsql://acme.turso.io" // remote Turso database url
authToken: "...",
// or if you need local db
url: ":memory:", // inmemory database
// or
url: "file:sqlite.db", // file: prefix is required by libsql
}
});
import { defineConfig } from 'drizzle-kit'
export default defineConfig({
dialect: "sqlite",
driver: "d1-http",
dbCredentials: {
accountId: "",
databaseId: "",
token: "",
}
});
import { defineConfig } from 'drizzle-kit'
export default defineConfig({
dialect: "postgresql",
driver: "aws-data-api",
dbCredentials: {
database: "database",
resourceArn: "resourceArn",
secretArn: "secretArn",
},
});
import { defineConfig } from 'drizzle-kit'
export default defineConfig({
dialect: "postgresql",
driver: "pglite",
dbCredentials: {
url: "./database/", // database folder path
}
});
migrationsWhen running drizzle-kit migrate - drizzle will records about
successfully applied migrations in your database in log table named __drizzle_migrations in public schema(PostgreSQL only).
migrations config options lets you change both migrations log table name and schema.
| type | { table: string, schema: string } |
| default | { table: "__drizzle_migrations", schema: "drizzle" } |
| commands | migrate |
export default defineConfig({
dialect: "postgresql",
schema: "./src/schema.ts",
migrations: {
table: 'my-migrations-table', // `__drizzle_migrations` by default
schema: 'public', // used in PostgreSQL only, `drizzle` by default
},
});
introspectConfiguration for drizzle-kit pull command.
casing is responsible for in-code column keys casing
| type | { casing: "preserve" | "camel" } |
| default | { casing: "camel" } |
| commands | pull |
<CodeTabs items={["camel", "preserve"]}>
<Section> ```ts import * as p from "drizzle-orm/pg-core"export const users = p.pgTable("users", { id: p.serial(), firstName: p.text("first-name"), lastName: p.text("LastName"), email: p.text(), phoneNumber: p.text("phone_number"), });
```sql
SELECT a.attname AS column_name, format_type(a.atttypid, a.atttypmod) as data_type FROM pg_catalog.pg_attribute a;
column_name | data_type
---------------+------------------------
id | serial
first-name | text
LastName | text
email | text
phone_number | text
export const users = p.pgTable("users", { id: p.serial(), "first-name": p.text("first-name"), LastName: p.text("LastName"), email: p.text(), phone_number: p.text("phone_number"), });
```sql
SELECT a.attname AS column_name, format_type(a.atttypid, a.atttypmod) as data_type FROM pg_catalog.pg_attribute a;
column_name | data_type
---------------+------------------------
id | serial
first-name | text
LastName | text
email | text
phone_number | text
tablesFiltertablesFilter option lets you specify glob
based table names filter, e.g. ["users", "user_info"] or "user*"
| type | string string[] |
| default | -- |
| commands | generate push pull |
export default defineConfig({ dialect: "postgresql", tablesFilter: ["users", "posts", "project1_*"], });
### `schemaFilter`
Was changed starting from `1.0.0-beta.1` version!
<Callout type='warning' collapsed="How it works in 0.x versions">
`drizzle-kit push` and `drizzle-kit pull` will by default manage all tables in `public` schema.
You can configure list of tables, schemas and extensions via `tablesFilters`, `schemaFilter` and `extensionFilters` options.
`schemaFilter` option lets you specify list of schemas for Drizzle Kit to manage
| | |
| :------------ | :----------------- |
| type | `string[]` |
| default | `["public"]` |
| commands | `push` `pull` |
</Callout>
<Callout>
If you want to run multiple projects with one database - check out [our guide](/docs/goodies#multi-project-schema).
</Callout>
`drizzle-kit push` and `drizzle-kit pull` will by default manage all schemas.
`schemaFilter` option lets you specify [`glob`](https://www.digitalocean.com/community/tools/glob?comments=true&glob=/**/*.js&matches=false&tests=//%20This%20will%20match%20as%20it%20ends%20with%20'.js'&tests=/hello/world.js&tests=//%20This%20won't%20match!&tests=/test/some/globs)
based schema names filter, e.g. `["public", "auth"]` or `"tenant_*"`
| | |
| :------------ | :----------------- |
| type | `string[]` |
| commands | `push` `pull` |
<rem025/>
```ts
export default defineConfig({
dialect: "postgresql",
schemaFilter: ["public", "schema1", "schema2"],
});
extensionsFiltersSome extensions like postgis, when installed on the database, create its own tables in public schema.
Those tables have to be ignored by drizzle-kit push or drizzle-kit pull.
extensionsFilters option lets you declare list of installed extensions for drizzle kit to ignore their tables in the schema.
| type | ["postgis"] |
| default | [] |
| commands | push pull |
export default defineConfig({
dialect: "postgresql",
extensionsFilters: ["postgis"],
});
entitiesThis configuration is created to set up management settings for specific entities in the database.
For now, it only includes roles, but eventually all database entities will migrate here, such as tables, schemas, extensions, functions, triggers, etc
rolesIf you are using Drizzle Kit to manage your schema and especially the defined roles, there may be situations where you have some roles that are not defined in the Drizzle schema.
In such cases, you may want Drizzle Kit to skip those roles without the need to write each role in your Drizzle schema and mark it with .existing().
The roles option lets you:
Neon and Supabase, which do not manage their specific roles.| type | boolean | { provider: "neon" | "supabase", include: string[], exclude: string[]} |
| default | false |
| commands | push pull generate |
By default, drizzle-kit won't manage roles for you, so you will need to enable that. in drizzle.config.ts
export default defineConfig({
dialect: "postgresql",
entities: {
roles: true
}
});
You have a role admin and want to exclude it from the list of manageable roles
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
...
entities: {
roles: {
exclude: ['admin']
}
}
});
You have a role admin and want to include to the list of manageable roles
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
...
entities: {
roles: {
include: ['admin']
}
}
});
If you are using Neon and want to exclude roles defined by Neon, you can use the provider option
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
...
entities: {
roles: {
provider: 'neon'
}
}
});
If you are using Supabase and want to exclude roles defined by Supabase, you can use the provider option
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
...
entities: {
roles: {
provider: 'supabase'
}
}
});
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
...
entities: {
roles: {
provider: 'supabase',
exclude: ['new_supabase_role']
}
}
});
strictPrompts confirmation to run printed SQL statements when running drizzle-kit push command.
| type | boolean |
| default | false |
| commands | push |
export default defineConfig({
dialect: "postgresql",
strict: false,
});
verbosePrint all SQL statements during drizzle-kit push command.
| type | boolean |
| default | true |
| commands | generate pull |
export default defineConfig({
dialect: "postgresql",
verbose: false,
});
breakpointsDrizzle Kit will automatically embed --> statement-breakpoint into generated SQL migration files,
that's necessary for databases that do not support multiple DDL alternation statements in one transaction(MySQL and SQLite).
breakpoints option flag lets you switch it on and off
| type | boolean |
| default | true |
| commands | generate pull |
export default defineConfig({
dialect: "postgresql",
breakpoints: false,
});