Skip to main content

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!

# 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โ€‹

  1. Start MongoDB: docker run -d -p 27017:27017 mongo:7.0
  2. Add to .env.local:
    MONGODB_URI=mongodb://localhost:27017
    MONGODB_DATABASE=caipe
  3. Refresh browser

โœ… Existing local conversations remain accessible
โœ… New conversations saved to MongoDB
โœ… Banner disappears

From MongoDB โ†’ localStorageโ€‹

  1. Stop MongoDB: docker stop caipe-mongo
  2. 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:

  1. Check MongoDB is running: docker ps | grep mongo
  2. Check .env.local has MONGODB_URI and MONGODB_DATABASE
  3. Restart dev server: npm run dev
  4. 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โ€‹

OperationlocalStorage ModeMongoDB 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-10MBUnlimited

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:

  1. localStorage conversations remain in cache
  2. New conversations go to MongoDB
  3. Old conversations accessible but local-only
  4. 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โ€‹

QuestionAnswer
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.