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"
如果你想使用 Prisma CLI 对数据库执行其他操作(例如迁移),你将需要添加 DIRECT_URL
环境变量以在 datasource.directUrl
属性中使用,以便 CLI 可以绕过 Supavisor:
¥If you would like to use the Prisma CLI in order to perform other actions on your database (e.g. migrations) you will need to add a DIRECT_URL
environment variable to use in the datasource.directUrl
property so that the CLI can bypass Supavisor:
# 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"
然后,你可以更新 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.