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:
-
使用 Prisma 模式语言 为你的数据库建模
¥Model your database with the Prisma Schema Language
-
在你的架构中使用 Prisma ORM 现有的
postgresql
数据库连接器 以及 Supabase 为你提供的连接字符串¥Use Prisma ORM's existing
postgresql
database connector in your schema, along with the connection string Supabase provides you -
如果你在 Supabase 中已有数据库架构,请将 内省 用于现有项目
¥Use Introspection for existing projects if you already have a database schema in Supabase
-
使用
db push
将你架构中的更改推送到 Supabase¥Use
db push
to push changes in your schema to Supabase -
在你的应用中使用 Prisma 客户端 与 Supabase 的数据库服务器通信
¥Use Prisma Client in your application to talk to the database server at Supabase
具体考虑因素
¥Specific considerations
如果你想使用 Supabase 提供的 连接池功能,则需要使用通过 Supabase 数据库设置 提供的连接池连接字符串,并将 ?pgbouncer=true
附加到 DATABASE_URL
环境变量的末尾:
¥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 your DATABASE_URL
environment variable:
# 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:
-
直接数据库连接字符串 –
postgresql://postgres:password@db.[your-project-ref].supabase.co:5432/postgres
¥Direct Database Connection string –
postgresql://postgres:password@db.[your-project-ref].supabase.co:5432/postgres
-
事务池连接字符串 -
postgresql://postgres.[your-project-ref]:password@aws-0-[region].pooler.supabase.com:6543/postgres
¥Transaction Pooler Connection string –
postgresql://postgres.[your-project-ref]:password@aws-0-[region].pooler.supabase.com:6543/postgres
-
会话池连接字符串 -
postgresql://postgres.[your-project-ref]:password@aws-0-[region].pooler.supabase.com:5432/postgres
¥Session Pooler Connection string –
postgresql://postgres.[your-project-ref]:password@aws-0-[region].pooler.supabase.com:5432/postgres
如果你想使用 Prisma CLI 对数据库执行其他操作(例如迁移),则你的设置取决于所使用的连接字符串:
¥If you would like to use the Prisma CLI in order to perform other actions on your database (e.g. migrations), your setup depends on the connection string used:
-
如果你的
DATABASE_URL
使用事务池(连接字符串 2),则必须为DIRECT_URL
提供直接数据库 (1) 或会话池 (3) 连接字符串,以便 Prisma Migrate 正常工作。¥If your
DATABASE_URL
uses the Transaction Pooler (connection string 2), you must provide aDIRECT_URL
with either the Direct Database (1) or Session Pooler (3) connection string in order for Prisma Migrate to work. -
如果你的
DATABASE_URL
已经使用会话池(连接字符串 3),则无需提供DIRECT_URL
。Prisma Migrate 无需它即可运行。¥If your
DATABASE_URL
already uses the Session Pooler (connection string 3), you do not need to provide aDIRECT_URL
. Prisma Migrate will work without it.
# 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 for migrations.
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"
然后,你可以更新 schema.prisma
以使用新的直接 URL:
¥You can then update your schema.prisma
to use the new direct URL:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}
有关 directUrl
字段的更多信息可以找到 此处。
¥More information about the directUrl
field can be found here.
除了 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.