生成器
Prisma 模式可以有一个或多个生成器,由 generator
块表示:
¥A Prisma schema can have one or more generators, represented by the generator
block:
generator client {
provider = "prisma-client-js"
output = "./generated/prisma-client-js"
}
生成器确定当你运行 prisma generate
命令时创建哪些资源。
¥A generator determines which assets are created when you run the prisma generate
command.
Prisma 客户端有两个生成器:
¥There are two generators for Prisma Client:
-
prisma-client-js
:将 Prisma 客户端生成到node_modules
¥
prisma-client-js
: Generates Prisma Client intonode_modules
-
prisma-client
(抢先体验):更新、更灵活的prisma-client-js
版本,支持 ESM;它输出纯 TypeScript 代码,并需要自定义output
路径。¥
prisma-client
(Early Access): Newer and more flexible version ofprisma-client-js
with ESM support; it outputs plain TypeScript code and requires a customoutput
path
或者,你可以配置任何符合我们生成器规范的 npm 包。
¥Alternatively, you can configure any npm package that complies with our generator specification.
prisma-client-js
prisma-client-js
是 Prisma ORM 6.X 及之前版本默认的生成器。它需要 @prisma/client
npm 包,并将 Prisma 客户端生成到 node_modules
中。
¥The prisma-client-js
is the default generator for Prisma ORM 6.X versions and before. It requires the @prisma/client
npm package and generates Prisma Client into node_modules
.
字段参考
¥Field reference
Prisma JavaScript 客户端的生成器接受多个附加属性:
¥The generator for Prisma's JavaScript Client accepts multiple additional properties:
-
previewFeatures
:预览功能 包括¥
previewFeatures
: Preview features to include -
binaryTargets
:prisma-client-js
的引擎二进制目标(例如,如果你要部署到 Ubuntu 18+,则为debian-openssl-1.1.x
;如果你在本地工作,则为native
)¥
binaryTargets
: Engine binary targets forprisma-client-js
(for example,debian-openssl-1.1.x
if you are deploying to Ubuntu 18+, ornative
if you are working locally)
generator client {
provider = "prisma-client-js"
previewFeatures = ["sample-preview-feature"]
binaryTargets = ["debian-openssl-1.1.x"] // defaults to `"native"`
}
二进制目标
¥Binary targets
从 v6.7.0 开始,Prisma ORM 具有 queryCompiler
预览功能。
¥As of v6.7.0, Prisma ORM has the queryCompiler
Preview feature.
启用后,你的 Prisma 客户端将生成 无需基于 Rust 的查询引擎二进制文件:
¥When enabled, your Prisma Client will be generated without a Rust-based query engine binary:
generator client {
provider = "prisma-client-js"
previewFeatures = ["queryCompiler", "driverAdapters"]
}
请注意,驱动适配器 预览功能与 queryCompiler
功能同时启用。
¥Note that the driver adapters Preview feature is required alongside queryCompiler
.
使用 queryCompiler
预览功能时,binaryTargets
字段已过时且不再需要。
¥When using the queryCompiler
Preview feature, the binaryTargets
field is obsolete and not needed.
prisma-client-js
生成器使用多个 engines。引擎使用 Rust 实现,并以可执行的、平台相关的引擎文件的形式由 Prisma 客户端使用。根据你执行代码的平台,你需要正确的文件。"二进制目标" 用于定义目标平台应存在哪些文件。
¥The prisma-client-js
generator uses several engines. Engines are implemented in Rust and are used by Prisma Client in the form of executable, platform-dependent engine files. Depending on which platform you are executing your code on, you need the correct file. "Binary targets" are used to define which files should be present for the target platform(s).
当你的应用投入生产时,正确的文件尤其重要,因为生产环境通常与你的本地开发环境不同。
¥The correct file is particularly important when deploying your application to production, which often differs from your local development environment.
native
二进制目标
¥The native
binary target
native
二进制目标很特殊。它不映射到具体的操作系统。相反,当在 binaryTargets
中指定 native
时,Prisma Client 会检测当前操作系统并自动为其指定正确的二进制目标。
¥The native
binary target is special. It doesn't map to a concrete operating system. Instead, when native
is specified in binaryTargets
, Prisma Client detects the current operating system and automatically specifies the correct binary target for it.
例如,假设你正在运行 macOS 并指定以下生成器:
¥As an example, assume you're running macOS and you specify the following generator:
generator client {
provider = "prisma-client-js"
binaryTargets = ["native"]
}
在这种情况下,Prisma 客户端会检测你的操作系统并根据 支持的操作系统列表 找到正确的二进制文件。 如果你使用 macOS Intel x86 (darwin
),则将选择为 darwin
编译的二进制文件。如果你使用 macOS ARM64 (darwin-arm64
),则将选择为 darwin-arm64
编译的二进制文件。
¥In that case, Prisma Client detects your operating system and finds the right binary file for it based on the list of supported operating systems .
If you use macOS Intel x86 (darwin
), then the binary file that was compiled for darwin
will be selected.
If you use macOS ARM64 (darwin-arm64
), then the binary file that was compiled for darwin-arm64
will be selected.
注意:
native
二进制目标是默认值。如果你希望包含额外的 二进制目标 以部署到不同的环境,你可以明确设置它。¥Note: The
native
binary target is the default. You can set it explicitly if you wish to include additional binary targets for deployment to different environments.
prisma-client
(抢先体验版)
¥prisma-client
(Early Access)
新的 prisma-client
生成器在不同的 JavaScript 环境(例如 ESM、Bun、Deno 等)中使用 Prisma ORM 时,提供了更强大的控制力和灵活性。
¥The new prisma-client
generator offers greater control and flexibility when using Prisma ORM across different JavaScript environments (such as ESM, Bun, Deno, ...).
它会将 Prisma 客户端生成到应用代码库中的自定义目录中,该目录通过 generator
块中的 output
字段指定。这将使你能够完全查看和控制生成的代码。它还将生成的 Prisma 客户端库 splits 到多个文件中。
¥It generates Prisma Client into a custom directory in your application's codebase that's specified via the output
field on the generator
block. This gives you full visibility and control over the generated code. It also splits the generated Prisma Client library into multiple files.
目前在 抢先体验 中,此生成器可确保你以所需的方式打包应用代码,而无需依赖隐藏或自动行为。
¥Currently in Early Access, this generator ensures you can bundle your application code exactly the way you want, without relying on hidden or automatic behaviors.
与 prisma-client-js
相比,主要区别如下:
¥Here are the main differences compared to prisma-client-js
:
-
需要
output
路径;不再需要“魔法”生成node_modules
。¥Requires an
output
path; no “magic” generation intonode_modules
any more -
通过
moduleFormat
字段支持 ESM 和 CommonJS¥Supports ESM and CommonJS via the
moduleFormat
field -
由于添加了字段,因此更加灵活
¥More flexible thanks to additional fields
-
输出纯 TypeScript,与应用的其他代码一样打包在一起。
¥Outputs plain TypeScript that's bundled just like the rest of your application code
prisma-client
生成器将成为 Prisma ORM v7 的新默认生成器。
¥The prisma-client
generator will become the new default with Prisma ORM v7.
入门
¥Getting started
按照以下步骤在你的项目中使用新的 prisma-client
生成器。
¥Follow these steps to use the new prisma-client
generator in your project.
1. 在 schema.prisma
中配置 prisma-client
生成器
¥ Configure the prisma-client
generator in schema.prisma
更新你的 generator
块:
¥Update your generator
block:
generator client {
provider = "prisma-client" // Required
output = "../src/generated/prisma" // Required path
}
output
选项是必需的,它告诉 Prisma ORM 将生成的 Prisma 数据放在何处客户端代码。你可以选择任何适合你项目结构的位置。例如,如果你有以下布局:
¥The output
option is required and tells Prisma ORM where to put the generated Prisma Client code. You can choose any location suitable for your project structure. For instance, if you have the following layout:
.
├── package.json
├── prisma
│ └── schema.prisma
├── src
│ └── index.ts
└── tsconfig.json
然后,../src/generated/prisma
将生成的代码放置在 src/generated/prisma
中,并相对于 schema.prisma
进行放置。
¥Then ../src/generated/prisma
places the generated code in src/generated/prisma
relative to schema.prisma
.
2. 生成 Prisma 客户端
¥ Generate Prisma Client
运行以下命令生成 Prisma 客户端:
¥Generate Prisma Client by running:
npx prisma generate
这会将 Prisma 客户端的代码(包括查询引擎二进制文件)生成到指定的 output
文件夹中。
¥This generates the code for Prisma Client (including the query engine binary) into the specified output
folder.
3. 从版本控制中排除生成的目录
¥ Exclude the generated directory from version control
新的生成器包含 TypeScript 客户端代码和 查询引擎。将查询引擎纳入版本控制可能会导致不同机器上的兼容性问题。为避免这种情况,请将生成的目录添加到 .gitignore
:
¥The new generator includes both the TypeScript client code and the query engine. Including the query engine in version control can cause compatibility issues on different machines. To avoid this, add the generated directory to .gitignore
:
# Keep the generated Prisma Client + query engine out of version control
/src/generated/prisma
将来,你可以在 Prisma ORM 已完全从 Rust 过渡到 TypeScript 时安全地将生成的目录包含在版本控制中。
¥In the future, you can safely include the generated directory in version control when Prisma ORM is fully transitioned from Rust to TypeScript.
4. 在应用中使用 Prisma 客户端
¥ Use Prisma Client in your application
导入 Prisma 客户端
¥Importing Prisma Client
生成 Prisma 客户端后,从你指定的路径导入它:
¥After generating the Prisma Client, import it from the path you specified:
import { PrismaClient } from "./generated/prisma/client.js"
const prisma = new PrismaClient()
Prisma 客户端现在可以在你的项目中使用了。
¥Prisma Client is now ready to use in your project.
导入生成的模型类型
¥Importing generated model types
如果你要导入为模型生成的类型,可以按如下方式操作:
¥If you're importing types generated for your models, you can do so as follows:
import { User, Post } from "./generated/prisma/models.js"
导入生成的枚举类型
¥Importing generated enum types
如果你要导入为枚举生成的类型,可以按如下方式操作:
¥If you're importing types generated for your enums, you can do so as follows:
import { Role } from "./generated/prisma/enums.js"
字段参考
¥Field reference
在 generator client { ... }
块中使用以下选项。仅需要 output
。其他字段使用默认值,或根据你的环境和 tsconfig.json
推断得出。
¥Use the following options in the generator client { ... }
block. Only output
is required. The other fields have defaults or are inferred from your environment and tsconfig.json
.
generator client {
// Required
provider = "prisma-client"
output = "../src/generated/prisma"
// Optional
runtime = "nodejs"
moduleFormat = "esm"
generatedFileExtension = "ts"
importFileExtension = "ts"
}
以下是 prisma-client
生成器的选项:
¥Below are the options for the prisma-client
generator:
选项 | 默认 | 描述 |
---|---|---|
output (必需) | 生成 Prisma 客户端的目录,例如 ../src/generated/prisma 。 | |
runtime | nodejs | 目标运行时环境。 支持的值:nodejs (别名 node )、deno 、bun 、deno-deploy 、workerd (别名 cloudflare )、edge-light (别名 vercel )、react-native 。 |
moduleFormat | 从环境推断 | 模块格式(esm 或 cjs )。确定使用 import.meta.url 还是 __dirname 。 |
generatedFileExtension | ts | 生成的 TypeScript 文件(ts 、mts 、cts )的文件扩展名。 |
importFileExtension | 从环境推断 | 导入语句中使用的文件扩展名。可以是 ts 、mts 、cts 、js 、mjs 、cjs 或空(用于裸导入)。 |
输出拆分和导入类型
¥Output splitting and importing types
prisma-client-js
生成器用于将所有类型生成到单个 index.d.ts
文件中,这可能会导致大型架构出现 降低编辑器速度(例如,破坏自动补齐功能)。
¥The prisma-client-js
generator used to generate all typings into a single index.d.ts
file, which could lead to slowing down editors (e.g. breaking auto-complete) with large schemas.
新的 prisma-client
生成器现在将生成的 Prisma 客户端库拆分为多个文件,从而避免了单个输出文件过大的问题。
¥The new prisma-client
generator now splits the generated Prisma Client library into multiple files and thus avoids the problems of a single, large output file.
之前 (prisma-client-js
)
¥Before (prisma-client-js
)
generated/
└── prisma
├── client.ts
├── index.ts # -> this is split into multiple files in 6.7.0
└── libquery_engine-darwin.dylib.node
(prisma-client
) 之后
¥After (prisma-client
)
generated/
└── prisma
├── client.ts
├── commonInputTypes.ts
├── enums.ts
├── internal
│ ├── class.ts
│ └── prismaNamespace.ts
├── libquery_engine-darwin.dylib.node
├── models
│ ├── Post.ts
│ └── User.ts
└── models.ts
社区生成器
¥Community generators
如果你使用 多文件 Prisma Schema,现有生成器或新生成器不会受到影响,除非生成器手动读取模式。
¥Existing generators or new ones should not be affected if you are using a multi-file Prisma schema, unless a generator reads the schema manually.
以下是社区创建的生成器列表。
¥The following is a list of community created generators.
-
prisma-dbml-generator
:将 Prisma 模式转换为 数据库标记语言 (DBML),从而实现简单的可视化表示¥
prisma-dbml-generator
: Transforms the Prisma schema into Database Markup Language (DBML) which allows for an easy visual representation -
prisma-docs-generator
:为 Prisma 客户端生成单独的 API 参考¥
prisma-docs-generator
: Generates an individual API reference for Prisma Client -
prisma-json-schema-generator
:转换 JSON 架构 中的 Prisma 架构¥
prisma-json-schema-generator
: Transforms the Prisma schema in JSON schema -
prisma-json-types-generator
:添加对所有数据库的 强类型Json
字段的支持。它继续prisma-client-js
输出并更改 json 字段以匹配你提供的类型。帮助代码生成器、智能感知等等。所有这些都不会影响任何运行时代码。¥
prisma-json-types-generator
: Adds support for Strongly TypedJson
fields for all databases. It goes onprisma-client-js
output and changes the json fields to match the type you provide. Helping with code generators, intellisense and much more. All of that without affecting any runtime code. -
typegraphql-prisma
:为 Prisma 模型生成 类型 GraphQL CRUD 解析器¥
typegraphql-prisma
: Generates TypeGraphQL CRUD resolvers for Prisma models -
typegraphql-prisma-nestjs
:typegraphql-prisma
的复刻,它还为 Prisma 模型生成 CRUD 解析器,但为 NestJS 生成 CRUD 解析器¥
typegraphql-prisma-nestjs
: Fork oftypegraphql-prisma
, which also generates CRUD resolvers for Prisma models but for NestJS -
prisma-typegraphql-types-gen
:从你的 prisma 类型定义生成 类型 GraphQL 类类型和枚举,生成的输出可以进行编辑,而不会被下一代覆盖,并且能够在你编辑弄乱类型时纠正你。¥
prisma-typegraphql-types-gen
: Generates TypeGraphQL class types and enums from your prisma type definitions, the generated output can be edited without being overwritten by the next gen and has the ability to correct you when you mess up the types with your edits. -
nexus-prisma
:允许通过 GraphQL Nexus 将 Prisma 模型投影到 GraphQL¥
nexus-prisma
: Allows to project Prisma models to GraphQL via GraphQL Nexus -
prisma-nestjs-graphql
:从 Prisma 模式生成对象类型、输入、参数等,以用于@nestjs/graphql
模块¥
prisma-nestjs-graphql
: Generates object types, inputs, args, etc. from the Prisma Schema for usage with@nestjs/graphql
module -
prisma-appsync
:为 AWS 应用同步 生成成熟的 GraphQL API¥
prisma-appsync
: Generates a full-blown GraphQL API for AWS AppSync -
prisma-kysely
:为 TypeScript SQL 查询构建器 Kysely 生成类型定义。这对于从边缘运行时对数据库执行查询很有用,或者在不降低类型安全性的情况下编写在 Prisma 中不可能实现的更复杂的 SQL 查询。¥
prisma-kysely
: Generates type definitions for Kysely, a TypeScript SQL query builder. This can be useful to perform queries against your database from an edge runtime, or to write more complex SQL queries not possible in Prisma without dropping type safety. -
prisma-generator-nestjs-dto
:生成具有关系connect
和create
选项的 DTO 和实体类,以便与 NestJS 资源 和 @nestjs/swagger 一起使用¥
prisma-generator-nestjs-dto
: Generates DTO and Entity classes with relationconnect
andcreate
options for use with NestJS Resources and @nestjs/swagger -
prisma-erd-generator
:生成实体关系图¥
prisma-erd-generator
: Generates an entity relationship diagram -
prisma-generator-plantuml-erd
:生成器用于为 PlantUML 生成 ER 图。还可以通过激活该选项来生成 Markdown 和 Asciidoc 文档。¥
prisma-generator-plantuml-erd
: Generator to generate ER diagrams for PlantUML. Markdown and Asciidoc documents can also be generated by activating the option. -
prisma-class-generator
:从 Prisma 架构生成可用作 DTO、Swagger Response、TypeGraphQL 等的类。¥
prisma-class-generator
: Generates classes from your Prisma Schema that can be used as DTO, Swagger Response, TypeGraphQL and so on. -
zod-prisma
:从 Prisma 模型创建 Zod 模式。¥
zod-prisma
: Creates Zod schemas from your Prisma models. -
prisma-pothos-types
:可以更轻松地定义基于 Prisma 的对象类型,并帮助解决 n+1 关系查询。它还集成了 Relay 插件,使定义节点和连接变得简单高效。¥
prisma-pothos-types
: Makes it easier to define Prisma-based object types, and helps solve n+1 queries for relations. It also has integrations for the Relay plugin to make defining nodes and connections easy and efficient. -
prisma-generator-pothos-codegen
:自动生成输入类型(用作参数)和自动生成解耦的类型安全基本文件可以轻松地从 Prisma 模式为 绿萝 创建可定制的对象、查询和突变。可以选择从基本文件一次生成所有增删改查内容。¥
prisma-generator-pothos-codegen
: Auto generate input types (for use as args) and auto generate decoupled type-safe base files makes it easy to create customizable objects, queries and mutations for Pothos from Prisma schema. Optionally generate all crud at once from the base files. -
prisma-joi-generator
:从 Prisma 模式生成完整的 Joi 模式。¥
prisma-joi-generator
: Generate full Joi schemas from your Prisma schema. -
prisma-yup-generator
:从你的 Prisma 模式生成完整的 Yup 模式。¥
prisma-yup-generator
: Generate full Yup schemas from your Prisma schema. -
prisma-class-validator-generator
:从 Prisma 架构中触发 TypeScript 模型,并准备好类验证器验证。¥
prisma-class-validator-generator
: Emit TypeScript models from your Prisma schema with class validator validations ready. -
prisma-zod-generator
:从 Prisma 模式中触发 Zod 模式。¥
prisma-zod-generator
: Emit Zod schemas from your Prisma schema. -
prisma-trpc-generator
:触发完全实现的 tRPC 路由。¥
prisma-trpc-generator
: Emit fully implemented tRPC routers. -
prisma-json-server-generator
:触发一个可以与 json-server 一起运行的 JSON 文件。¥
prisma-json-server-generator
: Emit a JSON file that can be run with json-server. -
prisma-trpc-shield-generator
:从 Prisma 架构中触发 tRPC 屏蔽。¥
prisma-trpc-shield-generator
: Emit a tRPC shield from your Prisma schema. -
prisma-custom-models-generator
:根据 Prisma 建议,从 Prisma 架构中触发自定义模型。¥
prisma-custom-models-generator
: Emit custom models from your Prisma schema, based on Prisma recommendations. -
nestjs-prisma-graphql-crud-gen
:使用 NestJS 和 Prisma 从 GraphQL 架构生成 CRUD 解析器。¥
nestjs-prisma-graphql-crud-gen
: Generate CRUD resolvers from GraphQL schema with NestJS and Prisma. -
prisma-generator-dart
:使用 to- 和 fromJson 方法生成 Dart/Flutter 类文件。¥
prisma-generator-dart
: Generates Dart/Flutter class files with to- and fromJson methods. -
prisma-generator-graphql-typedef
:生成 graphql 模式。¥
prisma-generator-graphql-typedef
: Generates graphql schema. -
prisma-markdown
:生成由 ERD 图及其描述组成的 Markdown 文档。支持通过@namespace
注释标签对 ERD 图进行分页。¥
prisma-markdown
: Generates markdown document composed with ERD diagrams and their descriptions. Supports pagination of ERD diagrams through@namespace
comment tag. -
prisma-models-graph
:为模式生成双向模型图,无需在模式中定义严格的关系,通过自定义模式注释工作。¥
prisma-models-graph
: Generates a bi-directional models graph for schema without strict relationship defined in the schema, works via a custom schema annotation. -
prisma-generator-fake-data
:为你的 Prisma 模型生成逼真的假数据,可用于单元/集成测试、演示等。¥
prisma-generator-fake-data
: Generates realistic-looking fake data for your Prisma models that can be used in unit/integration tests, demos, and more. -
prisma-generator-drizzle
:Prisma 生成器可轻松生成 Drizzle 模式。¥
prisma-generator-drizzle
: A Prisma generator for generating Drizzle schema with ease. -
prisma-generator-express
:生成 Express CRUD 和 Router 生成器函数。¥
prisma-generator-express
: Generates Express CRUD and Router generator function. -
prismabox
:从 Prisma 模型生成多功能 typebox 模式。¥
prismabox
: Generates versatile typebox schema from your Prisma models. -
prisma-generator-typescript-interfaces
:从 Prisma 模式生成零依赖 TypeScript 接口。¥
prisma-generator-typescript-interfaces
: Generates zero-dependency TypeScript interfaces from your Prisma schema. -
prisma-openapi
:从 Prisma 模型生成 OpenAPI 模式。¥
prisma-openapi
: Generates OpenAPI schema from Prisma models.