Skip to main content

使用 Accelerate 连接池设置 Neon

15 min

介绍

¥Introduction

本指南将教你如何使用 Prisma 加速 向托管在 Neon 上的 PostgreSQL 数据库添加连接池。

¥This guides teaches you how to add connection pooling to a PostgreSQL database hosted on Neon using Prisma Accelerate.

Prisma Accelerate 是一个强大而成熟的连接池,可使你的数据库在流量高峰和高负载情况下正常运行。查看此 video 演示其在负载测试或 了解连接池为何如此重要重要 中的表现。

¥Prisma Accelerate is a robust and mature connection pooler enabling your database to function properly during traffic spikes and high load scenarios. Check out this video demonstrating how it performs in a load test or learn why connection pooling is important.

先决条件

¥Prerequisites

要成功完成本指南,你需要一个托管在 Neon 上的 PostgreSQL 实例的连接字符串。它通常类似于以下内容:

¥To successfully complete this guide, you need a connection string for a PostgreSQL instance hosted on Neon. It typically looks similar to this:

postgresql://neondb_owner:[YOUR-PASSWORD]@ep-lingering-hat-a2e7tkt3.eu-central-1.aws.neon.tech/neondb?sslmode=require

如果你已经有使用 Prisma ORM 的项目,则可以跳过前两个步骤直接跳到 步骤 3.安装 Accelerate 扩展

¥If you already have a project using Prisma ORM, you can skip the first two steps and jump ahead to Step 3. Install the Accelerate extension.

1. 设置 Prisma ORM

¥ Set up Prisma ORM

首先在项目中安装 Prisma CLI:

¥Start by installing the Prisma CLI in your project:

npm install prisma --save-dev

然后,运行以下命令初始化新项目:

¥Then, run the following command to initialize a new project:

npx prisma init

这将创建一个包含 schema.prisma 文件的新 prisma 目录,并添加一个包含 DATABASE_URL 环境变量的 .env 文件。

¥This will create a new prisma directory with a schema.prisma file and add a .env file with the DATABASE_URL environment variable.

更新文件,并将 DATABASE_URL 参数设置为 Neon 连接字符串:

¥Update the file and set the DATABASE_URL to your Neon connection string:

.env
DATABASE_URL="postgresql://neondb_owner:[YOUR-PASSWORD]@ep-lingering-hat-a2e7tkt3.eu-central-1.aws.neon.tech/neondb?sslmode=require"

2. 检查你的数据库

¥ Introspect your database

接下来,运行以下命令来检查数据库并创建数据模型:

¥Next, run the following command to introspect your database and create your data model:

npx prisma db pull

此命令读取数据库模式,并在 schema.prisma 文件中创建与数据库中的表匹配的新模型。

¥This command reads your database schema and creates new models in your schema.prisma file that match the tables in your database.

注意

如果你将来想使用 Prisma Migrate,则还需要 数据库的基线

¥If you want to use Prisma Migrate in the future, you also need to baseline your database.

3. 安装 Accelerate 扩展

¥ Install the Accelerate extension

安装 Accelerate 的 Prisma 客户端扩展:

¥Install the Prisma Client extension for Accelerate:

npm install @prisma/extension-accelerate

这是访问 Prisma Accelerate 连接池所必需的。

¥This is needed to access Prisma Accelerate's connection pool.

4. 在 Prisma 控制台中设置 Accelerate

¥ Set up Accelerate in the Prisma Console

要在 Prisma 控制台中设置 Accelerate,请按照以下步骤操作:

¥To set up Accelerate in the Prisma Console, follow these steps:

  1. 登录

    ¥Log into the .

  2. 选择新项目

    ¥Select New project

  3. 为你的项目选择一个名称

    ¥Choose a Name for your project

  4. 在“选择你的起始产品”部分,找到“Accelerate”卡片并点击“开始使用”

    ¥In the Choose your starting product section, find the Accelerate card and click Get started

  5. 在数据库连接字符串字段中,粘贴你的 Neon 连接字符串

    ¥In the field for your Database connection string, paste your Neon connection string

  6. 选择距离你数据库最近的区域

    ¥Select the Region that's closest to your database

  7. 点击“创建项目”

    ¥Click Create project

  8. 在下一个屏幕上,点击“启用加速”

    ¥On the next screen, click Enable Accelerate

完成这些步骤后,你将被重定向到另一个页面,你需要在该页面点击“生成 API 密钥”按钮。

¥Once you went through these steps, you'll be redirected to another page where you need to the click the Generate API key button.

然后,你将看到一个新的连接 URL,该 URL 可用于连接到 Prisma Accelerate 的连接池。需要将其设置为 .env 文件中的新 DATABASE_URL

¥You'll then be shown a new connection URL which enables you to connect to Prisma Accelerate's connection pool. This needs to be set as the new DATABASE_URL in your .env file:

.env
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=ey..."
注意

如果你要将 Prisma Migrate 与 Prisma Accelerate 一起使用,则可以在 datasource 块上设置 directUrl 字段:

¥If you want to use Prisma Migrate with Prisma Accelerate, you can set the directUrl field on the datasource block:

schema.prisma
datasource db {
url = env("DATABASE_URL") // points to the connection pool for queries
directUrl = env("DIRECT_URL") // points to the database for migrations
}

因此,你需要在你的 .env 文件:

¥Accordingly, you'll need to set the DIRECT_URL in your .env file:

.env
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=ey..."
DIRECT_URL="postgresql://neondb_owner:[YOUR-PASSWORD]@ep-lingering-hat-a2e7tkt3.eu-central-1.aws.neon.tech/neondb?sslmode=require"

5. 生成 Prisma 客户端

¥ Generate Prisma Client

使用 Prisma 模式设置完成后,你可以继续生成 Prisma 客户端:

¥With your Prisma schema in place, you can go ahead and generate Prisma Client:

npx prisma generate --no-engine

--no-engine 选项用于在生成的 Prisma 客户端库中省略 查询引擎。查询引擎管理 Prisma ORM 的内部连接池,在使用 Prisma Accelerate 时无需使用它。

¥The --no-engine option is used to omit the query engine in the generated Prisma Client library. The query engine manages Prisma ORM's internal connection pool and is not needed when using Prisma Accelerate.

6. 通过连接池发送查询

¥ Send queries through the connection pool

在你的应用代码中,你现在需要将 Accelerate 扩展应用于你的 Prisma 客户端实例:

¥In your application code, you now need to apply the Accelerate extension to your Prisma Client instance:

import { PrismaClient } from "./generated/prisma"
import { withAccelerate } from "@prisma/extension-accelerate"

const prisma = new PrismaClient().$extends(withAccelerate())

此时,你现在可以开始发送查询,这些查询将通过连接池路由到你的数据库。

¥At this point, you can now start sending queries which will be routed through the connection pool to your database.


Stay connected with Prisma

Continue your Prisma journey by connecting with our active community. Stay informed, get involved, and collaborate with other developers:

We genuinely value your involvement and look forward to having you as part of our community!