drizzle-kit export lets you export SQL representation of Drizzle schema and print in console SQL DDL representation on it.
How it works under the hood?
Drizzle Kit export command triggers a sequence of events:
- It will read through your Drizzle schema file(s) and compose a json snapshot of your schema
- Based on json differences it will generate SQL DDL statements
- Output SQL DDL statements to console
Itβs designed to cover codebase first approach of managing Drizzle migrations. You can export the SQL representation of the Drizzle schema, allowing external tools like Atlas to handle all the migrations for you
drizzle-kit export command requires you to provide both dialect and schema path options, you can set them either via drizzle.config.ts config file or via CLI options
With config file
As CLI options
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./src/schema.ts",
});
npx drizzle-kit export
npx drizzle-kit export --dialect=postgresql --schema=./src/schema.ts
Schema files path
You can have a single schema.ts file or as many schema files as you want spread out across the project. Drizzle Kit requires you to specify path(s) to them as a glob via schema configuration option.
Example 1
Example 2
Example 3
Example 4
π¦ <project root>
β ...
β π drizzle
β π src
β β ...
β β π index.ts
β β π schema.ts
β π drizzle.config.ts
β π package.json
import { defineConfig } from "drizzle-kit";
export default defineConfig({
schema: "./src/schema.ts",
});
Multiple configuration files in one project
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 export --config=drizzle-dev.config.ts
npx drizzle-kit export --config=drizzle-prod.config.ts
yarn drizzle-kit export --config=drizzle-dev.config.ts
yarn drizzle-kit export --config=drizzle-prod.config.ts
pnpm drizzle-kit export --config=drizzle-dev.config.ts
pnpm drizzle-kit export --config=drizzle-prod.config.ts
bunx drizzle-kit export --config=drizzle-dev.config.ts
bunx drizzle-kit export --config=drizzle-prod.config.ts
π¦ <project root>
β π drizzle
β π src
β π .env
β π drizzle-dev.config.ts
β π drizzle-prod.config.ts
β π package.json
β π tsconfig.json
Extended list of available configurations
drizzle-kit export has a list of cli-only options
--sql | generating SQL representation of Drizzle Schema |
By default, Drizzle Kit outputs SQL files, but in the future, we want to support different formats
npx drizzle-kit push --name=init
npx drizzle-kit push --name=seed_users --custom
yarn drizzle-kit push --name=init
yarn drizzle-kit push --name=seed_users --custom
pnpm drizzle-kit push --name=init
pnpm drizzle-kit push --name=seed_users --custom
bunx drizzle-kit push --name=init
bunx drizzle-kit push --name=seed_users --custom
We recommend configuring drizzle-kit through drizzle.config.ts file, yet you can provide all configuration options through CLI if necessary, e.g. in CI/CD pipelines, etc.
dialect | required | Database dialect, one of postgresql mysql sqlite turso singlestore mssql cockroachdb |
schema | required | Path to typescript schema file(s) or folder(s) with multiple schema files |
config | Configuration file path, default is drizzle.config.ts |
Example
Example of how to export drizzle schema to console with Drizzle schema located in ./src/schema.ts
We will also place drizzle config file in the configs folder.
Letβs create config file:
π¦ <project root>
β π configs
β β π drizzle.config.ts
β π src
β β π schema.ts
β β¦
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./src/schema.ts",
});
import { pgTable, serial, text } from 'drizzle-orm/pg-core'
export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: text('email').notNull(),
name: text('name')
});
Now letβs run
npx drizzle-kit export --config=./configs/drizzle.config.ts
And it will successfully output SQL representation of drizzle schema
CREATE TABLE "users" (
"id" serial PRIMARY KEY NOT NULL,
"email" text NOT NULL,
"name" text
);