Background & Context
Purpose: Document the current state, limitations, and rationale for the new schema design
Current State Analysis
Database Statistics
| Metric | Value |
|---|---|
| Total Tables | ~501 |
| Domain Modules (parts/) | 13 |
Tables with clinic_* prefix | 66 |
Tables with basic_* prefix | 46 |
| Unprefixed tables | 57 |
| Duplicate/conflicting names | Several |
Current Domain Modules
The backend uses a parts-based architecture with 13 domain modules:
backend/parts/
├── clinic/ # 66 tables - Clinic operations
├── patient/ # 25 tables - Patient health records
├── doctor/ # 25 tables - Practitioner profiles
├── billing/ # 20 tables - Payment processing
├── hospital/ # 12 tables - Hospital operations (overlaps with clinic)
├── lab/ # 6 tables - Laboratory services
├── pharmacy/ # 5 tables - Pharmacy operations
├── growth/ # 11 tables - Marketing/leads
├── care_coordinator/# 7 tables - Care coordination
├── abdm/ # 15 tables - India healthcare integration
├── auth/ # ~8 tables - Authentication
├── admin/ # ~2 tables - Administration
└── core/ # 46+ tables - Shared reference dataNaming Convention Issues
1. Duplicate/Conflicting Names
careplansANDcare_plans(both exist)pharmasANDpharmacies(inconsistent)goalsexists standalone and in patient context
2. Inconsistent Abbreviations
msg_*vsmessage_*fb_*vsfacebook_*gmb_*(Google My Business)ip_encounters(IP = inpatient?)
3. Blurred Domain Boundaries
clinic_patient_*(clinic-scoped) vspatient_*(global)clinic_doctor_*vsdoctor_*- Unclear what belongs to which domain
4. Reference Data Scattered
basic_*prefix for lookup tables- Some reference data unprefixed
- No clear separation between global and tenant-specific
Why Change is Needed
1. Facility Model Limitations
Problem: Everything is a "Clinic"
The current model uses clinics table for all facility types:
- Medical clinics (outpatient)
- Hospitals (inpatient)
- Surgery centers
- Therapy centers (talk therapy, physical therapy)
- Diagnostic centers
- Virtual practices
Limitation: A single is_hospital boolean cannot capture the complexity of facilities with:
- OPD (outpatient department)
- IPD (inpatient department)
- Single OT (operation theater)
- Doctor-owned specialty centers
Impact: UI confusion, reporting limitations, incorrect feature exposure
2. Multi-Tenancy is Implicit
Current State:
- Tenant isolation via
clinic_idforeign keys - No database-level enforcement
- Risk of cross-tenant data leakage through application bugs
Needed:
- Explicit tenant boundaries
- Row-Level Security (RLS) at PostgreSQL level
- Clear tenant context for every operation
3. FHIR Compliance Gaps
Current State:
- Partial FHIR alignment for ABDM (India)
- Organization resource partially mapped
- No formal Location, HealthcareService, PractitionerRole resources
Needed:
- Complete FHIR R4 resource mapping for core entities
- Standardized identifier management
- Cross-border healthcare data exchange capability
4. LHR Architecture Already Defined
The team has done significant work on Longitudinal Health Record architecture:
Implemented:
- Episode of Care model
- Care Plans with versioning
- Multi-practitioner encounters
- Treatment goals and progress notes
- Bridge pattern for encounter documents
Reference: See Clinical Data Hierarchy
The new schema should preserve and enhance this work.
5. AI-Assisted Care Integration
Current State:
- AI Scribe with transcription sessions
- AI suggestions linked to appointments
- LLM call logging for audit
Needed:
- Proper FHIR linkage for AI suggestions
- Clear separation of clinical AI data vs audit logs
- Research/analytics data model
6. Multi-Country Expansion
Current Markets:
- India (IN) - ABDM/ABHA, DPDP, GST
- UAE (AE) - DHA, VAT
- USA (US) - HIPAA, insurance
Planned Expansion:
- Saudi Arabia (SA) - MOH, NPHIES
- Qatar (QA) - MOPH
- Kuwait (KW) - MOH
Needed:
- Country-agnostic core with country-specific compliance modules
- Flexible identifier systems
- Regional configuration patterns
FHIR R4 Alignment Requirements
Core Resources to Map
| FHIR Resource | Current Entity | New Entity |
|---|---|---|
| Organization | ClinicGroup, Clinic | Organization, Facility |
| Location | ClinicLocation | FacilityLocation |
| HealthcareService | ClinicService | HealthcareService |
| Practitioner | Doctor | Practitioner |
| PractitionerRole | ClinicDoctor | PractitionerRole |
| Patient | Patient | Patient |
| Encounter | Appointment, Encounter | Encounter |
| EpisodeOfCare | EpisodeOfCare | EpisodeOfCare |
| CarePlan | CarePlan | CarePlan |
| Goal | Goal | Goal |
| Condition | PatientProblem | Condition |
| Observation | PatientVital | Observation |
| MedicationRequest | Prescription | MedicationRequest |
| ServiceRequest | (partial) | ServiceRequest |
| DiagnosticReport | (partial) | DiagnosticReport |
FHIR Adoption Mandates
| Region | Authority | Requirement |
|---|---|---|
| USA | CMS/ONC | 21st Century Cures Act |
| UK | NHS Digital | NHS Data Standards |
| India | ABDM | Ayushman Bharat Digital Mission |
| UAE | DHA | Dubai Health Authority standards |
| GCC | Various | Emerging requirements |
Multi-Country Regulatory Landscape
India (IN)
| Requirement | Description |
|---|---|
| ABDM | Ayushman Bharat Digital Mission - health data exchange |
| ABHA | Ayushman Bharat Health Account - patient identifier |
| DPDP | Digital Personal Data Protection Act 2023 |
| GST | Goods and Services Tax for billing |
| MCI | Medical Council registration for practitioners |
UAE (AE)
| Requirement | Description |
|---|---|
| DHA | Dubai Health Authority integration |
| MOHAP | Ministry of Health and Prevention |
| VAT | Value Added Tax (5%) |
| Emirates ID | National identifier integration |
USA (US)
| Requirement | Description |
|---|---|
| HIPAA | Health Insurance Portability and Accountability Act |
| NPI | National Provider Identifier |
| DEA | Drug Enforcement Administration (controlled substances) |
| Insurance | Multi-payer insurance integration |
GCC Expansion
| Country | Key Requirements |
|---|---|
| Saudi Arabia (SA) | MOH, NPHIES (insurance), NHRA |
| Qatar (QA) | MOPH, Qatar Health Card |
| Kuwait (KW) | MOH, Civil ID integration |
Existing LHR Work
The new schema must support the clinical architecture already designed:
Clinical Data Hierarchy
Patient
|
+-- LHR (Longitudinal Health Record) <-- Computed, not stored
| |
| +-- Problems, Medications, Vitals, Assessments
| +-- Encounter History (grouped by Episode)
| +-- Health Score (computed)
|
+-- Episodes of Care <-- Treatment program containers
| |
| +-- Episode: "Post-ACL Rehab"
| | +-- Treatment Goals
| | +-- Progress Notes
| | +-- Encounters (sessions)
| |
| +-- Episode: "Anxiety Treatment"
| +-- Treatment Goals
| +-- Safety Plans
| +-- Encounters (therapy sessions)
|
+-- Standalone Encounters
+-- One-off visits, acute careKey Patterns to Preserve
- LHR as Computed View - Aggregated from FHIR resources, not stored
- Episode of Care - FHIR-aligned treatment program container
- Bridge Pattern - EncounterDocument links to CarePlan, Goal, EpisodeOfCare
- Document Immutability - Signed documents are immutable, amendments chain
- CarePlan Versioning - Versions track via
replaces_id
Reference Documentation
Summary
The new schema design addresses:
| Challenge | Solution |
|---|---|
| Clinic-centric model | Multi-dimensional facility classification |
| Implicit multi-tenancy | Explicit tenants with RLS |
| 501 scattered tables | 7 domain schemas with clear boundaries |
| Naming inconsistencies | Consistent entity naming aligned with FHIR |
| Partial FHIR alignment | Complete FHIR R4 resource mapping |
| LHR implementation | Preserve and enhance existing architecture |
| AI integration gaps | Clinical AI in clinical schema, audit in audit schema |
| Multi-country complexity | Country-agnostic core with compliance modules |