本页还没有译文
本页 36 段还没译成Español,下面显示的是英文原文。Build an API route in less than 2 minutes.
还没翻译译自英文(supabase.com)本页 36 段都还没译
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.
-
Set up a Supabase project with a 'leaderboard' table
Create a new project in the Supabase Dashboard.
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() );- Go to the Table editor section in the Dashboard.
- Click New Table and create a table with the name
leaderboard. - Add a
playercolumn of typetextand ascorecolumn of typeint4. - Click Save.
-
Enable Data API access to Anon Role
Expose the
leaderboardtable through the Data API so it can be queried over HTTP. A leaderboard is meant to be public, so anonymous clients only need read access.For more control over which tables and functions are exposed, read the Grant access explicitly guide.
-- Allow read-only access for anonymous clients grant select on public.leaderboard to anon;In the Integrations > Data API > Settings section of the Dashboard. Under Exposed schemas, make sure
publicis included, then under Exposed tables, toggle on access for theleaderboardtable. -
Configure RLS
Enable Row Level Security (RLS) for this table and create the policies that control who can read and write rows. For a leaderboard, anyone should be able to read scores. Only authenticated users should be able to submit or update them.
-- Turn on RLS alter table "leaderboard" enable row level security; -- Anyone can read the leaderboard create policy "Leaderboard is public" on leaderboard for select to anon, authenticated using (true); -- Authenticated users can submit and update scores create 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); -
Enable Data API access for authenticated and service roles
With RLS setup, grant write access to the
authenticatedandservice_roleroles.-- Grant write access only after RLS and policies are in place grant select, insert, update, delete on public.leaderboard to authenticated; grant select, insert, update, delete on public.leaderboard to service_role; -
Insert some dummy data
Now add some scores to the table so the API has something to query.
insert into leaderboard (player, score) values ('alice', 4200), ('bob', 3700), ('carol', 5100), ('dave', 2900); -
Fetch the data
You can find your API URL and Keys in the Settings > API Settings section of the Dashboard. Query the
leaderboardtable by appending/rest/v1/leaderboardto the API URL.Copy this block of code, substitute
<PROJECT_REF>and<PUBLISHABLE_KEY>, then run it from a terminal.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
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 })final data = await supabase
.from('leaderboard')
.select('*')
.order('score', ascending: false);response = (
supabase.table('leaderboard')
.select("*")
.order('score', desc=True)
.execute()
)let response = try await supabase
.from("leaderboard")
.select()
.order("score", ascending: false)[Table("leaderboard")]
class Leaderboard : BaseModel
{
[PrimaryKey("id", false)]
public int Id { get; set; }
[Column("player")]
public string Player { get; set; }
[Column("score")]
public int Score { get; set; }
}
var result = await supabase
.From<Leaderboard>()
.Order(x => x.Score, Ordering.Descending)
.Get();