Skip to main content

部署到 Render

本指南介绍如何部署使用 Prisma ORM 和 PostgreSQL 进行渲染的 Node.js 服务器。

¥This guide explains how to deploy a Node.js server that uses Prisma ORM and PostgreSQL to Render.

Prisma Render 部署示例 包含一个带有 REST 端点和简单前端的 Express.js 应用。此应用使用 Prisma Client 从其数据库中获取、创建和删除记录。

¥The Prisma Render deployment example contains an Express.js application with REST endpoints and a simple frontend. This app uses Prisma Client to fetch, create, and delete records from its database.

关于 Render

¥About Render

Render 是一个云应用平台,可让开发者轻松部署和扩展全栈应用。对于此示例,了解以下内容很有帮助:

¥Render is a cloud application platform that lets developers easily deploy and scale full-stack applications. For this example, it's helpful to know:

  • Render 允许你部署长时间运行的 "serverful" 全栈应用。你可以根据 CPU 和/或内存使用情况将 Render 服务配置为 autoscale。这是你可以选择的几个 部署范例 之一。

    ¥Render lets you deploy long-running, "serverful" full-stack applications. You can configure Render services to autoscale based on CPU and/or memory usage. This is one of several deployment paradigms you can choose from.

  • Render 原生支持 常见运行时,包括 Node.js 和 Bun。在本指南中,我们将使用 Node.js 运行时。

    ¥Render natively supports common runtimes, including Node.js and Bun. In this guide, we'll use the Node.js runtime.

  • Render 与 Git repos 集成 可在提交后自动部署。你可以从 GitHub、GitLab 或 Bitbucket 部署到 Render。在本指南中,我们将从 Git 存储库部署。

    ¥Render integrates with Git repos for automatic deployments upon commits. You can deploy to Render from GitHub, GitLab, or Bitbucket. In this guide, we'll deploy from a Git repository.

先决条件

¥Prerequisites

获取示例代码

¥Get the example code

示例代码 下载到本地机器。

¥Download the example code to your local machine.

curl https://codeload.github.com/prisma/prisma-examples/tar.gz/latest | tar -xz --strip=2 prisma-examples-latest/deployment-platforms/render
cd render

了解示例

¥Understand the example

在部署应用之前,让我们看一下示例代码。

¥Before we deploy the app, let's take a look at the example code.

Web 应用

¥Web application

Express 应用的逻辑位于两个文件中:

¥The logic for the Express app is in two files:

  • src/index.js:API。端点使用 Prisma Client 从数据库中获取、创建和删除数据。

    ¥src/index.js: The API. The endpoints use Prisma Client to fetch, create, and delete data from the database.

  • public/index.html:Web 前端。前端调用一些 API 端点。

    ¥public/index.html: The web frontend. The frontend calls a few of the API endpoints.

Prisma 模式和迁移

¥Prisma schema and migrations

此应用的 Prisma 组件位于两个文件中:

¥The Prisma components of this app are in two files:

  • prisma/schema.prisma:此应用的数据模型。此示例定义了两个模型,UserPost。此文件的格式遵循 Prisma 架构

    ¥prisma/schema.prisma: The data model of this app. This example defines two models, User and Post. The format of this file follows the Prisma schema.

  • prisma/migrations/<migration name>/migration.sql:在 PostgreSQL 数据库中构建此模式的 SQL 命令。你可以通过运行 prisma migrate dev 自动生成像这样的迁移文件。

    ¥prisma/migrations/<migration name>/migration.sql: The SQL commands that construct this schema in a PostgreSQL database. You can auto-generate migration files like this one by running prisma migrate dev.

渲染蓝图

¥Render Blueprint

render.yaml 文件是 Render 蓝图。蓝图是 Render 的基础设施即代码格式。你可以使用 Blueprint 以编程方式在 Render 上创建和修改服务。

¥The render.yaml file is a Render blueprint. Blueprints are Render's Infrastructure as Code format. You can use a Blueprint to programmatically create and modify services on Render.

render.yaml 定义了将由蓝图在渲染时启动的服务。在此 render.yaml 中,我们看到:

¥A render.yaml defines the services that will be spun up on Render by a Blueprint. In this render.yaml, we see:

  • 使用 Node 运行时的 Web 服务:这是 Express 应用。

    ¥A web service that uses a Node runtime: This is the Express app.

  • PostgreSQL 数据库:这是 Express 应用使用的数据库。

    ¥A PostgreSQL database: This is the database that the Express app uses.

此文件的格式遵循 蓝图规范

¥The format of this file follows the Blueprint specification.

Render 部署如何与 Prisma Migrate 配合使用

¥How Render deploys work with Prisma Migrate

通常,你希望在启动 Web 应用之前运行所有数据库迁移。否则,应用在查询没有预期表和行的数据库时可能会遇到错误。

¥In general, you want all your database migrations to run before your web app is started. Otherwise, the app may hit errors when it queries a database that doesn't have the expected tables and rows.

