配置 Drizzle kit

配置

默认情况下,Drizzle 会读取位于项目根目录的 drizzle.config.ts 文件。 如果您需要指定不同的配置文件,可以在任何 drizzle-kit 命令后面使用 --config=<path> 命令行选项。

Drizzle Kit 支持多种配置文件格式,包括 .ts.js。 这种灵活性允许您选择最适合您需求和偏好的格式。

以下是每种文件格式用法的例子:

Typescript 示例

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dialect: "postgresql",
  schema: "./schema.ts",
  out: "./drizzle"
})

JavaScript 示例

/** @type { import("drizzle-kit").Config } */
export default {
  dialect: "postgresql",
  schema: "./schema.ts",
  out: "./drizzle"
};

选项

dialect

  • 类型: 'postgresql' | 'mysql' | 'sqlite'
  • 命令: 所有命令
⚠️

当前参数适用于 drizzle-kit@0.21.0 及更高版本。在之前的版本中,不应使用此参数。

dialect 参数负责明确指定您正在使用的数据库方言 对于所有命令

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dialect: "postgresql",
})

schema

  • 类型: string | string[]
  • 命令: generate, check, up, push, drop

schema 参数允许您定义模式文件的位置。

您可以拥有任意数量的单独模式文件,并使用 glob 或者 globs 语法的数组来定义它们的路径。

用法

单个文件
多个文件
Glob 模式
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  schema: "./schema.ts",
})
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  schema: ["./user.sql.ts", "./post.sql.ts"],
})
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  schema: "./schema/*.ts",
})

out

  • 类型: string | string[]
  • 缺省值: drizzle
  • 命令: generate, check, up, drop, introspect, migrate

out 参数允许您定义迁移文件的文件夹。

在此文件夹中,drizzle-kit 将:

  1. 存储迁移文件、metajournal
  2. 在运行 drizzle-kit introspect 时输出 schema.ts 文件

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  out: "./drizzle",
})

driver

  • 类型: 'aws-data-api' | 'd1-http' | 'expo' | 'turso'
  • 命令: push, introspect, migrate

driver 参数负责明确提供要在访问数据库时使用的驱动程序, 用于录像和推送命令。这对于使用基于 HTTP 的数据库并且使用数据库连接 URL 连接不可行的情况非常有用。例如,TursoCloudflare D1

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  driver: "aws-data-api"
})

dbCredentials

  • 类型: PostgresCredentials, MySQLCredentials, SQLiteCredentials, TursoCredentials
  • 命令: push, introspect
💡

每个 dialect 都应该有 dbCredentials 字段,以提供与数据库连接的手段。

PostgresCredentials (postgresql)

选项 1
选项 2
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dbCredentials: {
    url: '',
  }
})
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dbCredentials: {
    host: "",
    port: "",
    user: "",
    password: "",
    database: "",
    ssl: true, // can be boolean | "require" | "allow" | "prefer" | "verify-full" | options from node:tls
  }
})

MySQLCredentials (mysql)

选项 1
选项 2
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dbCredentials: {
    url: '',
  }
})
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dbCredentials: {
    host: "",
    port: "",
    user: "",
    password: "",
    database: "",
    ssl: "" // can be: string | SslOptions (ssl options from mysql2 package)
  }
})

SQLiteCredentials (libsql, better-sqlite3)

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dbCredentials: {
    url: '', // 👈 this could also be a path to the local sqlite file using 'file:<<path>>'
  }
})

TursoCredentials (turso)

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dialect: "sqlite",
  driver: "turso",
  dbCredentials: {
    url: '',
    authToken: ''
  }
})

D1Credentials (d1)

To get all needed credentials from Cloudflare dashboard you need to

To get accountId:

  • Open “Workers & Pages” tab
  • Go to “Overview”
  • Copy Account ID from the right sidebar

To get databaseId:

  • Open D1 database you want to connect to and copy Database ID

To get token:

  • Open “My profile”
  • Go to “API Tokens”
  • Create token with D1 edit permissions
import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  dialect: "sqlite",
  driver: "d1-http",
  dbCredentials: {
    accountId: "",
    databaseId: "",
    token: "",
  }
})

migrations

  • 类型: object
  • 命令: migrate

migrations 参数允许您指定自定义迁移的表和模式(仅适用于 PostgreSQL)。 默认情况下,关于已执行迁移的所有信息都将存储在数据库中的 __drizzle_migrations 表中,并且对于 PostgreSQL,还将存储在 drizzle 模式中。 但是,您可以配置记录存储在何处。

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  migrations: {
    table: "custom_migrations_table",
    schema: "public"
  }
});

