Skip to main content

在现有的 TypeScript 项目中安装 Prisma 客户端 (PostgreSQL)

安装并生成 Prisma 客户端

¥Install and generate Prisma Client

要开始使用 Prisma 客户端,请先安装 @prisma/client 包:

¥To get started with Prisma Client, first install the @prisma/client package:

npm install @prisma/client

然后,运行 prisma generate,它会读取你的 Prisma 模式并生成 Prisma 客户端。

¥Then, run prisma generate which reads your Prisma schema and generates the Prisma Client.

npx prisma generate

你现在可以从 @prisma/client 包中导入 PrismaClient 构造函数来创建 Prisma Client 实例以将查询发送到你的数据库。你将在下一节中学习如何执行此操作。

¥You can now import the PrismaClient constructor from the @prisma/client package to create an instance of Prisma Client to send queries to your database. You'll learn how to do that in the next section.

有用

运行 prisma generate 时,你实际上是在创建代码(TypeScript 类型、方法、查询等),这些代码是根据 Prisma 模式文件或 prisma 目录中的文件定制的。这意味着,每当你更改 Prisma 模式文件时,你还需要更新 Prisma 客户端。你可以通过运行 prisma generate 命令来执行此操作。

¥When you run prisma generate, you are actually creating code (TypeScript types, methods, queries, ...) that is tailored to your Prisma schema file or files in the prisma directory. This means, that whenever you make changes to your Prisma schema file, you also need to update the Prisma Client. You can do this by running the prisma generate command.

Install and generate Prisma Client

每当你更新 Prisma 架构时,你都必须使用 prisma migrate devprisma db push 更新数据库架构。这将使你的数据库架构与 Prisma 架构保持同步。这些命令也会在后台运行 prisma generate 以重新生成你的 Prisma 客户端。

¥Whenever you update your Prisma schema, you will have to update your database schema using either prisma migrate dev or prisma db push. This will keep your database schema in sync with your Prisma schema. These commands will also run prisma generate under the hood to re-generate your Prisma Client.