Overview
The Medical Center API uses PostgreSQL as its database and Prisma as the ORM. This guide covers setting up the database, running migrations, and seeding initial data.Database Schema
The database schema is defined inprisma/schema.prisma and includes the following models:
- Personas - Internal system users (admin, assistants, doctors)
- Usuarios - User authentication linked to Supabase
- Clientes_publicos - Public clients (no authentication required)
- Medicos - Doctors with specialties
- Especialidades - Medical specialties
- Citas - Appointment bookings
- Contacto - Contact form submissions
- Historial - Medical history records
Prerequisites
Before setting up the database:- Install PostgreSQL (v14 or higher)
- Create a database:
- Configure
DATABASE_URLin your.envfile (see Environment Variables)
Initial Setup
Generate Prisma Client
Generate the Prisma Client based on your schema:This creates the typed Prisma Client in
node_modules/@prisma/client.This command runs automatically after
npm install via the postinstall script.Run migrations
Apply all pending migrations to create the database schema:This will:
- Create the database if it doesn’t exist
- Apply all migration files in order
- Generate the Prisma Client
- Prompt you to name the migration if there are schema changes
Migration History
The following migrations have been applied to the database:- 20251214032453_init - Initial database schema
- 20251214033348_add_especialidades - Added medical specialties
- 20251214062323_nuevo_flujo - New appointment workflow
- 20251215004108_add_foto_url_to_personas - Added photo URL to persons
- 20251219201958_add_asunto_to_contacto - Added subject field to contacts
Common Database Operations
Viewing the Database
Open Prisma Studio to browse and edit data visually:http://localhost:5555 where you can view and modify data.
Creating a New Migration
When you modifyprisma/schema.prisma, create a new migration:
Resetting the Database
To reset the database and reapply all migrations:- Drop the database
- Create a new database
- Apply all migrations
- Run the seed script
Viewing Migration Status
Check which migrations have been applied:Deploying Migrations to Production
For production environments, use:Database Seeding
The seed script (prisma/seed.js) populates initial data for medical specialties.
Seed Script Details
Location:prisma/seed.js
upsert to avoid duplicate entries when run multiple times.
Running the Seed Script
Especialidades cargadas
Database Connection Modes
The API supports two database connection modes:Schema Overview
Enums
Key Relationships
Personas→Usuarios(one-to-one) - Links internal users to Supabase authPersonas→Medicos(one-to-one) - Links doctors to person recordsMedicos→Especialidades(many-to-one) - Each doctor has one specialtyClientes_publicos→Citas(one-to-many) - Clients can have multiple appointmentsMedicos→Citas(one-to-many) - Doctors can have multiple appointmentsCitas→Historial(one-to-many) - Appointments can have multiple history records
Troubleshooting
Migration fails with “Database does not exist”
Create the database manually:“Schema is out of sync” error
Generate the Prisma Client:Connection refused errors
Verify PostgreSQL is running:.env:
Seed script fails
Ensure migrations are applied first:Production Considerations
Connection Pooling
For production, use PgBouncer for connection pooling:Backups
Regularly backup your production database:Database Migrations in Production
- Test migrations in staging first
- Use
prisma migrate deployfor production - Never use
prisma migrate devin production - Always backup before running migrations