虚拟字段
虚拟字段在查询结果上同步计算,不写入数据库。字段值和返回类型都可以自动推导。虚拟字段通过 $extend 挂到客户端上,连接与运行时选项仍由 createClient 配置。
定义
ts
import { createClient } from "./generated/client.ts";
const base = createClient({ connection: "mysql://root:test_password@127.0.0.1:3306/app" });
const db = base.$extend({
virtuals: {
user: {
displayName: {
needs: { name: true, email: true },
compute: (row) => row.name ?? row.email,
},
},
},
});1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
| 属性 | 说明 |
|---|---|
needs | 计算所依赖的物理标量字段 |
compute | 同步的纯函数;类型上是完整行,运行时只暴露 needs 声明的字段 |
跨模型共享
多个模型共享虚拟字段时,将字段定义提取为常量并在模型配置中复用;模型专属字段单独声明:
ts
const shared = {
emailDomain: {
needs: { email: true },
compute: (row: Readonly<{ email: string }>) => row.email.split("@")[1] ?? "",
},
} as const;
const db = createClient({ connection: "mysql://root:test_password@127.0.0.1:3306/app" }).$extend({
virtuals: {
user: {
...shared,
displayName: {
needs: { name: true, email: true },
compute: (row) => row.name ?? row.email,
},
},
account: shared,
},
});1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 共享常量是「字段名 → 定义」的映射,必须加
as const,否则needs里的true会被拓宽成boolean而报错。因为它是具名常量,compute的参数需要手动注解为公共字段子集,且这些字段必须存在于每个使用它的模型;内联的专属字段不需要注解,其compute参数自动是所在模型的完整行。 needs声明的字段在扩展客户端时逐模型校验,缺失会报错。
叠加、生命周期共享与链式扩展见扩展。
读取
默认的完整记录包含虚拟字段,也可以用 select 选择,或用 omit 排除:
ts
const rows = await db.user.findMany({ select: { displayName: true } });
const others = await db.user.findMany({ omit: { displayName: true } });1
2
2
依赖字段自动加载,结果按 select 返回。
规则
compute必须同步且只能读取已声明的依赖;返回 Promise 或读取未声明的依赖都会报错。- 依赖只能是物理标量字段;依赖其他虚拟字段、循环依赖和名称冲突都会在扩展客户端时被拒绝。
- 虚拟字段只出现在查询结果中,不写入数据库。