Supabase

Alternativa Firebase open-source - PostgreSQL, tiempo real, auth, almacenamiento, Edge Functions

TL;DR

Qué: Una alternativa open-source a Firebase con PostgreSQL.

Por qué: APIs instantáneas, suscripciones en tiempo real, auth, almacenamiento - todo en PostgreSQL.

Quick Start

Crear proyecto: Ve a supabase.com y crea un nuevo proyecto.

Instalar cliente:

npm install @supabase/supabase-js

Inicializar:

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

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

Crear tabla (vía Dashboard o SQL):

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

Cheatsheet

OperaciónCódigo
Selectsupabase.from('table').select()
Insertsupabase.from('table').insert({})
Updatesupabase.from('table').update({}).eq('id', 1)
Deletesupabase.from('table').delete().eq('id', 1)
Filtrar.eq(), .neq(), .gt(), .lt()
Ordenar.order('column', { ascending: false })
Límite.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