Hybrid Storage Quick Start
TL;DRโ
The CAIPE UI now supports both MongoDB and localStorage with automatic fallback:
- โ With MongoDB: Persistent storage, multi-device sync, sharing features
- โ Without MongoDB: Local-only storage, no setup required, fully functional
No configuration needed - the app automatically detects and adapts!
Quick Setupโ
Option 1: localStorage Only (Zero Config)โ
cd ui
# Comment out or remove MongoDB env vars in .env.local
# MONGODB_URI=...
# MONGODB_DATABASE=...
npm run dev
โ App works immediately with local storage!
Option 2: MongoDB Enabled (Recommended)โ
# Start MongoDB
docker run -d --name caipe-mongo -p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=changeme \
mongo:7.0
# Configure UI
cd ui
cat >> .env.local << EOF
MONGODB_URI=mongodb://admin:changeme@localhost:27017
MONGODB_DATABASE=caipe
EOF
npm run dev
โ App works with persistent storage!
Visual Indicatorsโ
MongoDB Mode (Default)โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ + New Chat โ No special indicator
โ โ Conversations synced
โ ๐ฌ Conversation 1 โ
โ ๐ฌ Conversation 2 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
localStorage Mode (Fallback)โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ + New Chat โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ ๐ฆ Local Storage โ โ โ Amber banner
โ โ Mode โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ ๐ฌ Conversation 1 โ
โ ๐ฌ Conversation 2 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
How to Switch Modesโ
From localStorage โ MongoDBโ
- Start MongoDB:
docker run -d -p 27017:27017 mongo:7.0 - Add to
.env.local:MONGODB_URI=mongodb://localhost:27017
MONGODB_DATABASE=caipe - Refresh browser
โ
Existing local conversations remain accessible
โ
New conversations saved to MongoDB
โ
Banner disappears
From MongoDB โ localStorageโ
- Stop MongoDB:
docker stop caipe-mongo - Refresh browser
โ
Cached conversations still visible
โ
New conversations save locally
โ
Banner appears
Testingโ
Test 1: localStorage Mode Worksโ
# Don't start MongoDB
cd ui && npm run dev
Expected:
- โ Amber "Local Storage Mode" banner in sidebar
- โ Can create new conversations
- โ Conversations persist in browser localStorage
- โ No errors in console
Test 2: MongoDB Mode Worksโ
# Start MongoDB first
docker run -d --name caipe-mongo -p 27017:27017 mongo:7.0
# Configure and start UI
cd ui && npm run dev
Expected:
- โ No storage mode banner
- โ Can create new conversations
- โ Conversations visible across browser sessions
- โ Console shows: "Synced X conversations from MongoDB"
Test 3: Automatic Fallbackโ
# Start with MongoDB
docker run -d --name caipe-mongo -p 27017:27017 mongo:7.0
cd ui && npm run dev
# Create some conversations
# Stop MongoDB mid-session
docker stop caipe-mongo
# Try to create new conversation
Expected:
- โ Banner appears after ~1 minute
- โ New conversations still work (localStorage)
- โ No app crashes or errors
- โ Console shows: "Falling back to localStorage"
Common Scenariosโ
Developer Starting Freshโ
git clone repo
cd ui
npm install
npm run dev
โ Works immediately with localStorage (no MongoDB needed)!
Production Deploymentโ
# docker-compose.yml
services:
mongodb:
image: mongo:7.0
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
volumes:
- mongo-data:/data/db
caipe-ui:
build: ./ui
environment:
MONGODB_URI: mongodb://admin:${MONGO_PASSWORD}@mongodb:27017
MONGODB_DATABASE: caipe
โ Persistent storage with backups and scaling!
Demo Environment (No Infrastructure)โ
# Just run the UI, no MongoDB needed
cd ui && npm run dev
โ Fully functional for single-user demos!
Troubleshootingโ
Problem: Banner says "Local Storage Mode" but I want MongoDBโ
Solution:
- Check MongoDB is running:
docker ps | grep mongo - Check
.env.localhasMONGODB_URIandMONGODB_DATABASE - Restart dev server:
npm run dev - Refresh browser
Problem: Conversations disappear when I close browserโ
Cause: Running in localStorage mode and cleared browser data
Solutions:
- Enable MongoDB for persistent storage
- Don't clear browser data
- Use browser's "Don't clear on exit" for this site
Problem: Old conversations not showing after enabling MongoDBโ
Expected behavior: localStorage conversations don't auto-migrate to MongoDB
Solution: They're still in localStorage and will remain accessible. New conversations go to MongoDB.
Advanced Usageโ
Check Current Storage Mode (Console)โ
// In browser console
localStorage.getItem('caipe-chat-history')
// If data exists โ localStorage has conversations
// Check storage mode
fetch('/api/chat/conversations?page=1&page_size=1')
.then(r => r.ok ? 'MongoDB' : 'localStorage')
.then(console.log)
Manually Sync Conversationsโ
// In browser console
// Trigger sync from MongoDB
useChatStore.getState().syncConversationsFromMongoDB()
Force localStorage Mode (Testing)โ
// In mongodb.ts, temporarily set:
export const isMongoDBConfigured = false;
// Or in .env.local, comment out MongoDB vars
Performance Comparisonโ
| Operation | localStorage Mode | MongoDB Mode |
|---|---|---|
| Create conversation | ~1ms โก | ~50ms ๐ |
| Load conversation | ~1ms โก | ~100ms ๐ |
| Search conversations | ~10ms (linear) | ~50ms (indexed) |
| First load | ~1ms โก | ~200ms ๐ |
| Cross-device sync | โ | โ |
| Storage limit | ~5-10MB | Unlimited |
Security Notesโ
localStorage Modeโ
- โ ๏ธ Data stored unencrypted in browser
- โ ๏ธ Accessible to all JS on same origin
- โ ๏ธ Cleared if user clears browsing data
- โ Fine for demos and development
- โ Not recommended for sensitive data
MongoDB Modeโ
- โ Server-side authentication
- โ Encrypted in transit (with TLS)
- โ Backed up on server
- โ Fine for production
- โ Suitable for sensitive data
What Gets Stored Where?โ
Always in localStorage (Zustand Cache)โ
- Recent conversation list
- Active conversation ID
- UI preferences
- Temporary streaming state
MongoDB Mode Onlyโ
- Full conversation history
- Message content with metadata
- Sharing permissions
- User settings
- Search indexes
localStorage Mode Onlyโ
- Full conversation history (local-only)
- Message content
- No sharing features
- Limited to one device
Migration Guideโ
Moving from localStorage to MongoDBโ
No action required! The hybrid system handles it:
- localStorage conversations remain in cache
- New conversations go to MongoDB
- Old conversations accessible but local-only
- Over time, MongoDB becomes source of truth
Optional manual migration: Export from localStorage, import to MongoDB (tool coming soon)
Moving from MongoDB to localStorageโ
Automatic: Recent conversations cached in localStorage remain accessible.
Limitation: Older conversations not in cache become unavailable until MongoDB restored.
Summaryโ
| Question | Answer |
|---|---|
| Do I need MongoDB? | No! App works great with localStorage only |
| Should I use MongoDB? | Yes, for production. Optional for dev. |
| What if MongoDB fails? | App automatically falls back to localStorage |
| Can I switch modes? | Yes, anytime. Data remains accessible. |
| Is it complicated? | No! Automatic detection, zero config needed. |
Recommendation:
- ๐งช Development: localStorage mode (fast setup)
- ๐ญ Demos: localStorage mode (no infrastructure)
- ๐ Production: MongoDB mode (persistent, scalable)
For more details, see the hybrid storage documentation in the UI source code.