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
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
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:
npm
yarn
pnpm
bun
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
npm
yarn
pnpm
bun
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 postgresqlmysqlsqlitetursosinglestore
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:
Now letβs run
And it will successfully output SQL representation of drizzle schema