Configuration files
Drizzle Kit lets you declare configurations in TypeScript
, JavaScript
or JSON
configuration files.
You can have autocomplete experience and very convenient enviroment variables flow!
π¦ <project root>
β ...
β π drizzle
β π src
β π drizzle.config.ts
β π package.json
import type { Config } from "drizzle-kit";
export default {
schema: "./src/schema.ts",
out: "./drizzle",
} satisfies Config;
Schema files paths
schema
param lets you define where your schema file/files live.
You can have as many separate schema files as you want and define paths to them using
glob
(opens in a new tab)
or array of globs syntax.
π¦ <project root>
β ...
β π drizzle
β π src
β β ...
β β π index.ts
β β π schema.ts
β π drizzle.config.ts
β π package.json
import type { Config } from "drizzle-kit";
export default {
schema: "./src/schema.ts",
} satisfies Config;
Migrations folder
out
param lets you define folder for your migrations, it's optional and drizzle
by default.
It's very useful since you can have many separate schemas for different databases in the same project
and have different migration folders for them.
Migration folder contains .sql
migration files and _meta
folder which is used by drizzle-kit
Don't delete any files manually, please refer to drizzle-kit drop
command
π¦ <project root>
β ...
β π drizzle
β β π _meta
β β π user.ts
β β π post.ts
β β π comment.ts
β π src
β π drizzle.config.ts
β π package.json
import type { Config } from "drizzle-kit";
export default {
schema: "./src/schema/*",
out: "./drizzle",
} satisfies Config;
SQL breakpoints
breakpoints
param lets you enable/disable SQL statement breakpoints in generated migrations. It's optional and true
by default,
it's necessary to properly apply migrations on databases, that do not support multiple DDL alternation statements in one transaction(MySQL, SQLite)
and Drizzle ORM has to apply them sequentially one by one.
CREATE TABLE `book` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text
);
--> statement-breakpoint
CREATE TABLE `book_to_author` (
`author_id` integer,
`book_id` integer,
PRIMARY KEY(`book_id`, `author_id`)
);
Push and Pull
Drizzle Kit provides you introspect and push APIs.
We mirror connection params of database drivers
import type { Config } from "drizzle-kit";
import * as dotenv from "dotenv";
dotenv.config();
export default {
schema: "./src/schema/*",
out: "./drizzle",
driver: 'pg',
dbCredentials: {
connectionString: process.env.DB_URL,
}
} satisfies Config;
When using the PlanetScale driver, your connection string must end with ?ssl={"rejectUnauthorized":true}
instead of ?sslaccept=strict
.
Multi-project schema
tablesFilter
param lets you filter tables with glob
(opens in a new tab)
syntax for db push
command.
It's useful when you have only one database avaialable for several separate projects with separate sql schemas.
How to define multi-project tables with Drizzle ORM - see here
import { serial, text, pgTableCreator } from 'drizzle-orm/pg-core';
const pgTable = pgTableCreator((name) => `project1_${name}`);
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
});
import type { Config } from "drizzle-kit";
import * as dotenv from "dotenv";
dotenv.config();
export default {
schema: "./src/schema/*",
out: "./drizzle",
driver: "mysql2",
dbCredentials: {
connectionString: process.env.DATABASE_URL,
}
tablesFilter: ["project1_*"],
} satisfies Config;
You can apply multiple or
filters
tablesFilter: ["project1_*", "project2_*"]