Supabase

Open-source Firebase alternative - instant APIs, real-time, auth, storage on PostgreSQL

TL;DR

What: An open-source Firebase alternative with PostgreSQL.

Why: Instant APIs, real-time subscriptions, auth, storage - all on PostgreSQL.

Quick Start

Create project: Go to supabase.com and create a new project.

Install client:

npm install @supabase/supabase-js

Initialize:

import { createClient } from '@supabase/supabase-js'

const supabase = createClient(
  'https://your-project.supabase.co',
  'your-anon-key'
)

Create table (via Dashboard or SQL):

CREATE TABLE posts (
  id SERIAL PRIMARY KEY,
  title TEXT NOT NULL,
  content TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

Cheatsheet

OperationCode
Selectsupabase.from('table').select()
Insertsupabase.from('table').insert({})
Updatesupabase.from('table').update({}).eq('id', 1)
Deletesupabase.from('table').delete().eq('id', 1)
Filter.eq(), .neq(), .gt(), .lt()
Order.order('column', { ascending: false })
Limit.limit(10)

Gotchas

CRUD operations

// Select
const { data, error } = await supabase
  .from('posts')
  .select('*')
  .order('created_at', { ascending: false })
  .limit(10)

// Insert
const { data, error } = await supabase
  .from('posts')
  .insert({ title: 'Hello', content: 'World' })
  .select()

// Update
const { data, error } = await supabase
  .from('posts')
  .update({ title: 'Updated' })
  .eq('id', 1)
  .select()

// Delete
const { error } = await supabase
  .from('posts')
  .delete()
  .eq('id', 1)

Authentication

// Sign up
const { data, error } = await supabase.auth.signUp({
  email: '[email protected]',
  password: 'password'
})

// Sign in
const { data, error } = await supabase.auth.signInWithPassword({
  email: '[email protected]',
  password: 'password'
})

// Get user
const { data: { user } } = await supabase.auth.getUser()

// Sign out
await supabase.auth.signOut()

Real-time subscriptions

const channel = supabase
  .channel('posts')
  .on('postgres_changes',
    { event: '*', schema: 'public', table: 'posts' },
    (payload) => console.log(payload)
  )
  .subscribe()

Row Level Security

-- Enable RLS
ALTER TABLE posts ENABLE ROW LEVEL SECURITY;

-- Users can only see their own posts
CREATE POLICY "Users can view own posts" ON posts
  FOR SELECT USING (auth.uid() = user_id);

Next Steps