跳到主要内容

计算字段

计算字段允许您基于现有数据派生新字段。一个常见的例子是计算全名。在您的数据库中,可能只存储了名和姓,但您可以定义一个函数来组合名和姓以计算全名。计算字段是只读的,存储在应用程序的内存中,而不是数据库中。

使用 Prisma Client 扩展

以下示例演示了如何创建一个 Prisma Client 扩展,它在运行时将 fullName 计算字段添加到 Prisma schema 中的 User 模型。

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient().$extends({
result: {
user: {
fullName: {
needs: { firstName: true, lastName: true },
compute(user) {
return `${user.firstName} ${user.lastName}`
},
},
},
},
})

async function main() {
/**
* Example query containing the `fullName` computed field in the response
*/
const user = await prisma.user.findFirst()
}

main()
显示CLI结果

计算字段是类型安全的,可以返回从连接值到复杂对象或可作为模型实例方法的函数。

Prisma ORM 4.16.0 之前的说明
警告

随着 Prisma ORM 4.16.0 版本中 Prisma Client 扩展普遍可用(GA),不再推荐以下步骤。请使用客户端扩展来实现此功能。

Prisma Client 尚未原生支持计算字段,但是,您可以定义一个接受泛型类型作为输入的函数,然后扩展该泛型以确保其符合特定结构。最后,您可以返回带有额外计算字段的该泛型。让我们看看它可能是什么样子

// Define a type that needs a first and last name
type FirstLastName = {
firstName: string
lastName: string
}

// Extend the T generic with the fullName attribute
type WithFullName<T> = T & {
fullName: string
}

// Take objects that satisfy FirstLastName and computes a full name
function computeFullName<User extends FirstLastName>(
user: User
): WithFullName<User> {
return {
...user,
fullName: user.firstName + ' ' + user.lastName,
}
}

async function main() {
const user = await prisma.user.findUnique({ where: 1 })
const userWithFullName = computeFullName(user)
}

在上面的 TypeScript 示例中,定义了一个扩展 FirstLastName 类型的 User 泛型。这意味着您传递给 computeFullName 的任何内容都必须包含 firstNamelastName 键。

还定义了一个 WithFullName<User> 返回类型,它接受任何 User 类型,并附加一个 fullName 字符串属性。

有了这个函数,任何包含 firstNamelastName 键的对象都可以计算出一个 fullName。相当巧妙,对吧?

进一步了解

  • 了解如何使用 Prisma Client 扩展将计算字段添加到您的 schema — 示例
  • 了解如何将 computeFullName 函数移至 自定义模型中。
  • 有一个开放的功能请求,希望为 Prisma Client 添加原生支持。如果您希望看到它实现,请务必为该 issue 点赞并分享您的用例!