SUPABASE CORE · 113
Supabase Architecture:SDK、API、Auth、Postgres、RLS 到底在哪一層
Supabase 把 Auth、Data API、Storage、Realtime 與 PostgreSQL 整合成平台。要能 debug,你必須知道 Browser SDK、HTTP API、Postgres、RLS 各在哪裡執行、負責什麼。
Learning outcomes
- 能畫出 Browser → Supabase API → Postgres 的資料流。
- 能區分 client SDK 與 database engine。
- 能把 Auth、RLS、constraint 放到正確責任層。
- 能依 failure evidence 判斷是哪一層出問題。
1. 最小架構圖
Browser / Android App
↓ client SDK
HTTPS
↓
Supabase Platform APIs
├─ Auth
├─ Data API
├─ Storage
└─ Realtime
↓
PostgreSQL
├─ schema
├─ constraints
├─ indexes
└─ RLS policiesSDK 只是 client-side abstraction;真正資料庫規則仍在 Postgres。
2. Web SDK 通常在 Browser runtime
const supabase =
createClient(
projectUrl,
publicClientKey
);這段 JavaScript 是 Browser 執行。SDK 不會把 Browser 直接變成 PostgreSQL process。
3. Data API 是 HTTP boundary
client SDK建立 requestHTTPSplatform APIPostgres context
因此 Browser Network 能看到 request/response;policy/constraint evidence 則屬資料層。
4. 四個責任不要混
| 層 | 主要問題 |
|---|---|
| Auth | 目前 request 對應誰? |
| RLS | 這個 user 能碰哪些 rows? |
| Constraint | 這筆資料本身是否合法? |
| Frontend | 怎麼顯示與操作資料? |
5. Failure layer
| Evidence | 先查 |
|---|---|
| 登入失敗 | Auth / credential / session |
| HTTP/network error | Client ↔ platform API |
| 空資料或 denied | query + user context + RLS |
| unique/check violation | Database constraint |
| column missing | migration/version mismatch |
Project checkpoint:Course Workspace v1
Browser
↓ Supabase client
courses table
course:
id
owner_id
title
score
created_at下一課決定哪些 credentials 可以在 Browser,哪些必須留在 trusted server boundary。
2026 current model:API key、Auth、GRANT、RLS 是四層
現在的 Supabase 要特別避免把「能連到 project」和「能看到某些 rows」混成一件事。API key 先辨識 application component;Auth 建立 user identity;Postgres privilege / GRANT 決定 role 能不能碰 table;RLS 再決定這個 identity 能看到或修改哪些 rows。
Application key
↓
Auth identity
↓
Table privilege / Data API exposure
↓
RLS row policy
↓
Constraint / trigger / transaction
因此「table 存在」不代表 Data API 一定可查;「已登入」也不代表所有 rows 一定可見。Debug 時要沿這條 chain 找最早失敗層。
Knowledge check
- Supabase Web client 由誰執行?
- RLS 和 CHECK constraint 各解什麼問題?
- API 200 但 data=[],列四種 hypothesis。
- 畫出 Course Workspace Browser → Postgres 路徑。