Skip to main content

类型实用程序

Prisma Client 中存在多种类型实用程序,可以帮助创建高度类型安全的扩展。

¥Several type utilities exist within Prisma Client that can assist in the creation of highly type-safe extensions.

类型 实用程序

¥Type Utilities

Prisma 客户端类型实用程序 是你的应用和 Prisma 客户端扩展中可用的实用程序,并提供为你的扩展构建安全和可扩展类型的有用方法。

¥Prisma Client type utilities are utilities available within your application and Prisma Client extensions and provide useful ways of constructing safe and extendable types for your extension.

可用的类型实用程序有:

¥The type utilities available are:

  • Exact<Input, Shape>:在 Input 上强制执行严格的类型安全。Exact 确保泛型类型 Input 严格符合你在 Shape 中指定的类型。它 narrows Input 到最精确的类型。

    ¥Exact<Input, Shape>: Enforces strict type safety on Input. Exact makes sure that a generic type Input strictly complies with the type that you specify in Shape. It narrows Input down to the most precise types.

  • Args<Type, Operation>:检索任何给定模型和操作的输入参数。这对于想要执行以下操作的扩展作者特别有用:

    ¥Args<Type, Operation>: Retrieves the input arguments for any given model and operation. This is particularly useful for extension authors who want to do the following:

    • 重用现有类型来扩展或修改它们。

      ¥Re-use existing types to extend or modify them.

    • 受益于与现有操作相同的自动补齐体验。

      ¥Benefit from the same auto-completion experience as on existing operations.

  • Result<Type, Arguments, Operation>:获取输入参数并提供给定模型和操作的结果。你通常会将其与 Args 结合使用。与 Args 一样,Result 可帮助你重用现有类型来扩展或修改它们。

    ¥Result<Type, Arguments, Operation>: Takes the input arguments and provides the result for a given model and operation. You would usually use this in conjunction with Args. As with Args, Result helps you to re-use existing types to extend or modify them.

  • Payload<Type, Operation>:检索结果的整个结构,作为给定模型和操作的标量和关系对象。例如,你可以使用它来确定哪些键是类型级别的标量或对象。

    ¥Payload<Type, Operation>: Retrieves the entire structure of the result, as scalars and relations objects for a given model and operation. For example, you can use this to determine which keys are scalars or objects at a type level.

以下示例基于 findFirst 创建一个新操作 exists。它具有 findFirst. 的所有参数。

¥The following example creates a new operation, exists, based on findFirst. It has all of the arguments that findFirst.

const prisma = new PrismaClient().$extends({
model: {
$allModels: {
// Define a new `exists` operation on all models
// T is a generic type that corresponds to the current model
async exists<T>(
// `this` refers to the current type, e.g. `prisma.user` at runtime
this: T,

// The `exists` function will use the `where` arguments from the current model, `T`, and the `findFirst` operation
where: Prisma.Args<T, 'findFirst'>['where']
): Promise<boolean> {
// Retrieve the current model at runtime
const context = Prisma.getExtensionContext(this)

// Prisma Client query that retrieves data based
const result = await (context as any).findFirst({ where })
return result !== null
},
},
},
})

async function main() {
const user = await prisma.user.exists({ name: 'Alice' })
const post = await prisma.post.exists({
OR: [
{ title: { contains: 'Prisma' } },
{ content: { contains: 'Prisma' } },
],
})
}

向方法添加自定义属性

¥Add a custom property to a method

以下示例说明了如何将自定义参数添加到扩展中的方法:

¥The following example illustrates how you can add custom arguments, to a method in an extension:

type CacheStrategy = {
swr: number
ttl: number
}

const prisma = new PrismaClient().$extends({
model: {
$allModels: {
findMany<T, A>(
this: T,
args: Prisma.Exact<
A,
// For the `findMany` method, use the arguments from model `T` and the `findMany` method
// and intersect it with `CacheStrategy` as part of `findMany` arguments
Prisma.Args<T, 'findMany'> & CacheStrategy
>
): Prisma.Result<T, A, 'findMany'> {
// method implementation with the cache strategy
},
},
},
})

async function main() {
await prisma.post.findMany({
cacheStrategy: {
ttl: 360,
swr: 60,
},
})
}

这里的例子只是概念性的。为了使实际的缓存发挥作用,你必须实现逻辑。如果你对缓存扩展/服务感兴趣,我们建议你查看 Prisma 加速

¥The example here is only conceptual. For the actual caching to work, you will have to implement the logic. If you're interested in a caching extension/ service, we recommend taking a look at Prisma Accelerate.