DrizzleORM v0.32.0 发布
2024 年 7 月 10 日

`[email protected]` 和 `[email protected]` 的发布说明

并非强制升级这两个包,但如果您想在查询和迁移中使用新功能,则需要同时升级这两个包。

新功能

🎉 MySQL `$returningId()` 函数

MySQL 本身不支持 `INSERT` 后使用 `RETURNING`。对于带有 `autoincrement`(或 `serial`)类型的 `primary keys`,只有一种方法可以实现此功能,即访问 `insertId` 和 `affectedRows` 字段。我们为您准备了一种自动方式,使用 Drizzle 处理此类情况,并自动将所有插入的 ID 作为单独的对象接收。

import { boolean, int, text, mysqlTable } from 'drizzle-orm/mysql-core';

const usersTable = mysqlTable('users', {
  id: int('id').primaryKey(),
  name: text('name').notNull(),
  verified: boolean('verified').notNull().default(false),
});


const result = await db.insert(usersTable).values([{ name: 'John' }, { name: 'John1' }]).$returningId();
//    ^? { id: number }[]

此外,使用 Drizzle,您可以使用 `$default` 函数指定一个 `primary key`,该函数将在运行时生成自定义主键。我们还将在 `$returningId()` 调用中为您返回这些生成的键。

import { varchar, text, mysqlTable } from 'drizzle-orm/mysql-core';
import { createId } from '@paralleldrive/cuid2';

const usersTableDefFn = mysqlTable('users_default_fn', {
  customId: varchar('id', { length: 256 }).primaryKey().$defaultFn(createId),
  name: text('name').notNull(),
});


const result = await db.insert(usersTableDefFn).values([{ name: 'John' }, { name: 'John1' }]).$returningId();
//  ^? { customId: string }[]

如果没有主键 -> 此类查询的类型将是 `{}`。

🎉 PostgreSQL 序列

您现在可以在 Postgres 中在您需要的任何 schema 中指定序列,并定义所有可用属性。

示例
import { pgSchema, pgSequence } from "drizzle-orm/pg-core";

// No params specified
export const customSequence = pgSequence("name");

// Sequence with params
export const customSequence = pgSequence("name", {
      startWith: 100,
      maxValue: 10000,
      minValue: 100,
      cycle: true,
      cache: 10,
      increment: 2
});

// Sequence in custom schema
export const customSchema = pgSchema('custom_schema');

export const customSequence = customSchema.sequence("name");

🎉 PostgreSQL 标识列

来源:如前所述,Postgres 中的 `serial` 类型已过时,应该被弃用。理想情况下,您不应使用它。`标识列` 是在您的 schema 中指定序列的推荐方式,这就是我们引入 `标识列` 功能的原因。

示例
import { pgTable, integer, text } from 'drizzle-orm/pg-core' 

export const ingredients = pgTable("ingredients", {
  id: integer("id").primaryKey().generatedAlwaysAsIdentity({ startWith: 1000 }),
  name: text("name").notNull(),
  description: text("description"),
});

您可以在 `.generatedAlwaysAsIdentity()` 函数中指定序列的所有可用属性。此外,您还可以为这些序列指定自定义名称。

PostgreSQL 文档 参考

🎉 PostgreSQL 生成列

您现在可以在 PostgreSQL 支持的任何列上指定生成列,以与生成列一起使用。

示例:使用 `tsvector` 生成列

注意:我们将在最新版本发布前添加 `tsVector` 列类型。

import { SQL, sql } from "drizzle-orm";
import { customType, index, integer, pgTable, text } from "drizzle-orm/pg-core";

const tsVector = customType<{ data: string }>({
  dataType() {
    return "tsvector";
  },
});

