在不到两分钟内建立一个 API 路由。
Create your first API route by creating a public leaderboard table.
本指南讲解了如何创建一个 REST 路由,你可以使用 cURL 或浏览器进行查询,通过创建一个名为 leaderboard 的数据库表来存储玩家分数。这会创建一个对应的 API 路由 /rest/v1/leaderboard,可以接收 GET、POST、PATCH 和 DELETE 请求。
🌐 This guide covers creating a REST route you can query using cURL or the browser by creating a database table called leaderboard to hold player scores. This creates a corresponding API route /rest/v1/leaderboard which can accept GET, POST, PATCH, and DELETE requests.
在 Supabase 仪表板中创建一个新项目。
在你的项目准备好之后,在你的 Supabase 数据库中创建一个表。你可以使用 表格编辑器 或 SQL 编辑器 来操作。
🌐 After your project is ready, create a table in your Supabase database. You can do this with either the Table Editor or the SQL Editor.
1-- Create a "leaderboard" table to store2-- player names and their scores.3create table leaderboard (4 id serial primary key,5 player text not null,6 score integer not null default 0,7 created_at timestamptz default now()8);通过数据 API 公开 leaderboard 表,这样就可以通过 HTTP 查询。排行榜本来就是公开的,所以匿名客户端只需要读取权限。
想更好地控制哪些表和函数可以被访问,可以阅读 显式授权指南。
1-- Allow read-only access for anonymous clients2grant select on public.leaderboard to anon;为这个表启用行级安全(RLS)并创建控制谁可以读取和写入行的策略。对于排行榜,任何人都应该可以查看分数。只有经过认证的用户才能提交或更新分数。
1-- Turn on RLS2alter table "leaderboard"3enable row level security;45-- Anyone can read the leaderboard6create policy "Leaderboard is public"7 on leaderboard8 for select9 to anon, authenticated10 using (true);1112-- Authenticated users can submit and update scores13create policy "Authenticated users can submit scores"14 on leaderboard15 for insert16 to authenticated17 with check (true);1819create policy "Authenticated users can update scores"20 on leaderboard21 for update22 to authenticated23 using (true)24 with check (true);设置 RLS 后,给 authenticated 和 service_role 角色写权限。
1-- Grant write access only after RLS and policies are in place2grant select, insert, update, delete on public.leaderboard to authenticated;3grant select, insert, update, delete on public.leaderboard to service_role;现在往表里加一些分数,这样 API 才有东西可以查询。
1insert into leaderboard (player, score)2values3 ('alice', 4200),4 ('bob', 3700),5 ('carol', 5100),6 ('dave', 2900);你可以在仪表板的 设置 > API 设置 部分找到你的 API URL 和密钥。通过在 API URL 后添加 /rest/v1/leaderboard 来查询 leaderboard 表。
复制这段代码,替换 <PROJECT_REF> 和 <PUBLISHABLE_KEY>,然后在终端运行它。
1curl 'https://<PROJECT_REF>.supabase.co/rest/v1/leaderboard?select=*&order=score.desc' \2-H "apikey: <PUBLISHABLE_KEY>"奖金 #
🌐 Bonus
有几种方式可以访问你的数据:
🌐 There are several options for accessing your data:
浏览器 #
🌐 Browser
你可以在浏览器中查询路线,只需将 publishable 键作为查询参数附加即可:
🌐 You can query the route in your browser, by appending the publishable key as a query parameter:
https://<PROJECT_REF>.supabase.co/rest/v1/leaderboard?apikey=<PUBLISHABLE_KEY>
大括号 #
🌐 Curl
1curl 'https://<PROJECT_REF>.supabase.co/rest/v1/leaderboard?select=*&order=score.desc' \2 -H "apikey: <PUBLISHABLE_KEY>" \客户端库 #
🌐 Client libraries
我们提供了多种客户端库。
🌐 We provide a number of Client Libraries.
1const { data, error } = await supabase2 .from('leaderboard')3 .select()4 .order('score', { ascending: false })