Supabase
Supabase cheat sheet covering PostgreSQL queries, Auth, Storage, Realtime subscriptions, Edge Functions, and client SDK examples.
Sign in to mark items as known and track your progress.
Sign inSetup & Configuration
Getting started with Supabase
Installation & Setup
Setting up Supabase in your project
# Install Supabase client
npm install @supabase/supabase-js
# Install CLI for local development
npm install -g supabase
# Initialize Supabase project
supabase init
# Start local development
supabase startClient Initialization
Initialize Supabase client in your application
import { createClient } from '@supabase/supabase-js'
// Initialize client
const supabase = createClient(
'YOUR_SUPABASE_URL',
'YOUR_ANON_KEY'
)
// With TypeScript types
import { Database } from './types/supabase'
const supabase = createClient<Database>(
url,
key
)Database Operations
CRUD operations and queries with Supabase
Basic CRUD Operations
Create, Read, Update, Delete operations
// SELECT - Get data
const { data, error } = await supabase
.from('posts')
.select('*')
// INSERT - Create record
const { data, error } = await supabase
.from('posts')
.insert({ title: 'Hello', content: 'World' })
// UPDATE - Modify record
const { data, error } = await supabase
.from('posts')
.update({ title: 'Updated' })
.eq('id', 1)
// DELETE - Remove record
const { error } = await supabase
.from('posts')
.delete()
.eq('id', 1)Advanced Queries
Complex queries, joins, and filters
// Foreign table joins
const { data } = await supabase
.from('posts')
.select(`
*,
author:users(name, email),
comments(id, content)
`)
// Filtering
const { data } = await supabase
.from('posts')
.select('*')
.eq('status', 'published')
.gte('created_at', '2024-01-01')
.order('created_at', { ascending: false })
.limit(10)Realtime Subscriptions
Subscribe to database changes in real-time
// Subscribe to INSERT
const channel = supabase
.channel('posts-insert')
.on('postgres_changes', {
event: 'INSERT',
schema: 'public',
table: 'posts'
}, (payload) => {
console.log('New post:', payload.new)
})
.subscribe()
// Cleanup
channel.unsubscribe()Authentication
User authentication and authorization
Auth Operations
Sign up, sign in, and session management
// Sign up
const { data, error } = await supabase.auth.signUp({
email: 'user@example.com',
password: 'password123'
})
// Sign in
const { data, error } = await supabase.auth.signInWithPassword({
email: 'user@example.com',
password: 'password123'
})
// Sign out
await supabase.auth.signOut()Row Level Security (RLS)
Secure database access with policies
-- Enable RLS on table
ALTER TABLE posts ENABLE ROW LEVEL SECURITY;
-- Policy: Users can read all posts
CREATE POLICY "Public posts are readable by everyone"
ON posts FOR SELECT
USING (status = 'published');
-- Policy: Users can only update their own posts
CREATE POLICY "Users can update own posts"
ON posts FOR UPDATE
USING (auth.uid() = author_id);Storage
File storage and management
File Operations
Upload, download, and manage files
// Upload file
const { data, error } = await supabase.storage
.from('avatars')
.upload('public/avatar.png', file)
// Download file
const { data } = await supabase.storage
.from('avatars')
.download('public/avatar.png')
// Get public URL
const { data } = supabase.storage
.from('avatars')
.getPublicUrl('public/avatar.png')Edge Functions
Serverless functions at the edge
Edge Functions
Deploy and invoke serverless functions
// Create function (functions/hello/index.ts)
import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
serve(async (req) => {
const { name } = await req.json()
return new Response(
JSON.stringify({ message: `Hello ${name}!` }),
{ headers: { 'Content-Type': 'application/json' } }
)
})
// Deploy
supabase functions deploy hello
// Invoke
const { data, error } = await supabase.functions
.invoke('hello', { body: { name: 'World' } })Advanced Features
Vectors, full-text search, and database functions
Vector Embeddings & AI
Work with vector embeddings for AI applications
// Enable pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;
// Create table with embeddings
CREATE TABLE documents (
id serial PRIMARY KEY,
content text,
embedding vector(1536)
);
// Store embedding
const { data, error } = await supabase
.from('documents')
.insert({
content: 'Document text',
embedding: vectorArray
})Database Functions & Triggers
Custom functions and automatic triggers
-- Create function
CREATE FUNCTION increment_views(post_id INT)
RETURNS void AS $$
BEGIN
UPDATE posts
SET views = views + 1
WHERE id = post_id;
END;
$$ LANGUAGE plpgsql;
-- Call from client
const { data, error } = await supabase
.rpc('increment_views', { post_id: 123 })Migration & Deployment
Database migrations and deployment strategies
Migrations & Deployment
Managing schema changes and deployments
# Create migration
supabase migration new add_users_table
# Apply migrations
supabase db push
# Generate types
supabase gen types typescript --project-id <id> > types.ts
# Deploy to production
supabase link --project-ref <ref>
supabase db push