supabase架构和源码的概要
supabase
后端即服务 (BaaS) 是一种云服务,开发人员在使用BaaS进行Web或移动应用开发时,只需编写和维护前端代码。BaaS提供商为开发者提供了开发应用所需要的后端服务,例如用户身份验证、数据库管理、推送通知(针对移动应用程序),以及云存储和托管等。BaaS使开发人员能够专注于前端应用程序代码的编写。通过 BaaS 供应商提供的 API和 SDK,他们可以集成所需的所有后端功能,而无需自行构建后端。他们也不必管理服务器、虚拟机或容器,即可保持应用程序运行。因此,开发者可以更快地构建和启动移动应用程序和 Web 应用程序。
Supabase 和 Firebase 都是BaaS领域的知名品牌,可以为开发者提供后端功能服务,以帮助其更快地构建产品。其中,Supabase 是一个开源的 Firebase 替代品,而 Firebase 是 Google 提供的云服务。Supabase 和 Firebase两者之间存在竞争的关系。
Supabase凭借其开源特性与全栈能力,已成为开发者构建现代应用的热门选择。作为Firebase的替代方案,Supabase以PostgreSQL为核心,整合了实时数据库、身份认证、文件存储等关键功能,为开发者提供了兼具灵活性与高性能的后端解决方案。
1概要
1.1 Supabase与Firebase的对比分析
https://gitcode.com/GitHub_Trending/supa/supabase?source_module=search_result_repo
https://github.com/supabase/supabase
1.2 Supabase基本架构
Supabase围绕PostgreSQL整合了一系列开源工具,以实现BaaS所需的用户认证、实时数据库、对象存储和REST API接口等功能。在整合这些工具的同时,为开发者封装了统一的SDK,方便开发者以统一的方式调用这些能力。官方提供了JavaScript和Flutter的SDK,社区贡献了Python、C#、Swift和Kotlin的SDK,开发者在开发移动端和Web应用时,可以方便地调用Supabase提供的后端功能。
下面是Supabase整体架构图:

- 应用开发层
Supabase SDK
如上所述,SupaBase官方及社区贡献了目前主流的移动端和Web端的SDK,帮助开发者更容易的开发应用。Supabase SDK由众多子项目构成,通常以supabase-*命名,例如supabase-js和supabase-dart。
Supabase Studio
Studio是Supabase的管理界面,即Supabase的官方网站和项目管理控制台,这些Web及后端服务均由Studio工具实现。studio的代码在supabase的主仓库里。
Supabase CLI
Supabase的命令行管理工具,可方便地在命令行上管理项目。
- Supabase四个核心组件
postgres是一个历史比较悠久的开源组件,能够将postgresql以REST接口暴露给开发者,开发者可以直接通过HTTP接口操作数据库。
storage api
提供对象存储功能,解决开发者需要上传、下载文件的需求,比如图片、文档等。Storage API将数据存储在S3存储服务上。
用户登录认证模块可帮助用户简化其应用的用户管理流程,允许开发者使用邮箱和手机号进行注册和登录模块的开发,同时支持通过OAuth协议接入GitHub、Google和Apple等平台的账号。
realtime
Realtime利用PostgreSQL的listen/notify机制,实现了Postgres数据库的实时数据变更通知功能。
在此基础上,Supabase还提供了如下一些能力
云函数(edge function)
postgrest本身提供了调用postgresql function的能力,开发者可以在pg中用sql或者JavaScript编写函数,然后通过rpc接口调用pg的函数。pg的函数能在一定程度上解决部分业务场景的需求,但是仍旧存在一些不方便或者不直观的地方。
云函数(edge function)可以更灵活的适配一些场景,比如支付等需要跟第三方系统进行对接的场景,使用云函数可以让开发者编写后端代码并运行在服务端。
graphql是通过postgresql插件实现的,开发者在postgres建表之后,可以直接通过graphql进行数据库操作。
Web hook为开发者提供了事件触发能力,开发者可以设定满足某个条件时,自动触发外部接口的调用,从而跟第三方系统进行集成。比如跟飞书机器人集成。
- 其他组件
PostgreSQL
一切的核心。SupaBase整个服务是围绕PostgreSQL构建的。
kong
API网关。负责将API请求路由到目标服务组件,并转发结果给用户。
postgres-meta
PostgreSQL的元数据管理组件,主要为studio提供服务,可以用来查询数据库中的表、角色,执行SQL等。
1.3 Supabase技术栈
由于SupaBase组合非常多的开源组件,因此其后端服务(不包括SDK)的整体技术栈也相对复杂。使用到的开发语言和框架有:
Javascript/Typescript
Tailwind
Go
Elixir
Haskell
React
Nextjs
Phoenix
fastify
knex
chi
rust
。。。
1.4 Supabase SDK介绍
Supabase的SDK也是通过组合多个客户端库实现的,以JavaScript SDK为例:

