Skip to content

Supabase Integration

Supabase provides the backend infrastructure for LenserFight: PostgreSQL database, authentication, file storage, and real-time subscriptions.

Setup

Local Supabase

bash
pnpm supabase start
pnpm supabase:db:reset

Configuration

bash
DATA_SOURCE=supabase
SUPABASE_URL=http://127.0.0.1:54321
SUPABASE_ANON_KEY=<your-anon-key>

Services

ServicePortPurpose
PostgreSQL54322Database
GoTrue54321/authAuthentication
PostgREST54321/restREST API
Realtime54321/realtimeWebSocket subscriptions
Storage54321/storageFile storage
Studio54323Web admin UI

Authentication

typescript
import { supabase } from '@lenserfight/data';

// Sign up
const { data, error } = await supabase.auth.signUp({
  email: 'user@example.com',
  password: 'password',
});

// Sign in
const { data, error } = await supabase.auth.signInWithPassword({
  email: 'user@example.com',
  password: 'password',
});

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

Database queries

typescript
// Select
const { data } = await supabase
  .from('lenses')
  .select('*')
  .eq('visibility', 'public')
  .order('created_at', { ascending: false })
  .limit(20);

// Insert
const { data, error } = await supabase
  .from('lenses')
  .insert({ name: 'My Lens', content: '...' })
  .select()
  .single();

// RPC call
const { data } = await supabase
  .rpc('get_agent_analytics_summary', { p_lenser_id: id });

Real-time subscriptions

typescript
const channel = supabase
  .channel('workflow-runs')
  .on(
    'postgres_changes',
    { event: 'UPDATE', schema: 'public', table: 'workflow_runs' },
    (payload) => {
      console.log('Run updated:', payload.new);
    }
  )
  .subscribe();

Storage

typescript
// Upload
const { data, error } = await supabase.storage
  .from('avatars')
  .upload(`${userId}/avatar.png`, file);

// Download
const { data } = supabase.storage
  .from('avatars')
  .getPublicUrl(`${userId}/avatar.png`);

Security

  • RLS enabled on all tables
  • Service role key is server-only (never expose to browser)
  • Anon key is safe for browser use (RLS restricts access)

Troubleshooting

IssueFix
PGRST116 (no rows)Check RLS policies
42501 (permission denied)Check grants and RLS
Realtime not connectingCheck SUPABASE_URL
Storage upload failsCheck bucket policies

Next steps