在不到两分钟内建立一个 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.
-- Create a "leaderboard" table to store-- player names and their scores.create table leaderboard ( id serial primary key, player text not null, score integer not null default 0, created_at timestamptz default now());通过数据 API 公开 leaderboard 表,这样就可以通过 HTTP 查询。排行榜本来就是公开的,所以匿名客户端只需要读取权限。
想更好地控制哪些表和函数可以被访问,可以阅读 显式授权指南。
-- Allow read-only access for anonymous clientsgrant select on public.leaderboard to anon;为这个表启用行级安全(RLS)并创建控制谁可以读取和写入行的策略。对于排行榜,任何人都应该可以查看分数。只有经过认证的用户才能提交或更新分数。
-- Turn on RLSalter table "leaderboard"enable row level security;-- Anyone can read the leaderboardcreate policy "Leaderboard is public" on leaderboard for select to anon, authenticated using (true);-- Authenticated users can submit and update scorescreate policy "Authenticated users can submit scores" on leaderboard for insert to authenticated with check (true);create policy "Authenticated users can update scores" on leaderboard for update to authenticated using (true) with check (true);设置 RLS 后,给 authenticated 和 service_role 角色写权限。
-- Grant write access only after RLS and policies are in placegrant select, insert, update, delete on public.leaderboard to authenticated;grant select, insert, update, delete on public.leaderboard to service_role;现在往表里加一些分数,这样 API 才有东西可以查询。
insert into leaderboard (player, score)values ('alice', 4200), ('bob', 3700), ('carol', 5100), ('dave', 2900);你可以在仪表板的 设置 > API 设置 部分找到你的 API URL 和密钥。通过在 API URL 后添加 /rest/v1/leaderboard 来查询 leaderboard 表。
复制这段代码,替换 <PROJECT_REF> 和 <PUBLISHABLE_KEY>,然后在终端运行它。
curl 'https://<PROJECT_REF>.supabase.co/rest/v1/leaderboard?select=*&order=score.desc' \-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
curl 'https://<PROJECT_REF>.supabase.co/rest/v1/leaderboard?select=*&order=score.desc' \ -H "apikey: <PUBLISHABLE_KEY>" \客户端库 #
🌐 Client libraries
我们提供了多种客户端库。
🌐 We provide a number of Client Libraries.
const { data, error } = await supabase .from('leaderboard') .select() .order('score', { ascending: false })