DrizzleORM v0.29.1 版本发布
2023 年 11 月 29 日

修复

新功能/辅助工具

所有方言中所有查询构建器的详细 JSDoc

现在,您在 IDE 中开发和使用 JSDoc 时,可以访问更多信息、提示、文档链接等。以前,我们只为筛选表达式提供这些功能,但现在您可以在 Drizzle 查询构建器的所有部分中看到它们。

SQL 中聚合函数的新辅助工具

请记住,聚合函数通常与 SELECT 语句的 GROUP BY 子句一起使用。因此,如果您在一次查询中同时使用聚合函数和其他列进行选择,请务必使用 `.groupBy` 子句。

以下是函数列表及其使用 sql 模板的等效项

count

await db.select({ value: count() }).from(users);
await db.select({ value: count(users.id) }).from(users);

// It's equivalent to writing
await db.select({ 
  value: sql`count('*'))`.mapWith(Number) 
}).from(users);
await db.select({ 
  value: sql`count(${users.id})`.mapWith(Number) 
}).from(users);

countDistinct

await db.select({ value: countDistinct(users.id) }).from(users);

// It's equivalent to writing
await db.select({ 
  value: sql`count(${users.id})`.mapWith(Number) 
}).from(users);

平均

await db.select({ value: avg(users.id) }).from(users);

// It's equivalent to writing
await db.select({ 
  value: sql`avg(${users.id})`.mapWith(String) 
}).from(users);

avgDistinct

await db.select({ value: avgDistinct(users.id) }).from(users);

// It's equivalent to writing
await db.select({ 
  value: sql`avg(distinct ${users.id})`.mapWith(String) 
}).from(users);

sum

await db.select({ value: sum(users.id) }).from(users);

// It's equivalent to writing
await db.select({ 
  value: sql`sum(${users.id})`.mapWith(String) 
}).from(users);

sumDistinct

await db.select({ value: sumDistinct(users.id) }).from(users);

// It's equivalent to writing
await db.select({ 
  value: sql`sum(distinct ${users.id})`.mapWith(String) 
}).from(users);

max

await db.select({ value: max(users.id) }).from(users);

// It's equivalent to writing
await db.select({ 
  value: sql`max(${expression})`.mapWith(users.id) 
}).from(users);

min

await db.select({ value: min(users.id) }).from(users);

// It's equivalent to writing
await db.select({ 
  value: sql`min(${users.id})`.mapWith(users.id) 
}).from(users);

要查找更多信息,请查阅文档:聚合辅助工具

新包

Drizzle ESLint 插件

对于无法进行特定场景的类型检查,或即使可以进行但错误消息难以理解的情况,我们决定创建一个包含推荐规则的 ESLint 包。此包旨在帮助开发人员在开发过程中处理关键场景。欲了解更多信息,请查阅文档

安装

npm
yarn
pnpm
bun
npm i eslint eslint-plugin-drizzle

您可以在 IDE 中安装这些包以获得 TypeScript 支持

npm
yarn
pnpm
bun
npm i @typescript-eslint/eslint-plugin @typescript-eslint/parser

使用方法

创建一个 .eslintrc.yml 文件,将 drizzle 添加到 plugins 中,并指定您想要使用的规则。您可以在下方找到所有现有规则的列表

root: true
parser: '@typescript-eslint/parser'
parserOptions:
  project: './tsconfig.json'
plugins:
  - drizzle
rules:
  'drizzle/enforce-delete-with-where': "error"
  'drizzle/enforce-update-with-where': "error"

所有配置

此插件导出一个包含所有规则(废弃规则除外)的完整配置。

root: true
extends:
  - "plugin:drizzle/all"
parser: '@typescript-eslint/parser'
parserOptions:
  project: './tsconfig.json'
plugins:
  - drizzle

目前,all 等同于 recommended

root: true
extends:
  - "plugin:drizzle/recommended"
parser: '@typescript-eslint/parser'
parserOptions:
  project: './tsconfig.json'
plugins:
  - drizzle

规则

enforce-delete-with-where:强制在 `.delete()` 语句中使用 `delete` 和 `the.where()` 子句。大多数情况下,您不需要删除表中的所有行,并且需要某种 `WHERE` 语句。

错误消息

Without `.where(...)` you will delete all the rows in a table. If you didn't want to do it, please use `db.delete(...).where(...)` instead. Otherwise you can ignore this rule here

您可以选择在插件选项中定义一个 drizzleObjectName,它接受 stringstring[] 类型的值。当您有不属于 Drizzle 的带有 delete 方法的对象或类时,这会很有用。这样的 delete 方法会触发 ESLint 规则。为了避免这种情况,您可以定义您在代码库中使用的 Drizzle 对象的名称(例如 db),这样只有当 delete 方法来自该对象时,规则才会触发。

示例,配置 1

"rules": {
  "drizzle/enforce-delete-with-where": ["error"]
}
class MyClass {
  public delete() {
    return {}
  }
}

const myClassObj = new MyClass();

// ---> Will be triggered by ESLint Rule
myClassObj.delete()

const db = drizzle(...)
// ---> Will be triggered by ESLint Rule
db.delete()

示例,配置 2

"rules": {
  "drizzle/enforce-delete-with-where": ["error", { "drizzleObjectName": ["db"] }],
}
class MyClass {
  public delete() {
    return {}
  }
}

const myClassObj = new MyClass();

// ---> Will NOT be triggered by ESLint Rule
myClassObj.delete()

const db = drizzle(...)
// ---> Will be triggered by ESLint Rule
db.delete()

enforce-update-with-where:强制在 `.update()` 语句中使用 `update` 和 `the.where()` 子句。大多数情况下,您不需要更新表中的所有行,并且需要某种 `WHERE` 语句。

错误消息

Without `.where(...)` you will update all the rows in a table. If you didn't want to do it, please use `db.update(...).set(...).where(...)` instead. Otherwise you can ignore this rule here

您可以选择在插件选项中定义一个 drizzleObjectName,它接受 stringstring[] 类型的值。当您有不属于 Drizzle 的带有 delete 方法的对象或类时,这会很有用。这样的 update 方法会触发 ESLint 规则。为了避免这种情况,您可以定义您在代码库中使用的 Drizzle 对象的名称(例如 db),这样只有当 delete 方法来自该对象时,规则才会触发。

示例,配置 1

"rules": {
  "drizzle/enforce-update-with-where": ["error"]
}
class MyClass {
  public update() {
    return {}
  }
}

const myClassObj = new MyClass();

// ---> Will be triggered by ESLint Rule
myClassObj.update()

const db = drizzle(...)
// ---> Will be triggered by ESLint Rule
db.update()

示例,配置 2

"rules": {
  "drizzle/enforce-update-with-where": ["error", { "drizzleObjectName": ["db"] }],
}
class MyClass {
  public update() {
    return {}
  }
}

const myClassObj = new MyClass();

// ---> Will NOT be triggered by ESLint Rule
myClassObj.update()

const db = drizzle(...)
// ---> Will be triggered by ESLint Rule
db.update()