Central database service for the HTPI healthcare platform. All microservices communicate with MongoDB through this service via NATS messaging.
This service provides:
- Centralized database operations via NATS
- Multi-tenant data isolation
- Automatic audit logging
- Soft delete functionality
- Connection pooling and optimization
- Index management
┌─────────────────┐ NATS Messages ┌──────────────────┐
│ Microservice │ ─────────────────────▶ │ MongoDB Service │
│ (Patient, etc) │ ◀───────────────────── │ │
└─────────────────┘ Response/Events └────────┬─────────┘
│
▼
┌──────────────────┐
│ MongoDB │
└──────────────────┘
db.request.{operation}
- Database operation requests
Responses are sent back to the requesting service's reply topic.
insert
- Insert single documentinsert_many
- Insert multiple documentsfind
- Find documents with filtering, sorting, paginationfind_one
- Find single documentupdate
- Update single documentupdate_many
- Update multiple documentsdelete
- Soft delete single documentdelete_many
- Soft delete multiple documentscount
- Count documentsaggregate
- Run aggregation pipelinecreate_index
- Create collection index
{
"request_id": "unique-request-id",
"org_id": "organization-id",
"collection": "collection-name",
"operation": "operation-type",
"document": {...}, // For insert
"filter": {...}, // For find/update/delete
"update": {...}, // For update operations
"projection": {...}, // For find operations
"sort": [...], // For find operations
"limit": 10, // For find operations
"skip": 0 // For find operations
}
{
"success": true,
"timestamp": "2024-01-01T00:00:00Z",
"data": {
"request_id": "unique-request-id",
"success": true,
"inserted_id": "document-id", // For insert
"documents": [...], // For find
"count": 10, // For count/find
"matched_count": 1, // For update
"modified_count": 1, // For update
"deleted_count": 1 // For delete
}
}
org_id + _id
- Compound index for multi-tenant queriesorg_id + email
- Unique email per organizationorg_id + mrn
- Unique MRN per organizationorg_id + deleted_at
- Soft delete queries- Text index for full-text search
org_id + _id
- Compound indexorg_id + patient_id
- Patient insurance queriesorg_id + policy_number
- Policy lookupsorg_id + deleted_at
- Soft delete queries
org_id + _id
- Compound indexorg_id + patient_id
- Patient forms queriesorg_id + status
- Status filteringorg_id + created_at
- Date sortingorg_id + deleted_at
- Soft delete queries
org_id + _id
- Compound indexorg_id + form_id
- Form claims queriesorg_id + claimmd_id
- ClaimMD tracking (unique)org_id + status
- Status filteringorg_id + created_at
- Date sorting
All operations are automatically filtered by org_id
to ensure complete data isolation between organizations. The service:
- Adds
org_id
to all insert operations - Includes
org_id
in all query filters - Prevents cross-organization data access
# NATS Configuration
NATS_URL=nats://localhost:4222
NATS_USER=mongodb_service
NATS_PASS=secure_password
# MongoDB Configuration
MONGO_URL=mongodb://localhost:27017
MONGO_DB_NAME=htpi
MONGO_TIMEOUT_MS=5000
MONGO_MAX_POOL_SIZE=100
MONGO_MIN_POOL_SIZE=10
# Service Configuration
LOG_LEVEL=INFO
ENABLE_METRICS=true
METRICS_PORT=9093
// NATS message to db.request.insert
{
"request_id": "req-123",
"org_id": "org-456",
"collection": "patients",
"operation": "insert",
"document": {
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@example.com",
"mrn": "MRN-12345"
}
}
// NATS message to db.request.find
{
"request_id": "req-124",
"org_id": "org-456",
"collection": "patients",
"operation": "find",
"filter": {
"status": "active",
"deleted_at": null
},
"sort": [["created_at", -1]],
"limit": 20,
"skip": 0
}
// NATS message to db.request.update
{
"request_id": "req-125",
"org_id": "org-456",
"collection": "patients",
"operation": "update",
"filter": {
"_id": "patient-id"
},
"update": {
"$set": {
"phone": "555-1234",
"address": "123 Main St"
}
}
}
railway up
docker build -t htpi-mongodb-service .
docker run -e NATS_URL=nats://host:4222 -e MONGO_URL=mongodb://host:27017 htpi-mongodb-service
docker-compose up -d
The service exposes health status via NATS:
- Topic:
health.check
ormongodb-service.health
- Returns: Service status, version, uptime, and database connectivity