Using Supabase from Your App
How Supabase works
- Your project is a PostgreSQL database
- PostgREST turns tables, views and functions into a REST API automatically
- The JS client (
@supabase/supabase-js) builds those requests for you — and they're parameterized (no SQL injection) - Auth issues a JWT; Postgres reads it (
auth.uid()) so RLS decides what each user can access - Realtime streams database changes over WebSockets
- Storage keeps files, with policies just like tables
Keys
anon(public) key → safe in the browser/app only because RLS protects your tablesservice_rolekey → full access, server-side only (Edge Functions, your backend)
Query builder ↔ SQL
| supabase-js | SQL |
|---|---|
.from('expenses').select('title, amount') | SELECT title, amount FROM expenses |
.eq('user_id', id) / .neq / .gt / .gte / .lt | WHERE user_id = … |
.ilike('title', '%groc%') | ILIKE |
.in('category_id', [7, 8]) | IN (…) |
.order('spent_on', { ascending: false }) | ORDER BY … DESC |
.range(0, 19) | LIMIT 20 OFFSET 0 |
.select('*, categories(name)') | JOIN via foreign key |
.insert({...}).select() | INSERT … RETURNING * |
.upsert({...}) | INSERT … ON CONFLICT DO UPDATE |
.rpc('fn', { args }) | SELECT fn(args) |
When to use a database function (rpc)
- Aggregations and reports (sums, GROUP BY)
- Multi-step operations that must be atomic (transfers, creating related rows)
- Complex queries the builder can't express
Generated types
supabase gen types typescript creates TypeScript types from your schema → autocomplete and type-safe queries.
Always check error
Every call returns { data, error }. Handle errors (RLS violations, constraint errors) and show friendly messages.
Example
// npm i @supabase/supabase-js
import { createClient } from "@supabase/supabase-js";
const supabase = createClient(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_ANON_KEY // public key: RLS protects the data
);
// Sign in (gives the user a JWT → auth.uid() in SQL)
await supabase.auth.signInWithPassword({ email: "asha@example.com", password: "…" });
// SELECT with a filter, a join through the FK, ordering and pagination
const { data: expenses, error } = await supabase
.from("expenses")
.select("id, title, amount, spent_on, categories ( name )")
.gte("spent_on", "2026-11-01")
.order("spent_on", { ascending: false })
.range(0, 19);
if (error) console.error(error.message);
// INSERT and get the new row back (RETURNING)
const { data: created } = await supabase
.from("expenses")
.insert({ title: "Momo", amount: 250, category_id: 8 }) // user_id defaults to auth.uid()
.select()
.single();
// UPDATE / DELETE (RLS only lets users touch their own rows)
await supabase.from("expenses").update({ amount: 300 }).eq("id", created.id);
await supabase.from("expenses").delete().eq("id", created.id);
// Reports and atomic operations → a database function
// SQL: CREATE FUNCTION monthly_summary(p_month DATE) RETURNS TABLE (category TEXT, total NUMERIC) ...
const { data: summary } = await supabase.rpc("monthly_summary", { p_month: "2026-11-01" });
// Realtime: react to new expenses as they're inserted
supabase
.channel("expenses-feed")
.on("postgres_changes",
{ event: "INSERT", schema: "public", table: "expenses" },
(payload) => console.log("New expense:", payload.new))
.subscribe();