Supabase SDK & RLS Cheat Sheet

Quick reference guide for Supabase: JS client SDK, Row Level Security (RLS) policies, storage, and auth.

Databases
supabase
backend
postgresql

Supabase is an open-source Firebase alternative providing a Postgres database, Authentication, instant APIs, Realtime subscriptions, and Storage.

Supabase JS Client Setup & Queries

typescript
import { createClient } from "@supabase/supabase-js";

const supabaseUrl = "https://your-project.supabase.co";
const supabaseKey = process.env.SUPABASE_ANON_KEY!;
export const supabase = createClient(supabaseUrl, supabaseKey);

// Select with filtering & relations
const { data, error } = await supabase
  .from("posts")
  .select("id, title, author:users(name, email)")
  .eq("status", "published")
  .order("created_at", { ascending: false });

// Insert record
const { data: newPost, error: insertError } = await supabase
  .from("posts")
  .insert([{ title: "Hello World", content: "Supabase rocks!" }])
  .select();

Row Level Security (RLS) SQL Policies

RLS restricts which table rows users can select, insert, update, or delete.

sql
-- 1. Enable RLS on table
ALTER TABLE posts ENABLE ROW LEVEL SECURITY;

-- 2. Allow public read access to published posts
CREATE POLICY "Public posts are viewable by everyone"
ON posts FOR SELECT
USING (status = 'published');

-- 3. Allow authenticated users to insert their own posts
CREATE POLICY "Users can create posts"
ON posts FOR INSERT
TO authenticated
WITH CHECK (auth.uid() = user_id);

-- 4. Allow users to update only their own posts
CREATE POLICY "Users can update own posts"
ON posts FOR UPDATE
TO authenticated
USING (auth.uid() = user_id);

Authentication & Storage API

typescript
// Auth: Sign Up with Email
const { data: authData, error: authError } = await supabase.auth.signUp({
  email: "user@example.com",
  password: "securepassword123"
});

// Storage: Upload File to Bucket
const { data: fileData, error: fileError } = await supabase.storage
  .from("avatars")
  .upload("public/avatar1.png", fileBody, {
    cacheControl: "3600",
    upsert: true
  });

Common Pitfalls & Tips

[!WARNING] Enabling RLS on a Postgres table without creating any policies will deny all reads and writes for non-service-role requests by default!

[!TIP] Use auth.uid() in Postgres RLS SQL policies to compare the authenticated user's JWT UUID directly against your table's foreign key columns.