Refactor: How to fix fragile .replace('/vitto-mind', '/vitto-users') p...
Documentation for Sessions
♻️ Refactor: How to fix fragile .replace('/vitto-mind', '/vitto-users') p...
Date: 2026-03-30
Time: 14:34:14 UTC
Session: gspace1-283.6
Agent: backend
Confidence: 97%
Status: ✅ Documented
📋 Decision Context
Question:
How to fix fragile .replace('/vitto-mind', '/vitto-users') pattern in backend-vitto-api MongoDB connection?
Task: S283: Fix fragile Vitto MongoDB connection
🎯 Decision Made
Chosen:
Replace inline Mongoose config with createMongooseConfig() from shared factory, using VITTO_MONGODB_OPTIONS with explicit dbName and connectionStringKey: 'VITTO_MONGODB_URI'. The factory's dbName parameter controls database selection regardless of URI path.
Options Considered:
- Keep .replace() but add validation
- Use separate VITTO_USERS_MONGODB_URI env var
- Use createMongooseConfig() with explicit dbName (chosen)
💭 Reasoning
ROOT CAUSE: backend-vitto-api was the ONLY backend not using the shared createMongooseConfig() factory. It had inline config with .replace('/vitto-mind', '/vitto-users') to derive the second database URI. WHAT BROKE: (1) Silently fails if VITTO_MONGODB_URI doesn't contain '/vitto-mind' — both connections hit same database. (2) Missing production-grade settings — no write concern, no compression, no read preference, no serverApi versioning. (3) Blocks staging — staging cluster URI wouldn't contain '/vitto-mind'. ALTERNATIVES: (1) Keep .replace() but add validation — doesn't solve missing factory settings. (2) Separate VITTO_USERS_MONGODB_URI env var — adds unnecessary config complexity. CHOSEN BECAUSE: Aligns vitto-api with same pattern as backend-api and backend-ai-api. URI path becomes irrelevant. Gets all factory production settings for free. WOULD BE WRONG IF: Vitto needed fundamentally different MongoDB connection settings than other backends (it doesn't). EARLY WARNING: If someone adds a new Vitto database, they should add it to VITTO_MONGODB_OPTIONS in mongodb.factory.ts, not inline it.
📁 Files Involved
APIs/backend-vitto-api/src/app/app.module.tslibs/libs-api/backend-core/src/lib/config/mongodb.factory.ts
🔗 Related
- Decision ID:
dev_1774881254586_j6gem2h - Session: 283
- Workspace: gspace1
Auto-generated by MCP Smart Consolidation