migrations.prefix

  • Type: index | supabase | timestamp | unix | none
  • Commands: generate
  1. index is the default type and will result in 0001_name.sql file names;
  2. supabase and timestamp are equal and will result in 20240627123900_name.sql file names;
  3. unix will result in unix seconds prefixes 1719481298_name.sql file names;
  4. none will omit the prefix completely;

You can specify a custom shape for migration file names, using different prefixes

💡

You can use prefixes only if you don’t have any migration history already. The kit won’t work properly if you mix prefix formats within the same migration folder. Please ensure you use only one prefix format throughout the project.

We are working on making it possible to migrate from one prefix format to another and to ensure interoperability

import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  migrations: {
    prefix: 'supabase'
  }
});

breakpoints

  • 类型: boolean
  • 缺省值: true
  • 命令: generate, introspect

breakpoints 参数允许您在生成的迁移中启用/禁用 SQL 语句断点。它是可选的,默认为 true, 它对于在不支持一个事务中的多个 DDL 交替语句(MySQL、SQLite)的数据库上正确应用迁移是必要的, 因此 Drizzle ORM 必须逐个顺序应用它们。

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`)
);

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  breakpoints: true
});

tablesFilters

  • 类型: string | string[]
  • 命令: push

tablesFilter 参数允许您使用用于 db push 命令的 glob(opens in a new tab) 语法过滤表。 当您只有一个数据库可用于多个独立的具有单独的 SQL 模式的项目时,这非常有用。

如何定义使用 Drizzle ORM 的多项目表 — 在此查看。

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  tablesFilter: ["project1_*"],
});

在这种情况下,仅以 project1_ 开头的表将与数据库同步

extensionsFilters

  • Type: string[]
  • Commands: push, introspect
💡

Available from drizzle-kit@0.22.0

The PostGIS extension creates a few internal tables in the public schema. This means that if you have a database with the PostGIS extension and use push or introspect, all those tables will be included in diff operations. In this case, you would need to specify tablesFilter, find all tables created by the extension, and list them in this parameter.

We have addressed this issue so that you won’t need to take all these steps. Simply specify extensionsFilters with the name of the extension used, and Drizzle will skip all the necessary tables.

Currently, we only support the postgis option, but we plan to add more extensions if they create tables in the public schema.

The postgis option will skip the geography_columns, geometry_columns, and spatial_ref_sys tables

Usage

import { defineConfig } from 'drizzle-kit'

export default defaultConfig({
  dialect: "postgresql",
  extensionsFilters: ["postgis"],
})

schemaFilter

  • 类型: string | string[]
  • 缺省值: ["public"]
  • 命令: push, introspect
  • 可用数据库: PostgreSQL

schemaFilter 参数允许您定义在 introspectpush 命令中应使用的 PostgreSQL 架构。 此参数接受单个模式作为字符串或作为字符串数组。此处不支持 glob 模式。 默认情况下,drizzle 将在这两个命令中使用 public 架构,但您可以添加任何需要的模式。

例如,设置 schemaFilter: ["my_schema"] 将只查找数据库和 drizzle 模式中属于 my_schema 模式的表。

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  schemaFilter: ["public", "my_schema"],
});

verbose

  • 类型: boolean
  • 缺省值: false
  • 命令: push

此命令用于 drizzle-kit push 命令,打印将要执行的所有语句。

💡

注意:此命令只会打印应该执行的语句。 要在应用之前批准它们,请参考 strict 命令。

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  verbose: true
});

strict

  • 类型: boolean
  • 缺省值: false
  • 命令: push

此命令用于 drizzle-kit push 命令,将始终要求您确认是否 要执行同步架构与数据库所需的所有语句。

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  strict: true
});

introspect

  • 类型: object

此部分是 drizzle.config.* 文件中 introspect 对象的参考。

casing

  • 类型: 'preserve' | 'camel'
  • 缺省值: camel
  • 命令:

此命令负责选择 introspect 数据库时的列、表、约束 JS 键名称的策略。

用法

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
  introspect: {
    casing: 'preserve'
  }
});

示例

preserve
camel
export const user_preferences = sqliteTable(
  "user_preferences",
  {
    id: integer("id").primaryKey(),
    name: integer("name").notNull(),
    user_id: integer("user_id").notNull(),
  }
);
export const userPreferences = sqliteTable(
  "user_preferences",
  {
    id: integer("id").primaryKey(),
    name: integer("name").notNull(),
    userId: integer("user_id").notNull(),
  }
);
Become a Gold Sponsor