你可以在 Render 部署中使用预部署命令设置在启动应用之前运行任何命令,例如数据库迁移。

¥You can use the Pre-Deploy Command setting in a Render deploy to run any commands, such as database migrations, before the app is started.

有关预部署命令的更多详细信息,请参阅 Render 的部署指南

¥For more details about the Pre-Deploy Command, see Render's deploy guide.

在我们的示例代码中,render.yaml 显示 Web 服务的构建命令、预部署命令和启动命令。值得注意的是,npx prisma migrate deploy(预部署命令)将在 npm run start(启动命令)之前运行。

¥In our example code, the render.yaml shows the web service's build command, pre-deploy command, and start command. Notably, npx prisma migrate deploy (the pre-deploy command) will run before npm run start (the start command).

命令
构建命令npm install --production=false
预部署命令npx prisma migrate deploy
启动命令npm run start

部署示例

¥Deploy the example

1. 初始化你的 Git 存储库

¥ Initialize your Git repository

  1. 示例代码 下载到本地机器。

    ¥Download the example code to your local machine.

  2. 在 GitHub、GitLab 或 BitBucket 上创建一个新的 Git 存储库。

    ¥Create a new Git repository on GitHub, GitLab, or BitBucket.

  3. 将示例代码上传到你的新存储库。

    ¥Upload the example code to your new repository.

2. 手动部署

¥ Deploy manually

  1. 在渲染仪表板中,单击新建 > PostgreSQL。提供数据库名称并选择计划。(免费计划适用于此演示。)

    ¥In the Render Dashboard, click New > PostgreSQL. Provide a database name, and select a plan. (The Free plan works for this demo.)

  2. 数据库准备好后,查找其 内部 URL

    ¥After your database is ready, look up its internal URL.

  3. 在 Render 仪表板中,单击新建 > Web 服务并连接包含示例代码的 Git 存储库。

    ¥In the Render Dashboard, click New > Web Service and connect the Git repository that contains the example code.

  4. 在服务创建期间提供以下值:

    ¥Provide the following values during service creation:

设置
语言Node
构建命令npm install --production=false
预部署命令(注意:这可能在 "高级" 选项卡中)npx prisma migrate deploy
启动命令npm run start
环境变量DATABASE_URL 设置为数据库的内部 URL

就是这样。构建完成后,你的 Web 服务将在其 onrender.com URL 上线。

¥That’s it. Your web service will be live at its onrender.com URL as soon as the build finishes.

3. (可选)使用基础设施即代码部署

¥ (optional) Deploy with Infrastructure as Code

你还可以使用 Render Blueprint 部署示例。按照 Render 的 [蓝图设置指南] 并在示例中使用 render.yaml

¥You can also deploy the example using the Render Blueprint. Follow Render's [Blueprint setup guide] and use the render.yaml in the example.

奖金:种子数据库

¥Bonus: Seed the database

Prisma ORM 包含一个带有入门数据的 播种数据库 框架。在我们的示例中,prisma/seed.js 定义了一些测试用户和帖子。

¥Prisma ORM includes a framework for seeding the database with starter data. In our example, prisma/seed.js defines some test users and posts.

要将这些用户添加到数据库,我们可以:

¥To add these users to the database, we can either:

  1. 将种子脚本添加到我们的预部署命令中,或者

    ¥Add the seed script to our Pre-Deploy Command, or

  2. 通过 SSH shell 在我们的服务器上手动运行命令

    ¥Manually run the command on our server via an SSH shell

方法 1:预部署命令

¥Method 1: Pre-Deploy Command

如果你手动部署了渲染服务:

¥If you manually deployed your Render services:

  1. 在 Render 仪表板中,导航到你的 Web 服务。

    ¥In the Render dashboard, navigate to your web service.

  2. 选择设置。

    ¥Select Settings.

  3. 将预部署命令设置为:npx prisma migrate deploy; npx prisma db seed

    ¥Set the Pre-Deploy Command to: npx prisma migrate deploy; npx prisma db seed

如果你使用 Blueprint 部署了渲染服务:

¥If you deployed your Render services using the Blueprint:

  1. 在你的 render.yaml 文件中,将 preDeployCommand 更改为:npx prisma migrate deploy; npx prisma db seed

    ¥In your render.yaml file, change the preDeployCommand to: npx prisma migrate deploy; npx prisma db seed

  2. 将更改提交到你的 Git 存储库。

    ¥Commit the change to your Git repo.

方法 2:SSH

¥Method 2: SSH

Render 允许你通过 SSH 进入你的 Web 服务。

¥Render allows you to SSH into your web service.

  1. 按照 Render 的 SSH 指南 连接到你的服务器。

    ¥Follow Render's SSH guide to connect to your server.

  2. 在 shell 中,运行:npx prisma db seed

    ¥In the shell, run: npx prisma db seed