Refactor: How to fix the fragile .replace('/vitto-mind', '/vitto-users...

Documentation for Sessions

♻️ Refactor: How to fix the fragile .replace('/vitto-mind', '/vitto-users...

Date: 2026-03-28
Time: 15:22:28 UTC
Session: gspace1-283.6
Agent: backend
Confidence: 97%
Status: ✅ Documented


📋 Decision Context

Question:

How to fix the fragile .replace('/vitto-mind', '/vitto-users') pattern in backend-vitto-api's MongoDB connection setup?

Task: Fix fragile Vitto MongoDB connection pattern


🎯 Decision Made

Chosen:

Replace inline Mongoose config with createMongooseConfig() from shared factory, using VITTO_MONGODB_OPTIONS with explicit dbName. The factory's dbName parameter controls which database is used, regardless of what's in the URI path. Added VITTO_MONGODB_OPTIONS to mongodb.factory.ts with connectionStringKey: 'VITTO_MONGODB_URI' for both mind and users databases.

Options Considered:

  1. Keep .replace() but add validation
  2. Use separate VITTO_USERS_MONGODB_URI env var
  3. 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 a fragile string replace to derive the second database URI. This pattern: (1) Silently fails if VITTO_MONGODB_URI doesn't contain '/vitto-mind' in the path — both connections would hit the same database. (2) Missing production-grade settings — no write concern, no compression, no read preference, no serverApi versioning that all other backends get via the factory. (3) Blocks staging — the staging cluster URI wouldn't contain '/vitto-mind', so the replace would do nothing. The fix aligns vitto-api with the same pattern used by backend-api and backend-ai-api, making the URI path irrelevant.


📁 Files Involved


🔗 Related


Auto-generated by MCP Smart Consolidation