如上图所示,supabase-js实际上是粘合了多个不同的组件的sdk客户端,这样做的好处:
开发者不需要关注某个具体的组件如何工作,全部统一使用supabase-js的接口访问后端服务。
supabase-js帮助开发者完成了各个组件间用户认证鉴权的统一处理。
- Postgres 是一个超过 30 年活跃开发的面向对象关系数据库系统,因其可靠性、功能健壮性和性能而获得了良好的声誉。
- Realtime 是一个 Elixir 服务器,允许您通过 WebSocket 监听 PostgreSQL 的插入、更新和删除操作。Realtime 利用 Postgres 内置的复制功能来获取数据库变化,将变化转换为 JSON,然后通过 WebSocket 广播给授权的客户端。
- PostgREST 是一个 Web 服务器,能够直接将您的 PostgreSQL 数据库转换为 RESTful API。
- GoTrue 是一个基于 JWT 的认证 API,简化了应用程序中的用户注册、登录和会话管理。
- Storage 是一个用于管理 S3 中文件的 RESTful API,权限由 Postgres 管理。
- pg_graphql 是一个 PostgreSQL 扩展,提供了一个 GraphQL API。
- postgres-meta 是一个用于管理您的 Postgres 的 RESTful API,允许您获取表、添加角色和运行查询等。
- Kong 是一个云原生 API 网关。
2 源码分析
2.1 运行
Self-Hosting with Docker | Supabase Docs
- # Get the code using git sparse checkout
- git clone --filter=blob:none --no-checkout https://github.com/supabase/supabase
- cd supabase
- git sparse-checkout set --cone docker && git checkout master
- cd ..
- # Make your new supabase project directory
- mkdir supabase-project
- # Tree should look like this
- # .
- # ├── supabase
- # └── supabase-project
- # Copy the compose files over to your project
- cp -rf supabase/docker/* supabase-project
- # Copy the fake env vars
- cp supabase/docker/.env.example supabase-project/.env
- # Switch to your project directory
- cd supabase-project
- # Pull the latest images
- docker compose pull
- # Start the services (in detached mode)
- docker compose up -d
2.2 代码运行
supabase/DEVELOPERS.md at master · supabase/supabase
Install dependencies
- Install the dependencies in the root of the repo:
pnpm install # install dependencies
- Copy the example .env.local.example to .env.local
cp apps/www/.env.local.example apps/www/.env.local
- After that you can run the apps simultaneously with the following:
pnpm dev # start all the applications
Then visit, and edit, any of the following sites:
Running sites individually
You can run any of the sites individually by using the scope name. For example:
pnpm dev:www
Note: Particularly for www make sure you have copied apps/www/.env.local.example to apps/www/.env.local
Shared components
The monorepo has a set of shared components under /packages:
- /packages/ai-commands: Helpers/Commands for AI related functions
- /packages/common: Common React components, shared between all sites
- /packages/config: All shared config
- /packages/shared-data: Shared data that can be used across all apps
- /packages/tsconfig: Shared Typescript settings
- /packages/ui: Common UI components
2.3 主要资料
https://supabase.com/docs/guides/database/
https://supabase.com/docs/guides/auth
https://supabase.com/docs/guides/storage
https://supabase.com/docs/guides/functions
https://supabase.com/docs/guides/realtime
https://supabase.com/docs/guides/ai
https://supabase.com/docs/guides/cron
https://supabase.com/docs/guides/queues
Supabase的架构模块和实现语言
Architecture | Supabase Docs https://supabase.com/docs/guides/getting-started/architecture
Postgres (database)#
Postgres is the core of Supabase. We do not abstract the Postgres database—you can access it and use it with full privileges. We provide tools which make Postgres as easy to use as Firebase.
- Official Docs: postgresql.org/docs
- Source code: github.com/postgres/postgres (mirror)
- License: PostgreSQL License- Language: C
Studio (dashboard)#
An open source Dashboard for managing your database and services.
- Official Docs: Supabase docs
- Source code: github.com/supabase/supabase
- License: Apache 2
- Language: TypeScript
GoTrue (Auth)#
A JWT-based API for managing users and issuing access tokens. This integrates with PostgreSQL's Row Level Security and the API servers.
- Official Docs: Supabase Auth reference docs
- Source code: github.com/supabase/gotrue
- License: MIT
- Language: Go
PostgREST (API)#
A standalone web server that turns your Postgres database directly into a RESTful API.
We use this with our pg_graphql extension to provide a GraphQL API.
- Official Docs: postgrest.org
- Source code: github.com/PostgREST/postgrest
- License: MIT
- Language: Haskell
Realtime (API & multiplayer)#
A scalable WebSocket engine for managing user Presence, broadcasting messages, and streaming database changes.
- Official Docs: Supabase Realtime docs
- Source code: github.com/supabase/realtime
- License: Apache 2
- Language: Elixir
Storage API (large file storage)#
An S3-compatible object storage service that stores metadata in Postgres.
- Official Docs: Supabase Storage reference docs
- Source code: github.com/supabase/storage-api
- License: Apache 2.0
- Language: Node.js / TypeScript
Deno (Edge Functions)#
A modern runtime for JavaScript and TypeScript.
- Official Docs: Deno documentation
- Source code: Deno source code
- License: MIT
- Language: TypeScript / Rust
postgres-meta (database management)#
A RESTful API for managing your Postgres. Fetch tables, add roles, and run queries.
- Official Docs: supabase.github.io/postgres-meta
- Source code: github.com/supabase/postgres-meta
- License: Apache 2.0
- Language: Node.js / TypeScript
Supavisor#
A cloud-native, multi-tenant Postgres connection pooler.
- Official Docs: Supavisor GitHub Pages
- Source code: supabase/supavisor
- License: Apache 2.0
- Language: Elixir
Kong (API gateway)#
A cloud-native API gateway, built on top of NGINX.
- Official Docs: docs.konghq.com
- Source code: github.com/kong/kong
- License: Apache 2.0
- Language: Lua
2.4 PostgreSQL
https://github.com/PostgREST/postgrest
PostgREST Documentation — PostgREST 14 documentation
Supabase provides a RESTful API using PostgREST. This is a very thin API layer on top of Postgres.
It exposes everything you need from a CRUD API at the URL https://<project_ref>.supabase.co/rest/v1/.
- GraphQL
Postgres 自动生成的 GraphQL API。
Supabase GraphQL API 使用pg_graphql.它支持:
- 基本 CRUD作(创建/读取/更新/删除)
- 支持表、视图、物化视图和外部表
- 表/视图之间的任意深度关系
- 用户定义的计算字段
- Postgres 的安全模型 - 包括行级安全性、角色和授权
所有请求都在一次往返中解决,从而实现快速响应时间和高吞吐量。
supabase/pg_graphql: GraphQL support for PostgreSQL https://github.com/supabase/pg_graphql
2.5 Studio (dashboard)
https://github.com/supabase/supabase/tree/master/apps/studio
这是个前端项目,使用 Next.js Tailwind 实现
https://github.com/supabase/supabase 这个仓库使用pnpm管理了一个monorepo的代码仓【 Turborepo 管理】
2.6 supabase/auth源码分析
There are four major layers to Supabase Auth:
- Client layer. This can be one of the Supabase client SDKs, or manually made HTTP requests using the HTTP client of your choice.
- Kong API gateway. This is shared between all Supabase products.
- Auth service (formerly known as GoTrue).
- Postgres database. This is shared between all Supabase products.

Client layer#
The client layer runs in your app. This could be running in many places, including:
- Your frontend browser code
- Your backend server code
- Your native application
The client layer provides the functions that you use to sign in and manage users. We recommend using the Supabase client SDKs, which handle:
- Configuration and authentication of HTTP calls to the Supabase Auth backend
- Persistence, refresh, and removal of Auth Tokens in your app's storage medium
- Integration with other Supabase products
But at its core, this layer manages the making of HTTP calls, so you could write your own client layer if you wanted to.
See the Client SDKs for more information:
Auth service#
The Auth service is an Auth API server written and maintained by Supabase. It is a fork of the GoTrue project, originally created by Netlify.
supabase/auth源码分析 | go空间 Next-Blogger
supabase/auth采用golang实现
2.6 Realtime
Elixir /erlang语言实现
发送临时消息,跟踪和同步共享状态,并在 WebSockets 上监听 Postgres 更改。
目前的代码库状态
2.7 Supabase Storage Engine
supabase/storage: S3 compatible object storage service that stores metadata in Postgres

2.8 Edge Functions
基于deno的javascript运行时
Edge Functions | Supabase Docs
Edge Functions are server-side TypeScript functions, distributed globally at the edge—close to your users. They can be used for listening to webhooks or integrating your Supabase project with third-parties like Stripe. Edge Functions are developed using Deno, which offers a few benefits to you as a developer:
- It is open source.
- It is portable. Supabase Edge Functions run locally, and on any other Deno-compatible platform (including self-hosted infrastructure).
- It is TypeScript first and supports WASM.
- Edge Functions are globally distributed for low-latency.
函数的工作过程
- Request enters an edge gateway (relay) — the gateway routes traffic, handles auth headers/JWT validation, and applies routing/traffic rules.
- Auth & policies are applied — the gateway (or your function) can validate Supabase JWTs, apply rate-limits, and centralize security checks before executing code.
- Edge runtime executes your function — the function runs on a regionally-distributed Edge Runtime node closest to the user for minimal latency.
- Integrations & data access — functions commonly call Supabase APIs (Auth, Postgres, Storage) or third-party APIs. For Postgres, prefer connection strategies suited for edge/serverless environments (see the connect-to-postgres guide).
- Observability and logs — invocations emit logs and metrics you can explore in the dashboard or downstream monitoring (Sentry, etc.).
- Response returns via the gateway — the gateway forwards the response back to the client and records request metadata.
2.9 postgres-meta
管理pg数据库的rest API
A RESTful API for managing your Postgres. Fetch tables, add roles, and run queries (and more).
typescript语言实现
2.10 supavisor
supabase/supavisor: A cloud-native, multi-tenant Postgres connection pooler.
Elixir /erlang语言实现
pg数据库多租户的云原生连接池管理

2.11 Kong
这个就是比较常见的了API网关了,在nginx基础上构建
3 小结
从以上可见,作为BAAS的Supabase是在pg数据库基础上,深度绑定pg数据库,通过集成了大量的开源项目来实现的
【
低代码 - Oracle APEX 这个思路的商业产品oralce apex和这个有很大的类似性。如其官网的说明“Oracle APEX 可以帮助开发人员构建出色的应用并解决实际问题。您无需精通各种各样的 Web 技术。您只需关注要解决的问题,其他繁重的工作都可以交给 Oracle APEX 来为您处理。”
】