Skip to main content

Supabase

本指南讨论使用 Prisma ORM 和 Supabase 背后的概念,解释 Supabase 与其他数据库提供商之间的共性和差异,并引导你完成配置应用以与 Supabase 集成的过程。

¥This guide discusses the concepts behind using Prisma ORM and Supabase, explains the commonalities and differences between Supabase and other database providers, and leads you through the process for configuring your application to integrate with Supabase.

什么是 Supabase?

¥What is Supabase?

Supabase 是 PostgreSQL 托管服务和开源 Firebase 替代方案,提供构建产品所需的所有后端功能。与 Firebase 不同,Supabase 由 PostgreSQL 支持,可以使用 Prisma ORM 直接访问。

¥Supabase is a PostgreSQL hosting service and open source Firebase alternative providing all the backend features you need to build a product. Unlike Firebase, Supabase is backed by PostgreSQL which can be accessed directly using Prisma ORM.

要了解有关 Supabase 的更多信息,你可以查看他们的架构 此处 和功能 此处

¥To learn more about Supabase, you can check out their architecture here and features here

与其他数据库提供商的共同点

¥Commonalities with other database providers

将 Prisma ORM 与 Supabase 结合使用的许多方面就像将 Prisma ORM 与任何其他关系数据库结合使用一样。你仍然可以:

¥Many aspects of using Prisma ORM with Supabase are just like using Prisma ORM with any other relational database. You can still:

具体考虑因素

¥Specific considerations

如果你想使用 Supabase 中提供的 连接池功能,则需要使用通过 Supabase 数据库设置 提供的连接池连接字符串,并在 Prisma Client 通过驱动程序适配器实例化时读取的环境变量末尾附加 ?pgbouncer=true

¥If you'd like to use the connection pooling feature available with Supabase, you will need to use the connection pooling connection string available via your Supabase database settings with ?pgbouncer=true appended to the end of the environment variable that Prisma Client reads when you instantiate it with a driver adapter:

.env
# Connect to Supabase via connection pooling with Supavisor.
DATABASE_URL="postgres://postgres.[your-supabase-project]:[password]@aws-0-[aws-region].pooler.supabase.com:6543/postgres?pgbouncer=true"

Supabase 为每个数据库提供三种类型的连接字符串:

¥Supabase provides three types of connection strings for each database:

  1. 直接数据库连接字符串 – postgresql://postgres:password@db.[your-project-ref].supabase.co:5432/postgres

    ¥Direct Database Connection stringpostgresql://postgres:password@db.[your-project-ref].supabase.co:5432/postgres

  2. 事务池连接字符串 - postgresql://postgres.[your-project-ref]:password@aws-0-[region].pooler.supabase.com:6543/postgres

    ¥Transaction Pooler Connection stringpostgresql://postgres.[your-project-ref]:password@aws-0-[region].pooler.supabase.com:6543/postgres

  3. 会话池连接字符串 - postgresql://postgres.[your-project-ref]:password@aws-0-[region].pooler.supabase.com:5432/postgres

    ¥Session Pooler Connection stringpostgresql://postgres.[your-project-ref]:password@aws-0-[region].pooler.supabase.com:5432/postgres

Prisma CLI 命令(例如迁移和自省)现在从 prisma.config.ts 读取直接的非连接池连接字符串。配置两个环境变量 - Prisma 客户端的连接池字符串 (DATABASE_URL) 和 Prisma CLI 的直接连接字符串 (DIRECT_URL):

¥Prisma CLI commands (for example, migrations and introspection) now read the direct, non-pooled connection string from prisma.config.ts. Configure two environment variables — the pooled connection string for Prisma Client (DATABASE_URL) and a direct connection string for the Prisma CLI (DIRECT_URL):

.env
# Connect to Supabase via connection pooling with Supavisor.
DATABASE_URL="postgres://postgres.[your-supabase-project]:[password]@aws-0-[aws-region].pooler.supabase.com:6543/postgres?pgbouncer=true"

# Direct connection to the database used by the Prisma CLI.
DIRECT_URL="postgres://postgres.[your-supabase-project]:[password]@aws-0-[aws-region].pooler.supabase.com:5432/postgres"
# or
DIRECT_URL="postgresql://postgres:password@db.[your-project-ref].supabase.co:5432/postgres"

prisma.config.ts 指向直接连接字符串:

¥Point prisma.config.ts to the direct connection string:

prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config'

export default defineConfig({
schema: 'prisma/schema.prisma',
datasource: {
url: env('DIRECT_URL'),
},
})

运行时,使用连接池中的 DATABASE_URL,通过驱动程序适配器实例化 Prisma Client。它能将直接连接字符串的作用域限定在 Prisma CLI 工作流中,同时应用连接仍然通过 Supavisor 进行。

¥At runtime, instantiate Prisma Client with a driver adapter using the pooled DATABASE_URL. This keeps the direct connection string scoped to Prisma CLI workflows while your application connections continue to flow through Supavisor.

src/db/client.ts
import { PrismaClient } from '../prisma/generated/client'
import { PrismaPg } from '@prisma/adapter-pg'

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
export const prisma = new PrismaClient({ adapter })
信息

除了 DIRECT_URL 之外,我们强烈建议使用 Supavisor 的连接池。你将获得 Prisma CLI 的出色开发者体验,同时还允许池化连接,无论你的部署策略如何。虽然这对于每个应用来说并不是绝对必要的,但无服务器解决方案将不可避免地需要连接池。

¥We strongly recommend using connection pooling with Supavisor in addition to DIRECT_URL. You will gain the great developer experience of the Prisma CLI while also allowing for connections to be pooled regardless of your deployment strategy. While this is not strictly necessary for every app, serverless solutions will inevitably require connection pooling.

Supabase 入门

¥Getting started with Supabase

如果你有兴趣了解更多信息,Supabase 有一个很好的指南,用于将 Supabase 提供的数据库连接到可用的 Prisma 项目 此处

¥If you're interested in learning more, Supabase has a great guide for connecting a database provided by Supabase to your Prisma project available here.

如果你在与 Supabase 集成时遇到问题,请查看这些 具体的故障排除提示Prisma 的 GitHub 讨论 以获得更多帮助。

¥If you're running into issues integrating with Supabase, check out these specific troubleshooting tips or Prisma's GitHub Discussions for more help.