部署到 Vercel Edge 功能和中间件
本页面涵盖了部署使用 Prisma 客户端与 Vercel 边缘中间件 中的数据库或部署到 Vercel 边缘运行时 的 维塞尔函数 中的数据库进行通信的应用所需了解的所有内容。
¥This page covers everything you need to know to deploy an app that uses Prisma Client for talking to a database in Vercel Edge Middleware or a Vercel Function deployed to the Vercel Edge Runtime.
要将 Vercel 函数部署到 Vercel Edge 运行时,你可以在 Vercel 函数的请求处理程序之外设置 export const runtime = 'edge'
。
¥To deploy a Vercel Function to the Vercel Edge Runtime, you can set export const runtime = 'edge'
outside the request handler of the Vercel Function.
部署到 Vercel Edge Functions 和 Edge Middleware 时的一般注意事项
¥General considerations when deploying to Vercel Edge Functions & Edge Middleware
使用 Prisma Postgres
¥Using Prisma Postgres
你可以在 Vercel 的边缘运行时中使用 Prisma Postgres。按照本指南获取有关 使用 Prisma Postgres 将应用部署到 Vercel 的端到端教程。
¥You can use Prisma Postgres in Vercel's edge runtime. Follow this guide for an end-to-end tutorial on deploying an application to Vercel using Prisma Postgres.
使用边缘兼容驱动程序
¥Using an edge-compatible driver
Vercel 的 Edge Runtime 目前仅支持一组有限的数据库驱动程序:
¥Vercel's Edge Runtime currently only supports a limited set of database drivers:
-
Neon 无服务器 使用 HTTP 访问数据库(也兼容 Vercel Postgres)
¥Neon Serverless uses HTTP to access the database (also compatible with Vercel Postgres)
-
PlanetScale 无服务器 使用 HTTP 访问数据库
¥PlanetScale Serverless uses HTTP to access the database
-
@libsql/client
用于访问 Turso 数据库¥
@libsql/client
is used to access Turso databases
请注意,Vercel Edge Functions 目前不支持 node-postgres
(pg
)。
¥Note that node-postgres
(pg
) is currently not supported on Vercel Edge Functions.
部署使用 Prisma ORM 的 Vercel Edge Function 时,你需要为 Prisma ORM 使用这些 边缘兼容驱动程序 之一及其相应的 驱动适配器。
¥When deploying a Vercel Edge Function that uses Prisma ORM, you need to use one of these edge-compatible drivers and its respective driver adapter for Prisma ORM.
如果你的应用使用 PostgreSQL,我们建议使用 Prisma Postgres。它完全支持边缘运行时,不需要专门的边缘兼容驱动程序。对于其他数据库,Prisma 加速 扩展了边缘兼容性,因此你可以从任何边缘函数提供商连接到任何数据库。
¥If your application uses PostgreSQL, we recommend using Prisma Postgres. It is fully supported on edge runtimes and does not require a specialized edge-compatible driver. For other databases, Prisma Accelerate extends edge compatibility so you can connect to any database from any edge function provider.
将数据库连接 URL 设置为环境变量
¥Setting your database connection URL as an environment variable
首先,确保在 Prisma 架构中将 DATABASE_URL
设置为 datasource
的 url
:
¥First, ensure that the DATABASE_URL
is set as the url
of the datasource
in your Prisma schema:
datasource db {
provider = "postgresql" // this might also be `mysql` or another value depending on your database
url = env("DATABASE_URL")
}
发展
¥Development
在开发时,你可以通过 DATABASE_URL
环境变量(例如 使用 .env
文件)配置数据库连接。
¥When in development, you can configure your database connection via the DATABASE_URL
environment variable (e.g. using .env
files).
生产
¥Production
将 Edge Function 部署到生产环境时,你需要使用 vercel
CLI 设置数据库连接:
¥When deploying your Edge Function to production, you'll need to set the database connection using the vercel
CLI:
npx vercel env add DATABASE_URL
此命令是交互式的,将要求你选择环境并在后续步骤中提供 DATABASE_URL
的值。
¥This command is interactive and will ask you to select environments and provide the value for the DATABASE_URL
in subsequent steps.
或者,你可以在 Vercel Dashboard 中配置项目的环境变量 通过用户界面。
¥Alternatively, you can configure the environment variable via the UI of your project in the Vercel Dashboard.
在 postinstall
钩子中生成 Prisma 客户端
¥Generate Prisma Client in postinstall
hook
在 package.json
中,你应该添加 "postinstall"
部分,如下所示:
¥In your package.json
, you should add a "postinstall"
section as follows:
{
// ...,
"postinstall": "prisma generate"
}
免费账户的大小限制
¥Size limits on free accounts
Vercel 有一个 免费账户的大小限制为 1 MB。如果你的 Prisma ORM 应用包超过该大小,我们建议升级到付费账户或使用 Prisma Accelerate 来部署你的应用。
¥Vercel has a size limit of 1 MB on free accounts. If your application bundle with Prisma ORM exceeds that size, we recommend upgrading to a paid account or using Prisma Accelerate to deploy your application.
特定于数据库的注意事项和示例
¥Database-specific considerations & examples
本部分提供有关使用 Prisma ORM 部署 Vercel Edge Functions 的数据库特定说明。
¥This section provides database-specific instructions for deploying a Vercel Edge Functions with Prisma ORM.
先决条件
¥Prerequisites
作为以下部分的先决条件,你需要在本地运行 Vercel Edge Function(通常以 Next.js API 路由的形式)并安装 Prisma 和 Vercel CLI。
¥As a prerequisite for the following section, you need to have a Vercel Edge Function (which typically comes in the form of a Next.js API route) running locally and the Prisma and Vercel CLIs installed.
如果你还没有,你可以运行这些命令从头开始设置 Next.js 应用(按照 Vercel 函数快速入门 的说明):
¥If you don't have that yet, you can run these commands to set up a Next.js app from scratch (following the instructions of the Vercel Functions Quickstart):
npm install -g vercel
npx create-next-app@latest
npm install prisma --save-dev && npm install @prisma/client
npx prisma init --output ../app/generated/prisma
我们将在下面的示例中使用默认的 User
模型:
¥We'll use the default User
model for the example below:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
Vercel Postgres
如果你使用 Vercel Postgres,则需要:
¥If you are using Vercel Postgres, you need to:
-
使用
@prisma/adapter-neon
数据库适配器(通过driverAdapters
预览功能),因为 Vercel Postgres 在底层使用 Neon¥use the
@prisma/adapter-neon
database adapter (via thedriverAdapters
Preview feature) because Vercel Postgres uses Neon under the hood -
请注意,Vercel 默认情况下调用数据库连接字符串
POSTGRES_PRISMA_URL
的环境变量,而 Prisma 文档中使用的默认名称通常为DATABASE_URL
;使用 Vercel 的命名,你需要在datasource
块上设置以下字段:¥be aware that Vercel by default calls the environment variable for the database connection string
POSTGRES_PRISMA_URL
while the default name used in the Prisma docs is typicallyDATABASE_URL
; using Vercel's naming, you need to set the following fields on yourdatasource
block:datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL") // uses connection pooling
directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection
}
1. 配置 Prisma 架构和数据库连接
¥ Configure Prisma schema & database connection
如果你没有要部署的项目,请按照 先决条件 中的说明引导包含 Prisma ORM 的基本 Next.js 应用。
¥If you don't have a project to deploy, follow the instructions in the Prerequisites to bootstrap a basic Next.js app with Prisma ORM in it.
首先,确保数据库连接配置正确。在 Prisma 架构中,将 datasource
块的 url
设置为 POSTGRES_PRISMA_URL
,将 directUrl
设置为 POSTGRES_URL_NON_POOLING
环境变量。你还需要启用 driverAdapters
功能标志:
¥First, ensure that the database connection is configured properly. In your Prisma schema, set the url
of the datasource
block to the POSTGRES_PRISMA_URL
and the directUrl
to the POSTGRES_URL_NON_POOLING
environment variable. You also need to enable the driverAdapters
feature flag:
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL") // uses connection pooling
directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection
}
接下来,你需要将 POSTGRES_PRISMA_URL
和 POSTGRES_URL_NON_POOLING
环境变量设置为数据库连接的值。
¥Next, you need to set the POSTGRES_PRISMA_URL
and POSTGRES_URL_NON_POOLING
environment variable to the values of your database connection.
如果你运行 npx prisma init
,则可以使用由此命令创建的 .env
文件来设置这些:
¥If you ran npx prisma init
, you can use the .env
file that was created by this command to set these:
POSTGRES_PRISMA_URL="postgres://user:password@host-pooler.region.postgres.vercel-storage.com:5432/name?pgbouncer=true&connect_timeout=15"
POSTGRES_URL_NON_POOLING="postgres://user:password@host.region.postgres.vercel-storage.com:5432/name"
2. 安装依赖
¥ Install dependencies
接下来,安装所需的软件包:
¥Next, install the required packages:
npm install @prisma/adapter-neon
3. 配置 postinstall
钩子
¥ Configure postinstall
hook
接下来,将新密钥添加到 package.json
中的 scripts
部分:
¥Next, add a new key to the scripts
section in your package.json
:
{
// ...
"scripts": {
// ...
"postinstall": "prisma generate"
}
}
4. 迁移你的数据库架构(如果适用)
¥ Migrate your database schema (if applicable)
如果你运行了上面的 npx prisma init
,则需要迁移数据库架构以创建 Prisma 架构中定义的 User
表(如果你的数据库中已拥有所需的所有表,则可以跳过此步骤):
¥If you ran npx prisma init
above, you need to migrate your database schema to create the User
table that's defined in your Prisma schema (if you already have all the tables you need in your database, you can skip this step):
npx prisma migrate dev --name init
5. 在 Vercel Edge Function 中使用 Prisma Client 将查询发送到数据库
¥ Use Prisma Client in your Vercel Edge Function to send a query to the database
如果你从头开始创建项目,则可以按如下方式创建新的边函数。
¥If you created the project from scratch, you can create a new edge function as follows.
首先,创建一个新的 API 路由,例如 通过使用这些命令:
¥First, create a new API route, e.g. by using these commands:
mkdir src/app/api
mkdir src/app/api/edge
touch src/app/api/edge/route.ts
下面是一个示例代码片段,你可以使用它来实例化 PrismaClient
并向刚刚创建的新 app/api/edge/route.ts
文件中的数据库发送查询:
¥Here is a sample code snippet that you can use to instantiate PrismaClient
and send a query to your database in the new app/api/edge/route.ts
file you just created:
import { NextResponse } from 'next/server'
import { PrismaClient } from '@prisma/client'
import { PrismaNeon } from '@prisma/adapter-neon'
export const runtime = 'edge'
export async function GET(request: Request) {
const adapter = new PrismaNeon({ connectionString: process.env.POSTGRES_PRISMA_URL })
const prisma = new PrismaClient({ adapter })
const users = await prisma.user.findMany()
return NextResponse.json(users, { status: 200 })
}
6. 本地运行边缘函数
¥ Run the Edge Function locally
使用以下命令运行应用:
¥Run the app with the following command:
npm run dev
你现在可以通过以下 URL 访问 Edge Function:http://localhost:3000/api/edge
。
¥You can now access the Edge Function via this URL: http://localhost:3000/api/edge
.
7. 设置 POSTGRES_PRISMA_URL
环境变量并部署 Edge Function
¥ Set the POSTGRES_PRISMA_URL
environment variable and deploy the Edge Function
运行以下命令以使用 Vercel 部署项目:
¥Run the following command to deploy your project with Vercel:
npx vercel deploy
请注意,在 Vercel 上创建项目后,你将需要设置 POSTGRES_PRISMA_URL
环境变量(如果这是你的第一次部署,则可能会失败)。你可以通过 Vercel UI 或运行以下命令来执行此操作:
¥Note that once the project was created on Vercel, you will need to set the POSTGRES_PRISMA_URL
environment variable (and if this was your first deploy, it likely failed). You can do this either via the Vercel UI or by running the following command:
npx vercel env add POSTGRES_PRISMA_URL
此时,你可以从 Vercel Dashboard 获取已部署应用的 URL,并通过 /api/edge
路由访问边缘功能。
¥At this point, you can get the URL of the deployed application from the Vercel Dashboard and access the edge function via the /api/edge
route.
PlanetScale
如果你使用的是 PlanetScale 数据库,你需要:
¥If you are using a PlanetScale database, you need to:
-
使用
@prisma/adapter-planetscale
数据库适配器(通过driverAdapters
预览功能)¥use the
@prisma/adapter-planetscale
database adapter (via thedriverAdapters
Preview feature)
1. 配置 Prisma 架构和数据库连接
¥ Configure Prisma schema & database connection
如果你没有要部署的项目,请按照 先决条件 中的说明引导包含 Prisma ORM 的基本 Next.js 应用。
¥If you don't have a project to deploy, follow the instructions in the Prerequisites to bootstrap a basic Next.js app with Prisma ORM in it.
首先,确保数据库连接配置正确。在 Prisma 架构中,将 datasource
块的 url
设置为 DATABASE_URL
环境变量。你还需要启用 driverAdapters
功能标志:
¥First, ensure that the database connection is configured properly. In your Prisma schema, set the url
of the datasource
block to the DATABASE_URL
environment variable. You also need to enable the driverAdapters
feature flag:
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma" // required for PlanetScale (as by default foreign keys are disabled)
}
接下来,你需要在 .env
文件中设置 DATABASE_URL
环境变量,Prisma 和 Next.js 都使用该变量来读取你的环境变量:
¥Next, you need to set the DATABASE_URL
environment variable in your .env
file that's used both by Prisma and Next.js to read your env vars:
DATABASE_URL="mysql://32qxa2r7hfl3102wrccj:password@us-east.connect.psdb.cloud/demo-cf-worker-ps?sslaccept=strict"
2. 安装依赖
¥ Install dependencies
接下来,安装所需的软件包:
¥Next, install the required packages:
npm install @prisma/adapter-planetscale
3. 配置 postinstall
钩子
¥ Configure postinstall
hook
接下来,将新密钥添加到 package.json
中的 scripts
部分:
¥Next, add a new key to the scripts
section in your package.json
:
{
// ...
"scripts": {
// ...
"postinstall": "prisma generate"
}
}
4. 迁移你的数据库架构(如果适用)
¥ Migrate your database schema (if applicable)
如果你运行了上面的 npx prisma init
,则需要迁移数据库架构以创建 Prisma 架构中定义的 User
表(如果你的数据库中已拥有所需的所有表,则可以跳过此步骤):
¥If you ran npx prisma init
above, you need to migrate your database schema to create the User
table that's defined in your Prisma schema (if you already have all the tables you need in your database, you can skip this step):
npx prisma db push
5. 在 Edge Function 中使用 Prisma Client 将查询发送到数据库
¥ Use Prisma Client in an Edge Function to send a query to the database
如果你从头开始创建项目,则可以按如下方式创建新的边函数。
¥If you created the project from scratch, you can create a new edge function as follows.
首先,创建一个新的 API 路由,例如 通过使用这些命令:
¥First, create a new API route, e.g. by using these commands:
mkdir src/app/api
mkdir src/app/api/edge
touch src/app/api/edge/route.ts
下面是一个示例代码片段,你可以使用它来实例化 PrismaClient
并向刚刚创建的新 app/api/edge/route.ts
文件中的数据库发送查询:
¥Here is a sample code snippet that you can use to instantiate PrismaClient
and send a query to your database in the new app/api/edge/route.ts
file you just created:
import { NextResponse } from 'next/server'
import { PrismaClient } from '@prisma/client'
import { PrismaPlanetScale } from '@prisma/adapter-planetscale'
export const runtime = 'edge'
export async function GET(request: Request) {
const adapter = new PrismaPlanetScale({ url: process.env.DATABASE_URL })
const prisma = new PrismaClient({ adapter })
const users = await prisma.user.findMany()
return NextResponse.json(users, { status: 200 })
}
6. 本地运行边缘函数
¥ Run the Edge Function locally
使用以下命令运行应用:
¥Run the app with the following command:
npm run dev
你现在可以通过以下 URL 访问 Edge Function:http://localhost:3000/api/edge
。
¥You can now access the Edge Function via this URL: http://localhost:3000/api/edge
.
7. 设置 DATABASE_URL
环境变量并部署 Edge Function
¥ Set the DATABASE_URL
environment variable and deploy the Edge Function
运行以下命令以使用 Vercel 部署项目:
¥Run the following command to deploy your project with Vercel:
npx vercel deploy
请注意,在 Vercel 上创建项目后,你将需要设置 DATABASE_URL
环境变量(如果这是你的第一次部署,则可能会失败)。你可以通过 Vercel UI 或运行以下命令来执行此操作:
¥Note that once the project was created on Vercel, you will need to set the DATABASE_URL
environment variable (and if this was your first deploy, it likely failed). You can do this either via the Vercel UI or by running the following command:
npx vercel env add DATABASE_URL
此时,你可以从 Vercel Dashboard 获取已部署应用的 URL,并通过 /api/edge
路由访问边缘功能。
¥At this point, you can get the URL of the deployed application from the Vercel Dashboard and access the edge function via the /api/edge
route.
Neon
如果你使用的是 Neon 数据库,你需要:
¥If you are using a Neon database, you need to:
-
使用
@prisma/adapter-neon
数据库适配器(通过driverAdapters
预览功能)¥use the
@prisma/adapter-neon
database adapter (via thedriverAdapters
Preview feature)
1. 配置 Prisma 架构和数据库连接
¥ Configure Prisma schema & database connection
如果你没有要部署的项目,请按照 先决条件 中的说明引导包含 Prisma ORM 的基本 Next.js 应用。
¥If you don't have a project to deploy, follow the instructions in the Prerequisites to bootstrap a basic Next.js app with Prisma ORM in it.
首先,确保数据库连接配置正确。在 Prisma 架构中,将 datasource
块的 url
设置为 DATABASE_URL
环境变量。你还需要启用 driverAdapters
功能标志:
¥First, ensure that the database connection is configured properly. In your Prisma schema, set the url
of the datasource
block to the DATABASE_URL
environment variable. You also need to enable the driverAdapters
feature flag:
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
接下来,你需要在 .env
文件中设置 DATABASE_URL
环境变量,Prisma 和 Next.js 都使用该变量来读取你的环境变量:
¥Next, you need to set the DATABASE_URL
environment variable in your .env
file that's used both by Prisma and Next.js to read your env vars:
DATABASE_URL="postgresql://janedoe:password@ep-nameless-pond-a23b1mdz.eu-central-1.aws.neon.tech/neondb?sslmode=require"
2. 安装依赖
¥ Install dependencies
接下来,安装所需的软件包:
¥Next, install the required packages:
npm install @prisma/adapter-neon
3. 配置 postinstall
钩子
¥ Configure postinstall
hook
接下来,将新密钥添加到 package.json
中的 scripts
部分:
¥Next, add a new key to the scripts
section in your package.json
:
{
// ...
"scripts": {
// ...
"postinstall": "prisma generate"
}
}
4. 迁移你的数据库架构(如果适用)
¥ Migrate your database schema (if applicable)
如果你运行了上面的 npx prisma init
,则需要迁移数据库架构以创建 Prisma 架构中定义的 User
表(如果你的数据库中已拥有所需的所有表,则可以跳过此步骤):
¥If you ran npx prisma init
above, you need to migrate your database schema to create the User
table that's defined in your Prisma schema (if you already have all the tables you need in your database, you can skip this step):
npx prisma migrate dev --name init
5. 在 Edge Function 中使用 Prisma Client 将查询发送到数据库
¥ Use Prisma Client in an Edge Function to send a query to the database
如果你从头开始创建项目,则可以按如下方式创建新的边函数。
¥If you created the project from scratch, you can create a new edge function as follows.
首先,创建一个新的 API 路由,例如 通过使用这些命令:
¥First, create a new API route, e.g. by using these commands:
mkdir src/app/api
mkdir src/app/api/edge
touch src/app/api/edge/route.ts
下面是一个示例代码片段,你可以使用它来实例化 PrismaClient
并向刚刚创建的新 app/api/edge/route.ts
文件中的数据库发送查询:
¥Here is a sample code snippet that you can use to instantiate PrismaClient
and send a query to your database in the new app/api/edge/route.ts
file you just created:
import { NextResponse } from 'next/server'
import { PrismaClient } from '@prisma/client'
import { PrismaNeon } from '@prisma/adapter-neon'
export const runtime = 'edge'
export async function GET(request: Request) {
const adapter = new PrismaNeon({ connectionString: process.env.DATABASE_URL })
const prisma = new PrismaClient({ adapter })
const users = await prisma.user.findMany()
return NextResponse.json(users, { status: 200 })
}
6. 本地运行边缘函数
¥ Run the Edge Function locally
使用以下命令运行应用:
¥Run the app with the following command:
npm run dev
你现在可以通过以下 URL 访问 Edge Function:http://localhost:3000/api/edge
。
¥You can now access the Edge Function via this URL: http://localhost:3000/api/edge
.
7. 设置 DATABASE_URL
环境变量并部署 Edge Function
¥ Set the DATABASE_URL
environment variable and deploy the Edge Function
运行以下命令以使用 Vercel 部署项目:
¥Run the following command to deploy your project with Vercel:
npx vercel deploy
请注意,在 Vercel 上创建项目后,你将需要设置 DATABASE_URL
环境变量(如果这是你的第一次部署,则可能会失败)。你可以通过 Vercel UI 或运行以下命令来执行此操作:
¥Note that once the project was created on Vercel, you will need to set the DATABASE_URL
environment variable (and if this was your first deploy, it likely failed). You can do this either via the Vercel UI or by running the following command:
npx vercel env add DATABASE_URL
此时,你可以从 Vercel Dashboard 获取已部署应用的 URL,并通过 /api/edge
路由访问边缘功能。
¥At this point, you can get the URL of the deployed application from the Vercel Dashboard and access the edge function via the /api/edge
route.