This guide will help you complete the migration from Firebase to Supabase for the GameHub project.
The migration has been set up to:
- Replace Firebase with Supabase as the database
- Create a secure API layer that only allows server-side Supabase access
- Maintain the same functionality while improving security and performance
- Supabase Account: Create a new project at supabase.com
- Environment Variables: Update your
.envfile with Supabase credentials - Database Schema: The schema is ready in
supabase-schema.sql
pnpm installUpdate your .env file with the following Supabase variables:
# Supabase Configuration
SUPABASE_PROJECT_URL=your-supabase-project-url
SUPABASE_API_KEY_ANON_PUBLIC=your-supabase-anon-key
SUPABASE_SERVICE_ROLE_SECRET=your-supabase-service-role-secret- Go to your Supabase project dashboard
- Navigate to the SQL Editor
- Copy and paste the contents of
supabase-schema.sql - Execute the SQL to create all tables and indexes
pnpm run migrate:supabaseThis will:
- Test the Supabase connection
- Create sample data
- Verify the setup
Client β Firebase SDK β Firebase Database
Client β API Client β API Routes β Supabase β Database
- Security: Supabase keys never exposed to client
- Performance: Optimized queries and indexing
- Scalability: PostgreSQL-based with advanced features
- Type Safety: Better TypeScript integration
src/
βββ lib/
β βββ supabase.ts # Server-side Supabase client
β βββ api.ts # Client-side API client
βββ api/ # API routes (server-side only)
β βββ users/
β βββ rooms/
β βββ games/
β βββ wallet/
βββ [other directories] # Client-side code (no direct Supabase access)
- Supabase imports are restricted to
/api/**files only - Client-side code must use the API client
- Direct Supabase access from client is blocked
- Row Level Security (RLS) enabled on all tables
- Service role key only used server-side
- API routes handle all database operations
import { apiClient } from '@/lib/api';
// Get user data
const userResponse = await apiClient.getUser('user-id');
if (userResponse.success) {
console.log(userResponse.data);
}
// Create a room
const roomResponse = await apiClient.createRoom({
name: 'Poker Room',
game_type: 'poker',
stake_amount: 100
});
// Update wallet
const walletResponse = await apiClient.addCoins('user-id', 500);import { supabase } from '@/lib/supabase';
// Direct Supabase access (only in API routes)
const { data, error } = await supabase
.from('users')
.select('*')
.eq('id', userId);- users - User information and Telegram data
- wallets - User wallet balances
- rooms - Game rooms and settings
- games - Individual game sessions
- game_players - Many-to-many game participants
- room_players - Many-to-many room participants
- game_history - Game action history
- transactions - Wallet transaction history
- UUID primary keys for better performance
- JSONB columns for flexible data storage
- Proper foreign key relationships
- Automatic timestamps
- Indexes for common queries
- Row Level Security policies
- β Supabase client configuration
- β API routes structure
- β Database schema
- β ESLint rules
- β Migration scripts
- Update existing Firebase usage to use API routes
- Replace Firebase imports with API client calls
- Update type definitions
- Test all functionality
- Export Firebase data
- Transform data format
- Import to Supabase
- Verify data integrity
- Remove Firebase dependencies
- Update deployment scripts
- Update documentation
- Performance testing
pnpm test# Test user creation
curl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{"telegram_id": 123456789, "username": "testuser"}'
# Test room listing
curl http://localhost:3000/api/rooms?game_type=pokerError: Missing Supabase environment variables
Solution: Add required environment variables to .env file
Error: Do not import supabase directly outside of /api
Solution: Use the API client instead of direct Supabase imports
Error: Connection failed
Solution: Check Supabase project URL and service role key
Error: Row Level Security policy violation
Solution: Check and update RLS policies in Supabase dashboard
If you encounter issues during migration:
- Check the error logs
- Verify environment variables
- Test database connection
- Review ESLint rules
- Check Supabase dashboard for errors
Once all steps are completed:
- β Supabase is configured and working
- β API routes are functional
- β Client code uses API client
- β Database schema is deployed
- β Security rules are enforced
- β Tests are passing
Your application is now successfully migrated from Firebase to Supabase!