drizzle-kit export
This guide assumes familiarity with:
drizzle-kit exportλ Drizzle μ€ν€λ§μ SQL ννμ λ΄λ³΄λ΄κ³ μ½μμ SQL DDL ννμ μΆλ ₯ν μ μκ² ν΄μ€λλ€.
λ΄λΆμ μΌλ‘ μ΄λ»κ² μλνλμ?
Drizzle Kit export λͺ
λ Ήμ΄λ μΌλ ¨μ μ΄λ²€νΈλ₯Ό νΈλ¦¬κ±°ν©λλ€:
Drizzle μ€ν€λ§ νμΌμ μ½κ³ μ€ν€λ§μ json μ€λ
μ·μ μμ±ν©λλ€
json μ°¨μ΄μ μ κΈ°λ°μΌλ‘ SQL DDL λ¬Έμ μμ±ν©λλ€
SQL DDL λ¬Έμ μ½μμ μΆλ ₯ν©λλ€
Drizzle λ§μ΄κ·Έλ μ΄μ
κ΄λ¦¬μ μ½λλ² μ΄μ€ μ°μ μ κ·Ό λ°©μμ λ€λ£¨λλ‘ μ€κ³λμμ΅λλ€.
Drizzle μ€ν€λ§μ SQL ννμ λ΄λ³΄λΌ μ μμΌλ©°, Atlasμ κ°μ μΈλΆ λκ΅¬κ° λͺ¨λ λ§μ΄κ·Έλ μ΄μ
μ μ²λ¦¬νλλ‘ ν μ μμ΅λλ€
drizzle-kit export λͺ
λ Ήμ΄λ dialect λ° schema κ²½λ‘ μ΅μ
μ λͺ¨λ μ 곡ν΄μΌ νλ©°,
drizzle.config.ts κ΅¬μ± νμΌ λλ CLI μ΅μ
μ ν΅ν΄ μ€μ ν μ μμ΅λλ€
κ΅¬μ± νμΌ μ¬μ©
CLI μ΅μ
μ¬μ©
// 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" ,
}); π¦ <project root>
β ...
β π drizzle
β π src
β β π user
β β β π handler.ts
β β β π schema.ts
β β π posts
β β β π handler.ts
β β β π schema.ts
β β π index.ts
β π drizzle.config.ts
β π package.json import { defineConfig } from "drizzle-kit" ;
export default defineConfig ({
schema : "./src/**/schema.ts" ,
//or
schema : [ "./src/user/schema.ts" , "./src/posts/schema.ts" ]
}); π¦ <project root>
β ...
β π drizzle
β π src
β β π schema
β β β π user.ts
β β β π post.ts
β β β π comment.ts
β β π index.ts
β π drizzle.config.ts
β π package.json import { defineConfig } from "drizzle-kit" ;
export default defineConfig ({
schema : "./src/schema/*" ,
}); π¦ <project root>
β ...
β π drizzle
β π src
β β π userById.ts
β β π userByEmail.ts
β β π listUsers.ts
β β π user.sql.ts
β β π postById.ts
β β π listPosts.ts
β β π post.sql.ts
β π index.ts
β π drizzle.config.ts
β π package.json import { defineConfig } from "drizzle-kit" ;
export default defineConfig ({
schema : "./src/**/*.sql.ts" , // Dax's favourite
});
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
--sqlgenerating 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.
dialectrequiredDatabase dialect, one of postgresql mysql sqlite turso singlestore schemarequiredPath to typescript schema file(s) or folder(s) with multiple schema files configConfiguration 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
);