src/content/docs/drizzle-kit-export.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 Prerequisites from "@mdx/Prerequisites.astro" import Npx from "@mdx/Npx.astro"; import SchemaFilePaths from "@mdx/SchemaFilePaths.mdx" import Dialects from "@mdx/Dialects.mdx"
drizzle-kit exportdrizzle-kit export lets you export SQL representation of Drizzle schema and print in console SQL DDL representation on it.
<Callout collapsed="How it works under the hood?">
Drizzle Kit export command triggers a sequence of events:
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
<CodeTabs items={["With config file", "As CLI options"]}>
export default defineConfig({ dialect: "postgresql", schema: "./src/schema.ts", });
```shell
npx drizzle-kit export
npx drizzle-kit export --dialect=postgresql --schema=./src/schema.ts
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.
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 drizzle-kit export --config=drizzle-prod.config.ts </Npx>
š¦ <project root>
ā š drizzle
ā š src
ā š .env
ā š drizzle-dev.config.ts
ā š drizzle-prod.config.ts
ā š package.json
ā š tsconfig.json
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
<rem025/> <Npx> drizzle-kit push --name=init drizzle-kit push --name=seed_users --custom </Npx> <hr/>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 <Dialects/> |
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 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
);