This document explains how to set up and use the Supabase database for the WikiTruth project.
- Visit Supabase Dashboard
- Create a new project
- Record the project URL and API Keys
- Copy
env.templateto.env - Fill in Supabase project configuration:
SUPABASE_URL=https://xxxxxxxxxxxxx.supabase.co
SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...# Install Supabase CLI
npm install -g supabase
# Login to Supabase
supabase login
# Link to project
supabase link --project-ref your-project-ref
# Run migrations
supabase db push- Login to Supabase Dashboard
- Go to Project -> SQL Editor
- Execute the contents of the following files in order:
migrations/20240101000000_01_tables.sqlmigrations/20240101000001_02_indexes.sqlmigrations/20240101000002_03_functions.sql
- Copy
config/supabase.config.example.tstoconfig/supabase.config.ts - Ensure
.envfile is configured - Use in code:
import { supabase } from './config/supabase.config';
// Query example
const { data, error } = await supabase
.from('boxes')
.select('*')
.limit(10);-
Service Role Key:
- Use only on the server side
- Do not expose to clients
- Do not commit to code repository
-
Row Level Security (RLS):
- Recommend enabling RLS for tables
- Configure appropriate access policies
- Refer to RLS policy examples in requirements documentation
-
Environment Variables:
- Do not commit
.envfile to code repository - Use
.env.exampleas template - Use secure key management services in production
- Do not commit
- Create new migration file:
migrations/YYYYMMDDHHMMSS_description.sql - Use
CREATE OR REPLACEto ensure idempotent execution - Test migration files
- Execute migrations
Supabase provides automatic backup functionality. Recommendations:
- Regularly check backup status
- Manually create backups before important changes
- Keep migration file history
- Check for SQL syntax errors
- Verify table dependencies are correct
- Check foreign key constraints
- View logs in Supabase Dashboard
- Check if indexes are created
- Use
EXPLAIN ANALYZEto analyze queries - Optimize query statements
- Consider adding composite indexes
- Check environment variable configuration
- Verify Supabase URL and Key
- Check network connection
- View project status in Supabase Dashboard
-
Configure Test Environment:
# Copy test environment variable template cp env.test.template .env.test # Edit .env.test and fill in Supabase configuration for test environment
-
Install Test Dependencies:
npm install
-
Run Tests:
# Run all tests npm test # Watch mode (for development) npm run test:watch # Run specific tests npm run test:migrations # Migration tests npm run test:network # Network isolation tests npm run test:search # Search function tests npm run test:crud # CRUD operation tests npm run test:types # Type definition tests # Generate coverage report npm run test:coverage