无服务器驱动程序
Choose a driver for connecting to your Postgres database from a serverless environment.
了解如何从无服务器环境连接到你的 Postgres 数据库。你使用的驱动取决于你的代码运行在哪个运行时环境。
🌐 Learn how to connect to your Postgres database from a serverless environment. The driver you use depends on which runtime your code runs in.
supabase-js 是一个同构的 JavaScript 客户端,它使用 自动生成的 REST API,因此可以在任何支持 HTTPS 连接的环境中工作。这个 API 内置了 连接池,可以处理成千上万的同时请求,非常适合无服务器工作负载。
如果你改用 Postgres 客户端连接,使用 事务模式下的共享池,然后 配置你的客户端 设置连接池大小、预处理语句和 SSL。这三个设置就是大多数无服务器连接问题的根源。
🌐 If you connect with a Postgres client instead, use the shared pooler in transaction mode, then configure your client for pool size, prepared statements, and SSL. Those three settings are what most serverless connection problems come down to.
Vercel Edge 函数 #
🌐 Vercel Edge Functions
Vercel 的 Edge 运行时 运行在 V8 引擎 上,并提供一组有限的 Web 标准 API。
🌐 Vercel's Edge runtime runs on the V8 engine and exposes a limited set of Web Standard APIs.
从部署模板开始,或者自己配置连接。
🌐 Start from a deploy template, or configure the connection yourself.
快速入门 #
🌐 Quickstart
选择其中一个 Vercel 部署模板。它们使用 Vercel 部署集成 将你的连接字符串配置为 Vercel 项目上的环境变量。
🌐 Choose one of these Vercel Deploy Templates. They use the Vercel Deploy Integration to configure your connection strings as environment variables on your Vercel project.
手动配置 #
🌐 Manual configuration
- 在 Supabase 控制面板中,点击 Connect 并从 事务池 部分复制 URI。
- 把密码占位符替换成你的数据库密码。
- 在 URI 后添加后缀
?workaround=supabase-pooler.vercel。 - 把结果保存为
POSTGRES_URL环境变量。
POSTGRES_URL="postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@[POOLER-HOST]:6543/postgres?workaround=supabase-pooler.vercel"import { sql } from '@vercel/postgres'import { InferInsertModel, InferSelectModel } from 'drizzle-orm'import { pgTable, serial, text, timestamp, uniqueIndex } from 'drizzle-orm/pg-core'import { drizzle } from 'drizzle-orm/vercel-postgres'export const UsersTable = pgTable( 'users', { id: serial('id').primaryKey(), name: text('name').notNull(), email: text('email').notNull(), image: text('image').notNull(), createdAt: timestamp('createdAt').defaultNow().notNull(), }, (users) => { return { uniqueIdx: uniqueIndex('unique_idx').on(users.email), } })export type User = InferSelectModel<typeof UsersTable>export type NewUser = InferInsertModel<typeof UsersTable>// Connect to Vercel Postgresexport const db = drizzle(sql)Cloudflare Workers#
Cloudflare 的 Workers 运行时也使用 V8 引擎,但它为部分 Node.js API 和 TCP 套接字 API 提供了填充。这给了你三种选择:
🌐 Cloudflare's Workers runtime also uses the V8 engine, but it provides polyfills for a subset of Node.js APIs and the TCP Sockets API. That gives you three options:
驱动程序 #
🌐 Drivers [#cloudflare-workers-drivers]
Supabase 边缘函数 #
🌐 Supabase Edge Functions
Supabase Edge Functions 使用 Deno 运行时,它原生支持 TCP 连接。你可以选择以下任何一个客户端:
🌐 Supabase Edge Functions use the Deno runtime, which has native support for TCP connections. You can choose any of these clients:
驾驶员 #
🌐 Drivers [#supabase-edge-functions-drivers]