drizzle-kit export
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 the current json snapshot 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
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./src/schema.ts",
});npx drizzle-kit exportSchema 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.
📦 <project root>
├ ...
├ 📂 drizzle
├ 📂 src
│ ├ ...
│ ├ 📜 index.ts
│ └ 📜 schema.ts
├ 📜 drizzle.config.ts
└ 📜 package.jsonimport { 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
📦 <project root>
├ 📂 drizzle
├ 📂 src
├ 📜 .env
├ 📜 drizzle-dev.config.ts
├ 📜 drizzle-prod.config.ts
├ 📜 package.json
└ 📜 tsconfig.jsonExtended list of available configurations
drizzle-kit export has a list of cli-only options
By default, Drizzle Kit outputs SQL files, but in the future, we want to support different formats
npx drizzle-kit export --sql=true
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.
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.tsAnd it will successfully output SQL representation of drizzle schema
CREATE TABLE "users" (
"id" serial PRIMARY KEY,
"email" text NOT NULL,
"name" text
);