使用 Supabase 部署
开源 Firebase 替代:Postgres、auth、存储、实时和边缘函数。
平台进阶
为什么
Supabase 提供完整 Postgres 数据库,自动生成 REST/GraphQL API、行级安全、auth、实时订阅和存储 — 全开源可自托管。平台即服务采用率 2024 年增长 24%,团队从虚拟机管理转向 git push 部署。
the cloud infrastructure market reached $330B in 2024, growing 22% year-over-year (Synergy Research),PaaS 层捕获了大多数新工作负载。
何时使用
Postgres 支持且需 auth、实时和存储的应用用 Supabase。非 Postgres 或需不同 auth 提供者时避免。
如何做
1. 创建 — supabase.com → 新建项目,获取 URL + anon key
2. Schema — SQL 编辑器或 supabase migration
3. Auth — 内置 email/OAuth,RLS 策略控制访问
4. 存储 — supabase.storage.from('bucket') 文件
5. 实时 — supabase.channel('table') 实时更新
6. 边缘函数 — supabase functions deploy
7. 客户端 — createClient(url, anonKey)
示例
import { createClient } from "@supabase/supabase-js";
const supabase = createClient(URL, ANON_KEY);
// 认证
const { data } = await supabase.auth.signInWithPassword({ email, password });
// RLS 查询
const { data: posts } = await supabase.from("posts").select("*").eq("author_id", user.id);
// 实时
supabase.channel("posts").on("INSERT", (payload) => console.log(payload)).subscribe();
// 边缘函数 — supabase/functions/hello/index.ts
Deno.serve(async (req) => new Response("hello"));
常见问题
- ⚠RLS 未启用 — 所有数据公开,每表启用行级安全
- ⚠Auth 回调 URL 不匹配 — 面板设 Site URL 用于 OAuth 重定向
- ⚠环境间配置漂移 — 使用基础设施即代码(Terraform/Pulumi)保持 dev/staging/prod 一致
最佳实践
- ✓每表启用 RLS 并写策略 — 数据库级安全,非应用级
- ✓schema 变更用 supabase migration — 版本控制可复现
- ✓设置健康检查和优雅关闭 — 处理 SIGTERM 以在退出前排空进行中的请求