Supabase Connection Setup Guide
Project: AI-Powered Photo Journaling iOS App Database: Supabase PostgreSQL Created: 2025-01-11 For: Backend and Frontend developersOverview
This guide explains how to connect to the Supabase database from:- Backend (Next.js API routes)
- Frontend (Flutter iOS app)
Supabase Project Information
Environment Variables Required
Create a.env.local file in your project root:
How to Get Credentials
- Go to Supabase Dashboard
- Select your project
- Navigate to Settings → API
- Copy:
- Project URL →
NEXT_PUBLIC_SUPABASE_URL - anon public key →
NEXT_PUBLIC_SUPABASE_ANON_KEY - service_role key →
SUPABASE_SERVICE_ROLE_KEY(keep secret!)
- Project URL →
Backend Setup (Next.js)
Install Supabase Client
Create Supabase Client
Createlib/supabase/server.ts:
Using in API Routes
Frontend Setup (Flutter)
Install Supabase Flutter Package
Add topubspec.yaml:
Initialize Supabase
Createlib/core/supabase/supabase_config.dart:
Using in Flutter App
Initialize inmain.dart:
Querying Data
Authentication
Sign Up
Sign In
Get Current User
Running Migrations
Option 1: Supabase CLI
Option 2: Supabase Dashboard
- Go to SQL Editor in Supabase Dashboard
- Copy/paste migration SQL files
- Run each migration in order (001, 002, 003, etc.)
Option 3: Programmatically (Backend)
Common Query Patterns
Get User’s Journal Entries (with Emotions)
Full-Text Search
Filter by Emotions
Upload Photo to Storage
Security Best Practices
1. Never Expose Service Role Key
- DO NOT include service role key in frontend code
- DO NOT commit service role key to version control
- USE environment variables only
- ROTATE keys if exposed
2. Use RLS Policies
All queries automatically respect Row Level Security policies:- Users can only access their own data
- No need to manually filter by
user_idin most queries - Policies are enforced at database level
3. Validate Input
Always validate user input before database operations:Troubleshooting
Connection Issues
Error:Invalid API key
- Fix: Verify
NEXT_PUBLIC_SUPABASE_ANON_KEYis correct - Check Supabase Dashboard → Settings → API
JWT expired
- Fix: Refresh user’s auth token
- Supabase client auto-refreshes tokens by default
RLS Policy Issues
Error:new row violates row-level security policy
- Fix: Ensure user is authenticated (
auth.uid()is not null) - Check that RLS policies allow the operation
- Cause: RLS policies filtering out data
- Debug: Use service role key to bypass RLS (testing only!)
Migration Issues
Error:relation already exists
- Cause: Migration already applied
- Fix: Use
IF NOT EXISTSin CREATE statements - Or track applied migrations in a separate table
Performance Tips
- Use indexes: All common queries are indexed (see schema)
- Limit results: Always use
.limit()for list queries - Select specific columns: Don’t use
SELECT *in production - Use connection pooling: Supabase handles this automatically
- Cache frequently accessed data: Implement app-level caching
Related Documentation
Need help? Contact @dba or @backend-developer for database questions.