AI 原生 BaaS 平台 Supabase 版
API 类型 | 使用者 | 作用 | 方式及示例 |
API 类型 | 使用者 | 作用 | 方式及示例 |
Admin API(HTTP 接口) | 应用(APP)开发者 | 执行初始化 DDL、DML 等 |
# 执行DDL curl -X POST $SUPABASE_BASE_URL/postgres/query \ -H "apikey: ${SERVICE_ROLE_KEY}" \ -H "Authorization: Bearer ${SERVICE_ROLE_KEY}" \ -H "Content-Type: application/json" \ -d '{"query": "CREATE TABLE foo(id serial primary key, name text);"}'
# 执行DML curl -X POST $SUPABASE_BASE_URL/postgres/query \ -H "apikey: ${SERVICE_ROLE_KEY}" \ -H "Authorization: Bearer ${SERVICE_ROLE_KEY}" \ -H "Content-Type: application/json" \ -d '{"query": "insert into table foo values(1, 'aaa');"}' |
Admin API(HTTP 接口) | 应用(APP)开发者 | 查询及修改 Data API 配置 (对应 Supabase 控制台的位置:Project Settings > Data API) |
# 查询 Data API 配置 curl -X GET $SUPABASE_BASE_URL/rest/v1/config \ -H "apikey: ${SERVICE_ROLE_KEY}" \ -H "Authorization: Bearer ${SERVICE_ROLE_KEY}" \ -H "Content-Type: application/json"
# 修改 Data API 配置 curl -X PATCH $SUPABASE_BASE_URL/rest/v1/config \ -H "apikey: ${SERVICE_ROLE_KEY}" \ -H "Authorization: Bearer ${SERVICE_ROLE_KEY}" \ -H "Content-Type: application/json" \ -d '{ "db_schema": "public,graphql_public,vault", "db_extra_search_path": "extensions,graphql_public,vault", "max_rows": 200 }' |
Client API(Supabase Client SDK) | 应用(APP)使用者 | 在 APP 中访问 Database | import { createClient } from '@supabase/supabase-js' const supabase = createClient( "<SUPABASE_BASE_URL>", "<SUPABASE_ANON_KEY>") async function find() { const { data, error } = await supabase .from('students') .select('*') .eq('name', 'tom') .gt('age', 18) if (error) { console.error('failed: ', err.message) } else { console.log('success: ', data) } } |
Client API(HTTP 接口) | 应用(APP)使用者 | 在 APP 中访问 Database | # curl查询表数据的示例 curl -X GET $SUPABASE_BASE_URL/rest/v1/users?id=eq.xxx \ -H "apikey: ${ANON_KEY}" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ -H "Content-Type: application/json" -H "Prefer: return-representation" // js修改表数据的示例 const updateUser = async (userId, updates): => const response = await fetch(`${SUPABASE_BASE_URL}/rest/v1/users?id=eq.${userId}`, { method: 'PATCH', headers: { 'apikey': '${ANON_KEY}', 'Authorization': 'Bearer ${ACCESS_TOKEN}', 'Content-Type': 'application/json', 'Prefer': 'return=representation' }, body: JSON.stringify(updates) }; 说明 ${ACCESS_TOKEN} 为 APP 使用者登录成功后自动获取的。 |