修复
新功能/助手
所有方言中所有查询构建器的详细 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);
avg
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 包。此软件包旨在帮助开发者处理开发过程中的关键场景。更多信息,你可以查看 docs。
安装
npm i eslint eslint-plugin-drizzle
你可以在 IDE 中安装这些软件包以支持 TypeScript
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"
所有配置
此插件导出一个使用所有规则(已弃用规则除外)的 all 配置。
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
你也可以选择在插件选项中定义一个接受 string
或 string[]
的 drizzleObjectName
。当你的对象或类具有非 Drizzle 的删除方法时,这很有用。此类 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
你也可以选择在插件选项中定义一个接受 string
或 string[]
的 drizzleObjectName
。当你的对象或类具有非 Drizzle 的删除方法时,这很有用。例如 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()