export const test = pgTable(
  "test",
  {
    id: integer("id").primaryKey().generatedAlwaysAsIdentity(),
    content: text("content"),
    contentSearch: tsVector("content_search", {
      dimensions: 3,
    }).generatedAlwaysAs(
      (): SQL => sql`to_tsvector('english', ${test.content})`
    ),
  },
  (t) => ({
    idx: index("idx_content_search").using("gin", t.contentSearch),
  })
);

如果您不需要引用表中的任何列,则只需使用 `sql` 模板或 `string`。

export const users = pgTable("users", {
  id: integer("id"),
  name: text("name"),
  generatedName: text("gen_name").generatedAlwaysAs(sql`hello world!`),
  generatedName1: text("gen_name1").generatedAlwaysAs("hello world!"),
}),

🎉 MySQL 生成列

您现在可以在 MySQL 支持的任何列上指定生成列,以与生成列一起使用。

您可以指定 `stored` 和 `virtual` 选项,更多信息请查阅 MySQL 文档

此外,MySQL 对此类列的使用有一些限制,此处对此进行了 描述

Drizzle Kit 对 `push` 命令也有限制。

  1. 您不能使用 `push` 命令更改生成约束表达式和类型。Drizzle-kit 将忽略此更改。要使其生效,您需要 `drop the column`,`push`,然后 `add a column with a new expression`。这是由于数据库端的复杂映射造成的,在数据库端 schema 表达式会被修改,并且在内省时我们会得到一个不同的字符串。我们无法确定您是否更改了此表达式,或者它是否已被数据库更改并格式化。只要这些是生成列,并且 `push` 主要用于在本地数据库上进行原型设计,那么 `drop` 和 `create` 生成列应该会很快。由于这些列是 `generated`,所有数据都将恢复。

  2. `generate` 不应有任何限制。

示例
export const users = mysqlTable("users", {
  id: int("id"),
  id2: int("id2"),
  name: text("name"),
  generatedName: text("gen_name").generatedAlwaysAs(
    (): SQL => sql`${schema2.users.name} || 'hello'`,
    { mode: "stored" }
  ),
  generatedName1: text("gen_name1").generatedAlwaysAs(
    (): SQL => sql`${schema2.users.name} || 'hello'`,
    { mode: "virtual" }
  ),
}),

如果您不需要引用表中的任何列,则只需在 `.generatedAlwaysAs()` 中使用 `sql` 模板或 `string`。

🎉 SQLite 生成列

您现在可以在 SQLite 支持的任何列上指定生成列,以与生成列一起使用。

您可以指定 `stored` 和 `virtual` 选项,更多信息请查阅 SQLite 文档

此外,SQLite 对此类列的使用有一些限制,此处对此进行了 描述

Drizzle Kit 对 `push` 和 `generate` 命令也有限制。

  1. 您无法更改现有表中带有存储类型的生成约束表达式。您需要删除此表并重新创建它。这是由于 SQLite 对此类操作的限制。我们将在未来的版本中处理此情况(它将涉及创建新表并进行数据迁移)。

  2. 由于上述相同原因,您无法向现有列添加 `stored` 生成表达式。但是,您可以向现有列添加 `virtual` 表达式。

  3. 由于上述相同原因,您无法更改现有列中的 `stored` 生成表达式。但是,您可以更改 `virtual` 表达式。

  4. 由于上述相同原因,您无法将生成约束类型从 `virtual` 更改为 `stored`。但是,您可以从 `stored` 更改为 `virtual`。

Drizzle Kit 新功能

🎉 迁移支持所有新的 ORM 功能

PostgreSQL 序列、标识列以及所有方言的生成列。

🎉 `drizzle-kit push` 的新标志 `--force`

您可以使用 push 命令自动接受所有数据丢失语句。它仅在 CLI 参数中可用。请确保如果您接受在数据库上运行数据丢失语句,则始终使用它。

🎉 新的 `migrations` 标志 `prefix`

您现在可以自定义迁移文件前缀,使格式适合您的迁移工具。

示例:Supabase 迁移格式
import { defineConfig } from "drizzle-kit";

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