使用 Prisma ORM 部署边缘功能
你可以将使用 Prisma ORM 的应用部署到边缘。根据你使用的边缘函数提供商和数据库,有不同的注意事项和需要注意的事项。
¥You can deploy an application that uses Prisma ORM to the edge. Depending on which edge function provider and which database you use, there are different considerations and things to be aware of.
以下是 Prisma ORM 当前支持的所有边缘函数提供程序的简要概述:
¥Here is a brief overview of all the edge function providers that are currently supported by Prisma ORM:
提供值 / 产品 | Prisma ORM 原生支持 | 支持 Prisma Postgres(和 Prisma Accelerate) |
---|---|---|
Vercel 边函数 | ✅ (预览版;仅限兼容的驱动程序) | ✅ |
Vercel 边缘中间件 | ✅ (预览版;仅限兼容的驱动程序) | ✅ |
Cloudflare Workers | ✅ (预览版;仅限兼容的驱动程序) | ✅ |
Cloudflare 页面 | ✅ (预览版;仅限兼容的驱动程序) | ✅ |
Deno 部署 | 还没有 | ✅ |
在 Cloudflare 和 Vercel 上部署使用 Prisma ORM 的边缘功能目前处于 预览 阶段。
¥Deploying edge functions that use Prisma ORM on Cloudflare and Vercel is currently in Preview.
数据库驱动程序的边缘兼容性
¥Edge-compatibility of database drivers
为什么边缘函数中的数据库驱动程序存在限制?
¥Why are there limitations around database drivers in edge functions?
边缘函数通常不使用标准 Node.js 运行时。例如,Vercel Edge Functions 和 Cloudflare Workers 在 V8 分离株 中运行代码。Deno Deploy 使用 Deno JavaScript 运行时。因此,这些边缘函数只能访问标准 Node.js API 的一小部分,并且计算资源(CPU 和内存)也受到限制。
¥Edge functions typically don't use the standard Node.js runtime. For example, Vercel Edge Functions and Cloudflare Workers are running code in V8 isolates. Deno Deploy is using the Deno JavaScript runtime. As a consequence, these edge functions only have access to a small subset of the standard Node.js APIs and also have constrained computing resources (CPU and memory).
特别是,无法自由打开 TCP 连接的限制使得从边缘功能与传统数据库进行通信变得困难。虽然 Cloudflare 引入了支持有限 TCP 连接的 connect()
API,但这仍然只能使用与该 API 兼容的特定数据库驱动程序来访问数据库。
¥In particular, the constraint of not being able to freely open TCP connections makes it difficult to talk to a traditional database from an edge function. While Cloudflare has introduced a connect()
API that enables limited TCP connections, this still only enables database access using specific database drivers that are compatible with that API.
我们建议使用 Prisma Postgres。它完全支持边缘运行时,不需要专门的边缘兼容驱动程序。对于其他数据库,Prisma 加速 扩展了边缘兼容性,因此你可以从任何边缘函数提供商连接到任何数据库。
¥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.
哪些数据库驱动程序是边缘兼容的?
¥Which database drivers are edge-compatible?
以下是不同数据库驱动程序及其与不同边缘功能产品的兼容性的概述:
¥Here is an overview of the different database drivers and their compatibility with different edge function offerings:
-
Neon 无服务器 使用 HTTP 访问数据库。它可与 Cloudflare Workers 和 Vercel Edge Functions 配合使用。
¥Neon Serverless uses HTTP to access the database. It works with Cloudflare Workers and Vercel Edge Functions.
-
PlanetScale 无服务器 使用 HTTP 访问数据库。它可与 Cloudflare Workers 和 Vercel Edge Functions 配合使用。
¥PlanetScale Serverless uses HTTP to access the database. It works with Cloudflare Workers and Vercel Edge Functions.
-
node-postgres
(pg
) 使用 Cloudflare 的connect()
(TCP) 访问数据库。它仅与 Cloudflare Workers 兼容,而不与 Vercel Edge Functions 兼容。¥
node-postgres
(pg
) uses Cloudflare'sconnect()
(TCP) to access the database. It is only compatible with Cloudflare Workers, not with Vercel Edge Functions. -
@libsql/client
用于访问 Turso 数据库。它可与 Cloudflare Workers 和 Vercel Edge Functions 配合使用。¥
@libsql/client
is used to access Turso databases. It works with Cloudflare Workers and Vercel Edge Functions. -
Cloudflare D1 用于访问 D1 数据库。它仅与 Cloudflare Workers 兼容,而不与 Vercel Edge Functions 兼容。
¥Cloudflare D1 is used to access D1 databases. It is only compatible with Cloudflare Workers, not with Vercel Edge Functions.
-
Prisma Postgres 用于访问使用 unikernels 在裸机上构建的 PostgreSQL 数据库。它支持 Cloudflare Workers 和 Vercel。
¥Prisma Postgres is used to access a PostgreSQL database built on bare-metal using unikernels. It is supported on both Cloudflare Workers and Vercel.
node-mysql2
驱动程序上有 还有正在进行的工作,将来也可以从 Cloudflare Workers 和 Pages 访问传统 MySQL 数据库。
¥There's also work being done on the node-mysql2
driver which will enable access to traditional MySQL databases from Cloudflare Workers and Pages in the future as well.
你可以使用相应的 驱动适配器 驱动程序将所有这些驱动程序与 Prisma ORM 结合使用。
¥You can use all of these drivers with Prisma ORM using the respective driver adapters.
根据你使用的部署提供商和数据库/驱动程序,可能需要特殊注意事项。请查看你各自场景的部署文档,以确保你可以成功部署你的应用:
¥Depending on which deployment provider and database/driver you use, there may be special considerations. Please take a look at the deployment docs for your respective scenario to make sure you can deploy your application successfully:
-
云耀
¥Cloudflare
-
Vercel
如果你想使用 Turso 部署应用,你可以按照说明 此处。
¥If you want to deploy an app using Turso, you can follow the instructions here.