部署到 Vercel
本指南将引导你完成设置和部署使用 Prisma 到 Vercel 的无服务器应用的步骤。
¥This guide takes you through the steps to set up and deploy a serverless application that uses Prisma to Vercel.
Vercel 是一个托管静态站点、无服务器和边缘功能的云平台。你可以将 Vercel 项目与 GitHub 存储库集成,以便在进行新提交时自动部署。
¥Vercel is a cloud platform that hosts static sites, serverless, and edge functions. You can integrate a Vercel project with a GitHub repository to allow you to deploy automatically when you make new commits.
我们使用 Next.js 创建了 示例应用,你可以在使用 Prisma 到 Vercel 部署应用时将其用作参考。
¥We created an example application using Next.js you can use as a reference when deploying an application using Prisma to Vercel.
虽然我们的示例使用 Next.js,但你可以将其他应用部署到 Vercel。请参阅 将 Express 与 Vercel 结合使用 和 Vercel 上的 Nuxt 作为其他选项的示例。
¥While our examples use Next.js, you can deploy other applications to Vercel. See Using Express with Vercel and Nuxt on Vercel as examples of other options.
构建配置
¥Build configuration
在 Vercel 构建期间更新 Prisma Client
¥Updating Prisma Client during Vercel builds
Vercel 将自动缓存部署的依赖。对于大多数应用来说,这不会导致任何问题。但是,对于 Prisma ORM,如果 Prisma 架构发生更改,可能会导致 Prisma 客户端版本过时。为了避免此问题,请将 prisma generate
添加到应用的 postinstall
脚本中:
¥Vercel will automatically cache dependencies on deployment. For most applications, this will not cause any issues. However, for Prisma ORM, it may result in an outdated version of Prisma Client on a change in your Prisma schema. To avoid this issue, add prisma generate
to the postinstall
script of your application:
{
...
"scripts": {
"postinstall": "prisma generate"
}
...
}
这将在构建时重新生成 Prisma 客户端,以便你的部署始终拥有最新的客户端。
¥This will re-generate Prisma Client at build time so that your deployment always has an up-to-date client.
如果你在部署到 Vercel 期间看到 prisma: command not found
错误,则说明你的依赖中缺少 prisma
。默认情况下,prisma
是开发依赖,可能需要移动为标准依赖。
¥If you see prisma: command not found
errors during your deployment to Vercel, you are missing prisma
in your dependencies. By default, prisma
is a dev dependency and may need to be moved to be a standard dependency.
避免 Prisma 客户端过时的另一个选择是使用 自定义输出路径 并将你的客户端签入版本控制。这样,每个部署都可以保证包含正确的 Prisma 客户端。
¥Another option to avoid an outdated Prisma Client is to use a custom output path and check your client into version control. This way each deployment is guaranteed to include the correct Prisma Client.
generator client {
provider = "prisma-client-js"
output = "./generated/client"
}
在 Vercel 上的 Monorepos 中部署 Prisma
¥Deploying Prisma in Monorepos on Vercel
如果你在 monorepo 中使用 Prisma(例如,使用 TurboRepo)并部署到 Vercel,你可能会遇到所需文件(例如 libquery_engine-rhel-openssl-3.0.x.so.node
)在部署的包中缺失的问题。这是因为 Vercel 积极优化无服务器部署,有时会删除必要的 Prisma 文件。要解决此问题,请使用 @prisma/nextjs-monorepo-workaround-plugin 插件,以确保 Prisma 引擎文件正确包含在最终打包包中。有关 Prisma 如何与 Webpack 和 Parcel 等不同打包器交互的更多详细信息,请参阅我们的 模块打包器 页面。
¥If you are using Prisma inside a monorepo (e.g., with TurboRepo) and deploying to Vercel, you may encounter issues where required files—such as libquery_engine-rhel-openssl-3.0.x.so.node
are missing from the deployed bundle. This is because Vercel aggressively optimizes serverless deployments, sometimes stripping out necessary Prisma files. To resolve this, use the @prisma/nextjs-monorepo-workaround-plugin plugin, which ensures that Prisma engine files are correctly included in the final bundle.
For more details on how Prisma interacts with different bundlers like Webpack and Parcel, see our Module bundlers page.
如果出现以下情况,此插件将不再使用:
¥The usage of this plugin becomes obsolet if:
-
你正在使用 不带 Rust 引擎的 Prisma ORM(通过
queryCompiler
功能标志)¥you are using Prisma ORM without Rust engines (via the
queryCompiler
feature flag) -
你正在使用 新的
prisma-client
生成器¥you are using the new
prisma-client
generator
CI/CD 工作流
¥CI/CD workflows
在更复杂的 CI/CD 环境中,你可能还希望使用在本地开发期间执行的任何迁移来更新数据库模式。你可以使用 prisma migrate deploy
命令来执行此操作。
¥In a more sophisticated CI/CD environment, you may additonally want to update the database schema with any migrations you have performed during local development. You can do this using the prisma migrate deploy
command.
在这种情况下,你可以在 package.json
中创建一个自定义构建命令(例如称为 vercel-build
),如下所示:
¥In that case, you could create a custom build command in your package.json
(e.g. called vercel-build
) that looks as follows:
{
...
"scripts" {
"vercel-build": "prisma generate && prisma migrate deploy && next build",
}
...
}
你可以使用以下命令在 CI/CD 管道内调用此脚本:
¥You can invoke this script inside your CI/CD pipeline using the following command:
npm run vercel-build
添加单独的数据库用于预览部署
¥Add a separate database for preview deployments
默认情况下,你的应用将具有与存储库的 main
git 分支关联的单个生产环境。如果你打开拉取请求来更改应用,Vercel 会创建一个新的预览环境。
¥By default, your application will have a single production environment associated with the main
git branch of your repository. If you open a pull request to change your application, Vercel creates a new preview environment.
Vercel 使用你在为生产环境和预览环境导入项目时定义的 DATABASE_URL
环境变量。如果你创建带有数据库架构迁移的拉取请求,这会导致问题,因为拉取请求将更改生产数据库的架构。
¥Vercel uses the DATABASE_URL
environment variable you define when you import the project for both the production and preview environments. This causes problems if you create a pull request with a database schema migration because the pull request will change the schema of the production database.
为了防止这种情况,请使用第二个托管数据库来处理预览部署。获得该连接字符串后,你可以使用 Vercel 仪表板为预览环境添加 DATABASE_URL
:
¥To prevent this, use a second hosted database to handle preview deployments. Once you have that connection string, you can add a DATABASE_URL
for your preview environment using the Vercel dashboard:
-
单击 Vercel 项目的“设置”选项卡。
¥Click the Settings tab of your Vercel project.
-
单击环境变量。
¥Click Environment variables.
-
添加密钥为
DATABASE_URL
的环境变量并仅选择预览环境选项:¥Add an environment variable with a key of
DATABASE_URL
and select only the Preview environment option:
-
将值设置为第二个数据库的连接字符串:
¥Set the value to the connection string of your second database:
postgresql://dbUsername:dbPassword@myhost:5432/mydb
-
单击“保存”。
¥Click Save.
连接池
¥Connection pooling
当你使用函数即服务提供程序(例如 Vercel Serverless 函数)时,每次调用都可能会导致与数据库的新连接。这可能会导致你的数据库快速耗尽打开的连接,并导致你的应用停止运行。因此,池化数据库连接至关重要。
¥When you use a Function-as-a-Service provider, like Vercel Serverless functions, every invocation may result in a new connection to your database. This can cause your database to quickly run out of open connections and cause your application to stall. For this reason, pooling connections to your database is essential.
你可以使用 加速 进行连接池或使用具有内置连接池的 Prisma Postgres 来减少 Prisma Client 包大小并避免冷启动。
¥You can use Accelerate for connection pooling or Prisma Postgres, which has built-in connection pooling, to reduce your Prisma Client bundle size, and to avoid cold starts.
有关无服务器环境连接管理的更多信息,请参阅我们的 连接管理指南。
¥For more information on connection management for serverless environments, refer to our connection management guide.