Skip to main content

将 Prisma ORM 添加到现有项目 (JavaScript 和 SQL Server)

了解如何将 Prisma ORM 添加到现有 Node.js 或 TypeScript 项目,方法是将其连接到数据库并生成用于数据库访问的 Prisma 客户端。以下教程向你介绍 Prisma CLIPrisma 客户端Prisma 内省

¥Learn how to add Prisma ORM to an existing Node.js or TypeScript project by connecting it to your database and generating a Prisma Client for database access. The following tutorial introduces you to the Prisma CLI, Prisma Client, and Prisma Introspection.

提示

如果你要从另一个 ORM 迁移到 Prisma ORM,请参阅我们的 从 TypeORM 迁移从 Sequelize 迁移 迁移指南。

¥If you're migrating to Prisma ORM from another ORM, see our Migrate from TypeORM or Migrate from Sequelize migration guides.

先决条件

¥Prerequisites

为了成功完成本指南,你需要:

¥In order to successfully complete this guide, you need:

请参阅 系统要求 了解确切的版本要求。

¥See System requirements for exact version requirements.

确保你手头有数据库 连接网址(包括你的身份验证凭据)!如果你没有运行数据库服务器而只想探索 Prisma ORM,请查看 快速开始

¥Make sure you have your database connection URL (that includes your authentication credentials) at hand! If you don't have a database server running and just want to explore Prisma ORM, check out the Quickstart.

设置 Prisma ORM

¥Set up Prisma ORM

第一步,导航到包含 package.json 文件的项目目录。

¥As a first step, navigate into your project directory that contains the package.json file.

接下来,将 Prisma CLI 添加为项目的开发依赖:

¥Next, add the Prisma CLI as a development dependency to your project:

npm install prisma --save-dev
注意

如果你的项目包含多个带有 package.json 文件的目录(例如 frontendbackend 等),请注意 Prisma ORM 专门设计用于 API/后端层。要设置 Prisma,请导航到包含相关 package.json 文件的相应后端目录并在那里配置 Prisma。

¥If your project contains multiple directories with package.json files (e.g., frontend, backend, etc.), note that Prisma ORM is specifically designed for use in the API/backend layer. To set up Prisma, navigate to the appropriate backend directory containing the relevant package.json file and configure Prisma there.

你现在可以通过添加 npx 前缀来调用 Prisma CLI:

¥You can now invoke the Prisma CLI by prefixing it with npx:

npx prisma
信息

请参阅 安装说明 了解如何使用不同的包管理器安装 Prisma ORM。

¥See installation instructions to learn how to install Prisma ORM using a different package manager.

接下来,使用以下命令创建 Prisma 模式 文件来设置 Prisma ORM 项目:

¥Next, set up your Prisma ORM project by creating your Prisma Schema file with the following command:

npx prisma init --datasource-provider sqlserver --output ../generated/prisma

此命令执行以下几项操作:

¥This command does a few things:

  • 创建一个名为 prisma 的新目录,其中包含一个名为 schema.prisma 的文件,该文件包含 Prisma Schema、数据库连接变量和模式模型。

    ¥Creates a new directory called prisma that contains a file called schema.prisma, which contains the Prisma Schema with your database connection variable and schema models.

  • 分别将 datasource 设置为 SQLServer,并将输出设置为自定义位置。

    ¥Sets the datasource to SQLServer and the output to a custom location, respectively.

  • 在项目根目录中创建 .env 文件,用于定义环境变量(例如数据库连接)。

    ¥Creates the .env file in the root directory of the project, which is used for defining environment variables (such as your database connection)

请注意,prisma init 创建的默认架构使用 PostgreSQL 作为 provider。如果你未使用 datasource-provider 选项指定提供程序,则需要编辑 datasource 块以使用 sqlserver 提供程序:

¥Note that the default schema created by prisma init uses PostgreSQL as the provider. If you didn't specify a provider with the datasource-provider option, you need to edit the datasource block to use the sqlserver provider instead:

prisma/schema.prisma
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}