Overview
Echoes of the Past uses Supabase as its database and authentication provider. This guide walks you through setting up your database schema, running migrations, and generating TypeScript types.Prerequisites
Before you begin:- ✅ Supabase project created (see Environment Setup)
- ✅ Environment variables configured
- ✅ Supabase CLI installed
Install Supabase CLI
Install the Supabase CLI globally:Link Your Supabase Project
Connect the Supabase CLI to your remote project:Your project reference can be found in your Supabase dashboard URL:
https://supabase.com/dashboard/project/[project-ref]Database Schema Overview
The application uses the following database tables:Tables
| Table | Description |
|---|---|
historicalFigures | Stores information about historical personalities |
quizzes | Quiz sessions for users |
quizQuestions | Questions associated with quizzes |
feedbacks | User feedback and quiz results |
Enums
| Enum | Values | Usage |
|---|---|---|
categories | scientists, artists, philosophers, leaders, others | Historical figure categorization |
quizType | ai, manual | Quiz generation method |
difficulty_type | easy, medium, hard | Quiz difficulty (legacy) |
mood_type | positive, neutral, negative | User mood tracking (legacy) |
Run Database Migrations
The project includes SQL migration files in thesupabase/migrations/ directory.
Available Migrations
20240308_quiz_system_revert.sql: Removes legacy quiz system tables20240320000000_create_feedback_table.sql: Creates the feedback system
Apply Migrations Remotely
Push migrations to your remote Supabase database:- Apply all pending migrations
- Update your database schema
- Create necessary tables, indexes, and relationships
Verify Migrations
Check the migration status:Local Development Setup (Optional)
For a complete local development environment:Start Supabase Locally
- Start a local Supabase instance using Docker
- Set up PostgreSQL database
- Launch Supabase Studio (local dashboard)
- Configure local authentication
Requires Docker Desktop to be installed and running.
Apply Migrations Locally
- Reset the local database
- Apply all migrations
- Seed data (if configured)
Access Local Database
After starting Supabase locally, you’ll see:.env.local to use local URLs:
.env.local
Generate TypeScript Types
The project includes a script to generate TypeScript types from your Supabase schema.Generate Types from Remote Database
Run the type generation script:Update the
--project-id flag in package.json to match your Supabase project ID.Generated Types Structure
Thedatabase.types.ts file includes:
Using Generated Types
Import and use types in your code:Database Schema Details
Historical Figures Table
Stores information about historical personalities:Quizzes Table
Tracks quiz sessions:Feedbacks Table
Stores quiz results and AI-generated feedback:feedbacks_user_id_idxonuserIdfeedbacks_quiz_id_idxonquizId- GIN indexes on JSONB columns for efficient querying
Supabase Client Configuration
The application uses different Supabase clients depending on the context:Browser Client
Location:utils/supabase/client.ts
Server Client
Location:utils/supabase/server.ts
Middleware Client
Location:utils/supabase/middleware.ts
Handles authentication in Next.js middleware for protected routes.
Row Level Security (RLS)
Ensure Row Level Security policies are configured in your Supabase dashboard to protect user data.
- historicalFigures: Public read access
- quizzes: Users can only access their own quizzes
- feedbacks: Users can only access their own feedback
Enable RLS
In Supabase Dashboard or via SQL:Backup and Restore
Create Backup
Restore from Backup
Next Steps
Troubleshooting
Migration Failed
If a migration fails:Type Generation Errors
If type generation fails:- Verify you’re logged in:
supabase login - Check project ID in
package.json - Ensure you have internet connection
- Try generating from local database:
Cannot Connect to Database
If you can’t connect:- Verify environment variables are correct
- Check Supabase project status in dashboard
- Ensure network firewall allows Supabase connections
- Confirm database password is correct
Local Supabase Won’t Start
If local Supabase fails to start:- Ensure Docker Desktop is running
- Check if ports are available (54321-54324)
- Stop existing containers:
supabase stop - Remove volumes:
supabase db reset --force