部署到 Netlify
本指南涵盖了将使用 Prisma ORM 的应用部署到 Netlify 所需执行的步骤。
¥This guide covers the steps you will need to take in order to deploy your application that uses Prisma ORM to Netlify.
Netlify 是一个用于持续部署、静态站点和无服务器功能的云平台。Netlify 与 GitHub 无缝集成,可在提交时自动部署。当你按照以下步骤操作时,你将使用该方法创建 CI/CD 管道,用于从 GitHub 存储库部署应用。
¥Netlify is a cloud platform for continuous deployment, static sites, and serverless functions. Netlify integrates seamlessly with GitHub for automatic deployments upon commits. When you follow the steps below, you will use that approach to create a CI/CD pipeline that deploys your application from a GitHub repository.
先决条件
¥Prerequisites
在遵循本指南之前,你需要设置应用以开始部署到 Netlify。我们建议使用 "开始使用 Netlify" 指南进行快速概述,并建议使用 "部署函数" 指南来深入了解你的部署选项。
¥Before you can follow this guide, you will need to set up your application to begin deploying to Netlify. We recommend the "Get started with Netlify" guide for a quick overview and "Deploy functions" for an in-depth look at your deployment options.
schema.prisma
中的二进制目标
¥Binary targets in schema.prisma
由于你的代码将部署到 Netlify 的环境(不一定与你的开发环境相同),因此你需要设置 binaryTargets
以便在构建步骤中下载与 Netlify 运行时兼容的查询引擎。如果你不设置此选项,你部署的代码将部署不正确的查询引擎,并且将无法运行。
¥Since your code is being deployed to Netlify's environment, which isn't necessarily the same as your development environment, you will need to set binaryTargets
in order to download the query engine that is compatible with the Netlify runtime during your build step. If you do not set this option, your deployed code will have an incorrect query engine deployed with it and will not function.
根据 Node.js 的版本,你的 Prisma 架构应在 generator
块中包含 rhel-openssl-1.0.x
或 rhel-openssl-3.0.x
:
¥Depending on the version of Node.js, your Prisma schema should contain either rhel-openssl-1.0.x
or rhel-openssl-3.0.x
in the generator
block:
- Node.js 16 and 18
- Node.js 20+
binaryTargets = ["native", "rhel-openssl-1.0.x"]
binaryTargets = ["native", "rhel-openssl-3.0.x"]
在 Netlify 中存储环境变量
¥Store environment variables in Netlify
我们建议将 .env
文件保留在 .gitignore
中,以防止敏感连接字符串泄露。相反,你可以使用 Netlify CLI 来 import values into netlify directly。
¥We recommend keeping .env
files in your .gitignore
in order to prevent leakage of sensitives connection strings. Instead, you can use the Netlify CLI to import values into netlify directly.
假设你有一个如下所示的文件:
¥Assuming you have a file like the following:
# Connect to DB
DATABASE_URL="postgresql://postgres:__PASSWORD__@__HOST__:__PORT__/__DB_NAME__"
你可以使用 env:import
命令将文件作为环境变量上传:
¥You can upload the file as environment variables using the env:import
command:
netlify env:import .env
site: my-very-very-cool-site
---------------------------------------------------------------------------------.
Imported environment variables |
---------------------------------------------------------------------------------|
Key | Value |
--------------|------------------------------------------------------------------|
DATABASE_URL | postgresql://postgres:__PASSWORD__@__HOST__:__PORT__/__DB_NAME__ |
---------------------------------------------------------------------------------'
If you are not using an .env
file
如果你以不同的方法存储数据库连接字符串和其他环境变量,则需要手动将环境变量上传到 Netlify。这些选项是 Netlfiy 的文档中讨论了,一种方法是通过 UI 上传,如下所述。
¥If you are storing your database connection string and other environment variables in a different method, you will need to manually upload your environment variables to Netlify. These options are discussed in Netlfiy's documentation and one method, uploading via the UI, is described below.
-
打开站点的 Netlify 管理 UI。你可以按如下方式使用 Netlify CLI:
¥Open the Netlify admin UI for the site. You can use Netlify CLI as follows:
netlify open --admin
-
单击站点设置:
¥Click Site settings:
-
导航到左侧边栏中的“构建和部署”,然后选择“环境”。
¥Navigate to Build & deploy in the sidebar on the left and select Environment.
-
单击编辑变量并使用键
DATABASE_URL
创建一个变量,并将其值设置为数据库连接字符串。¥Click Edit variables and create a variable with the key
DATABASE_URL
and set its value to your database connection string.
-
单击“保存”。
¥Click Save.
现在开始新的 Netlify 构建和部署,以便新构建可以使用新上传的环境变量。
¥Now start a new Netlify build and deployment so that the new build can use the newly uploaded environment variables.
netlify deploy
你现在可以测试已部署的应用。
¥You can now test the deployed application.
连接池
¥Connection pooling
当你使用像 Netlify 这样的函数即服务提供商时,出于性能原因,池化数据库连接是有益的。这是因为每个函数调用都可能会导致与数据库的新连接,而该连接可能会很快耗尽打开的连接。
¥When you use a Function-as-a-Service provider, like Netlify, it is beneficial to pool database connections for performance reasons. This is because every function invocation may result in a new connection to your database which can quickly run out of open connections.
你可以使用 加速 进行连接池或使用具有内置连接池的 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.