Overview
ARCA uses PostgreSQL as its database and Prisma ORM for database management, migrations, and type-safe queries. This guide covers database setup, migrations, and maintenance.Database Schema
The ARCA database includes the following main entities:Usuários
Users with role-based access (Admin, Secretário, Supervisor, Estagiário)
Pacientes
Patient records with demographic and treatment information
Atendimentos
Appointment sessions with timestamps and observations
Lista de Espera
Waiting list for prospective patients
Documentos
User documents and discharge reports
Logs de Auditoria
Complete audit trail for compliance
Initial Database Setup
Configure Database URL
Set the
DATABASE_URL in your apps/backend/.env:For production, use connection pooling and SSL:
Run Initial Migration
Navigate to the backend directory and run migrations:This creates all tables defined in the Prisma schema.
Seed the Database
Populate initial data (roles, lookup tables, default users):The seed script creates:
- Roles: ADMIN, SECRETARIO, SUPERVISOR, ESTAGIARIO
- Gêneros: Masculino, Feminino, Não-binário, Prefiro não informar
- Cores de Pele: Branca, Preta, Parda, Amarela, Indígena
- Escolaridades: Fundamental to Pós-graduação
- Status de Atendimento: Agendado, Em Andamento, Concluído, Cancelado, Faltou
- Default Users: One user for each role
Prisma Schema
The database schema is defined inapps/backend/prisma/schema.prisma:
Key Models
Database Migrations
Prisma uses migration files to track and version database schema changes.Creating Migrations
Generate Migration
Create a new migration:Example:This will:
- Generate migration SQL in
prisma/migrations/ - Apply the migration to your database
- Regenerate Prisma Client
Applying Migrations
Migration History
ARCA’s migration history:20250814224132_init
20250814224132_init
Initial database schema with all core tables:
- USUARIOS, ROLES
- PACIENTE, LISTA_ESPERA
- ATENDIMENTOS, STATUS_ATENDIMENTO
- DOCUMENTOS_USUARIO, RELATORIOS_ALTA
- LOGS_AUDITORIA
- Lookup tables (GENERO, CORES_PELE, ESCOLARIDADES)
20250819212429_change_role_id
20250819212429_change_role_id
Changed Role ID from UUID to SmallInt for better performance
20250828012458_add_is_active_to_waitlist
20250828012458_add_is_active_to_waitlist
Added
isActive field to waiting list for soft deletes20250902224108_is_active
20250902224108_is_active
Extended
isActive functionality across entitiesDatabase Management Tools
Prisma Studio
Prisma Studio provides a visual interface for your database:- View and edit records
- Filter and search data
- Explore relationships
- No SQL required
Prisma Studio is for development only. Do not expose it in production.
Prisma Client
Regenerate Prisma Client after schema changes:Database Introspection
Pull schema from existing database:schema.prisma to match your database structure.
Seeding the Database
The seed script (apps/backend/prisma/seed.ts) populates initial data.
Default Users Created
Running Seed Script
Custom Seed Data
Editapps/backend/prisma/seed.ts to add your own seed data:
Production Database Setup
Managed PostgreSQL Options
Supabase
Free tier available, automatic backups, connection pooling
Neon
Serverless PostgreSQL with branching
Railway
Easy setup with automatic SSL
AWS RDS
Enterprise-grade with full control
Production Configuration
Backup Strategy
Automated Backups
Automated Backups
Enable automated backups on your database provider:
- Supabase: Automatic daily backups
- Railway: Point-in-time recovery
- AWS RDS: Configure automated snapshots
Manual Backup
Manual Backup
Create manual database dumps:
Export Data
Export Data
Export specific data using Prisma:
Troubleshooting
Migration Conflicts
Migration Conflicts
If migrations are out of sync:
Connection Pool Exhausted
Connection Pool Exhausted
Increase connection limit or use connection pooling:Or use PgBouncer/Supabase pooler.
Schema Drift
Schema Drift
When database doesn’t match schema:
Slow Queries
Slow Queries
Add indexes for commonly queried fields:Then create migration:
Best Practices
Version Control
Commit all migration files to version control
Test Migrations
Test migrations on staging before production
Backup Before Migrating
Always backup production database before migrations
Use Transactions
Wrap multiple operations in Prisma transactions
Next Steps
Production Deployment
Deploy your application to production
API Reference
Explore the backend API endpoints