Skip to main content

安装 Prisma 客户端(TypeScript 和 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.

这些命令在使用 Prisma 管理数据库模式时有不同的用途。以下是何时以及为何使用每个方法的细分:

¥These commands serve different purposes in managing your database schema with Prisma. Here’s a breakdown of when and why to use each:

npx prisma migrate dev

  • 目的:此命令根据你的 Prisma 模式更改生成并应用新的迁移。它会创建保留更改历史记录的迁移文件。

    ¥Purpose: This command generates and applies a new migration based on your Prisma schema changes. It creates migration files that keep a history of changes.

  • 用例:当你想要维护数据库更改记录时使用此功能,这对于生产环境或团队合作至关重要。它允许对数据库架构进行版本控制。

    ¥Use Case: Use this when you want to maintain a record of database changes, which is essential for production environments or when working in teams. It allows for version control of your database schema.

  • 优点:此命令还包括以受控方式应用迁移的检查,以确保数据完整性。

    ¥Benefits: This command also includes checks for applying migrations in a controlled manner, ensuring data integrity.

npx prisma db push

  • 目的:此命令用于将你当前的 Prisma 模式直接推送到数据库。它会应用你对架构所做的任何更改,而无需创建迁移文件。

    ¥Purpose: This command is used to push your current Prisma schema to the database directly. It applies any changes you've made to your schema without creating migration files.

  • 用例:在开发阶段,当你想要快速将数据库模式与 Prisma 模式同步而不必担心迁移历史记录时,它特别有用。

    ¥Use Case: It’s particularly useful during the development phase when you want to quickly sync your database schema with your Prisma schema without worrying about migration history.

  • 警告:如果架构更改影响现有表或列,它可以覆盖数据,因此它最适合早期开发或原型设计。

    ¥Caution: It can overwrite data if your schema changes affect existing tables or columns, so it’s best for early-stage development or prototyping.