Spaces:
Sleeping
Supabase for SwiftOps - Complete Guide
What is Supabase?
Supabase is an open-source Firebase alternative that provides:
- PostgreSQL Database - Your main data storage
- Authentication - User login/signup (email, magic links, OAuth)
- Storage - File uploads (images, documents, PDFs)
- Row-Level Security (RLS) - Database-level authorization
- Realtime - Live updates when data changes
- Edge Functions - Serverless functions (optional for MVP)
- Auto-generated REST API - No backend code needed!
Why Supabase for SwiftOps MVP?
β
Database-First Approach - Perfect for your strategy
β
No Backend Required - Frontend talks directly to database
β
Built-in Security - RLS policies protect your data
β
Real-time Updates - Track field agents live
β
File Storage - Store ticket photos, documents
β
Fast Development - Get to market quickly
SwiftOps Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FRONTEND (React/Next.js) β
β - User Interface β
β - Business Logic (browser-side) β
β - Optimistic Updates β
β - Error Handling β
βββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ
β
β Supabase Client (supabase-js)
β
βββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββ
β SUPABASE PLATFORM β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β PostgreSQL Database (34 tables) β β
β β - Tickets, Users, Projects, Inventory, etc. β β
β β - Row-Level Security (RLS) enabled β β
β β - Optimistic locking (version columns) β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Authentication (auth.users) β β
β β - Email/Password, Magic Links, OTP β β
β β - JWT tokens for RLS policies β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Storage (Buckets) β β
β β - ticket-images, documents, user-docs β β
β β - RLS policies for file access β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Realtime (WebSockets) β β
β β - Live location tracking β β
β β - Ticket status updates β β
β β - Notifications β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β PostgREST API (Auto-generated) β β
β β - CRUD operations on all tables β β
β β - Filtering, sorting, pagination β β
β β - Enforces RLS policies β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Documentation Structure
This documentation is organized into focused modules:
π Getting Started
- 01_SETUP.md - Initial Supabase project setup
- 02_LOCAL_DEVELOPMENT.md - Running Supabase locally with Docker
π Authentication & Security
- 03_AUTHENTICATION.md - User login, signup, session management
- 04_ROW_LEVEL_SECURITY.md - RLS policies, helper functions, testing
πΎ Database Operations
- 05_DATABASE_QUERIES.md - CRUD operations, filtering, joins
- 06_MIGRATIONS.md - Schema versioning, deployment workflow
- 07_OPTIMISTIC_LOCKING.md - Handling concurrent writes safely
π File Management
- 08_STORAGE.md - File uploads, downloads, policies
β‘ Real-time Features
- 09_REALTIME.md - Live updates, location tracking, notifications
π οΈ Advanced Topics
- 10_ERROR_HANDLING.md - Graceful failures, retry logic
- 11_EDGE_FUNCTIONS.md - Serverless functions (future)
- 12_BEST_PRACTICES.md - Performance, security, patterns
π Reference
- 13_SWIFTOPS_EXAMPLES.md - Real code examples for SwiftOps features
- 14_TROUBLESHOOTING.md - Common issues and solutions
Key Concepts for Junior Developers
1. No Traditional Backend
You don't write Express.js or FastAPI. Your React app talks directly to Supabase using the supabase-js client library.
2. Row-Level Security (RLS) is Your Backend
RLS policies are SQL rules that control who can read/write data. They run in the database, so they can't be bypassed.
Example: A field agent can only see tickets assigned to them.
3. Optimistic Updates
Update the UI immediately (optimistic), then sync with database. If it fails, rollback the UI change.
4. Graceful Failures
Always handle errors. Never leave the app in a broken state.
// β BAD - No error handling
const { data } = await supabase.from('tickets').insert(ticket);
// β
GOOD - Graceful error handling
const { data, error } = await supabase.from('tickets').insert(ticket);
if (error) {
console.error('Failed to create ticket:', error);
toast.error('Failed to create ticket. Please try again.');
return;
}
toast.success('Ticket created successfully!');
5. Transactions Don't Exist in Supabase Client
You can't do multi-step transactions from the frontend. Use:
- Database triggers
- Edge Functions (for complex logic)
- Optimistic locking (version columns)
6. Everything is Async
All Supabase operations return Promises. Always use async/await or .then().
Quick Start Checklist
- Read 01_SETUP.md - Create Supabase project
- Read 02_LOCAL_DEVELOPMENT.md - Set up local environment
- Read 03_AUTHENTICATION.md - Implement login/signup
- Read 04_ROW_LEVEL_SECURITY.md - Deploy RLS policies
- Read 05_DATABASE_QUERIES.md - Learn CRUD operations
- Read 06_MIGRATIONS.md - Deploy your schema
- Read 08_STORAGE.md - Upload ticket photos
- Read 09_REALTIME.md - Track field agents live
- Read 10_ERROR_HANDLING.md - Handle failures gracefully
Common Pitfalls to Avoid
β Forgetting to Enable RLS
-- Without this, anyone can access your data!
ALTER TABLE tickets ENABLE ROW LEVEL SECURITY;
β Using Service Role Key in Frontend
// β NEVER DO THIS - Exposes full database access
const supabase = createClient(url, SERVICE_ROLE_KEY);
// β
Use anon key instead
const supabase = createClient(url, ANON_KEY);
β Not Handling Errors
// β BAD
const { data } = await supabase.from('tickets').select();
// What if this fails? Your app crashes!
// β
GOOD
const { data, error } = await supabase.from('tickets').select();
if (error) {
// Handle the error gracefully
}
β Forgetting Optimistic Locking
// β BAD - Race condition possible
await supabase.from('tickets').update({ status: 'completed' }).eq('id', ticketId);
// β
GOOD - Check version to prevent conflicts
const { data, error } = await supabase
.from('tickets')
.update({ status: 'completed', version: currentVersion + 1 })
.eq('id', ticketId)
.eq('version', currentVersion);
if (data.length === 0) {
// Someone else updated this ticket - conflict!
}
Environment Variables
You'll need these in your .env.local:
# Supabase Configuration
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key-here
# DO NOT expose service role key in frontend!
# Only use in server-side code or Edge Functions
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key-here
Getting Help
- Read the relevant doc file - Start with the specific topic
- Check 14_TROUBLESHOOTING.md - Common issues and solutions
- Supabase Official Docs - https://supabase.com/docs
- Supabase Discord - https://discord.supabase.com
- Stack Overflow - Tag questions with
supabase
Next Steps
π Start with 01_SETUP.md to create your Supabase project!