Skip to main content

生成 Prisma 客户端

Prisma 客户端是一个生成的数据库客户端,根据你的数据库模式量身定制。默认情况下,Prisma 客户端会生成到 node_modules/.prisma/client 文件夹中,但我们强烈建议使用 你指定输出位置

¥Prisma Client is a generated database client that's tailored to your database schema. By default, Prisma Client is generated into the node_modules/.prisma/client folder, but we highly recommend you specify an output location.

警告

在 Prisma ORM 7 中,默认情况下 Prisma 客户端将不再在 node_modules 中生成,并且需要定义输出路径。了解更多关于如何定义输出路径的信息

¥In Prisma ORM 7, Prisma Client will no longer be generated in node_modules by default and will require an output path to be defined. Learn more below on how to define an output path.

生成并实例化 Prisma 客户端:

¥To generate and instantiate Prisma Client:

  1. 确保你有 Prisma CLI 安装在你的计算机上

    ¥Ensure that you have Prisma CLI installed on your machine.

    npm install prisma --save-dev
  2. 将以下 generator 定义添加到你的 Prisma 架构中:

    ¥Add the following generator definition to your Prisma schema:

    generator client {
    provider = "prisma-client-js"
    output = "app/generated/prisma/client"
    }
    注意

    请随意自定义输出位置以匹配你的应用。常用目录包括 appsrc,甚至是项目的根目录。

    ¥Feel free to customize the output location to match your application. Common directories are app, src, or even the root of your project.

  3. 安装 @prisma/client npm 包:

    ¥Install the @prisma/client npm package:

    npm install @prisma/client
  4. 使用以下命令生成 Prisma 客户端:

    ¥Generate Prisma Client with the following command:

    prisma generate
  5. 你现在可以在代码中使用 实例化 Prisma 客户端

    ¥You can now instantiate Prisma Client in your code:

    import { PrismaClient } from 'app/generated/prisma/client'
    const prisma = new PrismaClient()
    // use `prisma` in your application to read and write data in your DB

重要的:每次对 Prisma 架构进行更改后,你都需要重新运行 prisma generate 命令以更新生成的 Prisma 客户端代码。

¥Important: You need to re-run the prisma generate command after every change that's made to your Prisma schema to update the generated Prisma Client code.

以下是生成 Prisma 客户端的典型工作流程的图解说明:

¥Here is a graphical illustration of the typical workflow for generation of Prisma Client:

Graphical illustration of the typical workflow for generation of Prisma Client

Prisma 客户端的位置

¥The location of Prisma Client

警告

我们强烈建议你定义自定义 output 路径。在 Prisma ORM 版本 6.6.0 中,未定义 output 路径将导致警告。在 Prisma ORM 7 中,该字段是必需的。

¥We strongly recommend you define a custom output path. In Prisma ORM version 6.6.0, not defining an output path will result in a warning. In Prisma ORM 7, the field will be required.

使用自定义 output 路径

¥Using a custom output path

例如,你还可以在 generator 配置上指定自定义 output 路径(假设你的 schema.prisma 文件位于默认的 prisma 子文件夹中):

¥You can also specify a custom output path on the generator configuration, for example (assuming your schema.prisma file is located at the default prisma subfolder):

generator client {
provider = "prisma-client-js"
output = "../src/generated/client"
}

为该架构文件运行 prisma generate 后,Prisma 客户端包将位于:

¥After running prisma generate for that schema file, the Prisma Client package will be located in:

./src/generated/client

要从自定义位置(例如,从名为 ./src/script.ts 的文件)导入 PrismaClient

¥To import the PrismaClient from a custom location (for example, from a file named ./src/script.ts):

import { PrismaClient } from './generated/client'
注意

为了提高与 ECMAScript 模块 (ESM) 的兼容性,并确保 Prisma ORM 在不同的 Node.js 运行时中行为一致,你还可以使用 prisma-client 生成器(预览版)。此生成器专门设计用于处理模块解析和运行时变化方面的常见挑战,提供更流畅的集成体验,并减少与打包器的摩擦。

¥For improved compatibility with ECMAScript modules (ESM) and to ensure consistent behaviour of Prisma ORM across different Node.js runtimes, you can also use the prisma-client generator (Preview). This generator is specifically designed to handle common challenges with module resolution and runtime variations, providing a smoother integration experience and less friction with bundlers.

@prisma/client npm 包

¥The @prisma/client npm package

@prisma/client npm 包由两个关键部分组成:

¥The @prisma/client npm package consists of two key parts:

  • @prisma/client 模块本身,仅在重新安装软件包时才会更改

    ¥The @prisma/client module itself, which only changes when you re-install the package

  • .prisma/client 文件夹,这是从你的架构生成的唯一 Prisma 客户端的 默认位置

    ¥The .prisma/client folder, which is the default location for the unique Prisma Client generated from your schema

@prisma/client/index.d.ts 导出 .prisma/client

¥@prisma/client/index.d.ts exports .prisma/client:

export * from '.prisma/client'

这意味着你仍然在自己的 .ts 文件中导入 @prisma/client

¥This means that you still import @prisma/client in your own .ts files:

import { PrismaClient } from '@prisma/client'

Prisma 客户端是根据你的 Prisma 模式生成的,并且对于你的项目来说是唯一的。每次更改架构(例如,通过执行 模式迁移)并运行 prisma generate 时,Prisma 客户端的代码都会更改:

¥Prisma Client is generated from your Prisma schema and is unique to your project. Each time you change the schema (for example, by performing a schema migration) and run prisma generate, Prisma Client's code changes:

The .prisma and @prisma folders

Node.js 包管理器中的 .prisma 文件夹不受 pruning 的影响。

¥The .prisma folder is unaffected by pruning in Node.js package managers.