This folder contains comprehensive documentation for the Book Tracker codebase. Use this guide to navigate the available resources.
Comprehensive architecture and design documentation
Contains:
- Overall architecture overview
- Complete directory structure
- Database models and relationships (Book, ReadingSession, ProgressLog, Streak)
- Calibre integration explanation
- Automatic sync mechanism (watcher + instrumentation)
- Complete API routes reference with examples
- Frontend architecture (pages and components)
- Data flow examples (Progress, Sync, Status workflows)
- Configuration files explained
- Deployment information (Docker)
- Design patterns used
- Common workflows for development
- Future expansion points
Best for: Understanding the big picture, architecture decisions, how systems interact
Code snippets, patterns, and quick lookup guide
Contains:
- 10 critical code sections with explanations
- Database connection patterns
- Syncing flow
- Auto-sync architecture
- Reading progress tracking
- Streak calculation
- API route patterns
- Calibre metadata extraction
- Cover image serving
- Status state machine
- Client-side data fetching
- Environment variables reference
- Database indexes quick reference
- Common issues and solutions
- Performance optimization tips
- Testing checklist
Best for: Quick lookups, understanding specific implementation details, troubleshooting
- Start with ARCHITECTURE.md sections 1-3
- Review the directory structure and models
- Read the Calibre integration section
- Look at patterns.md sections 1-2
- Find the feature in ARCHITECTURE.md
- Look for relevant code snippets in patterns.md
- Review the actual source files if needed
- ARCHITECTURE.md section 5 (API Routes)
- patterns.md sections 6-7 (API patterns)
- ARCHITECTURE.md section 6 (Frontend Architecture)
- patterns.md section 10 (Client-side patterns)
- ARCHITECTURE.md sections 3-4 (Calibre integration)
- patterns.md sections 2-3 (Sync details)
- patterns.md section 7 (Metadata extraction)
| File | Purpose | Documentation |
|---|---|---|
| db/mongodb.ts | MongoDB connection | Arch 2, QR 1 |
| db/calibre.ts | Calibre reader | Arch 3, QR 7 |
| sync-service.ts | Sync logic | Arch 3, QR 2 |
| calibre-watcher.ts | File monitoring | Arch 4, QR 3 |
| streaks.ts | Streak calculations | Arch 2, QR 5 |
| File | Purpose | Documentation |
|---|---|---|
| Book.ts | Calibre metadata | Arch 2 |
| ReadingSession.ts | Session tracking | Arch 2 |
| ProgressLog.ts | Progress entries | Arch 2 |
| Streak.ts | Streak tracking | Arch 2 |
| Route | Purpose | Documentation |
|---|---|---|
| /books | Book list | Arch 5, QR 6 |
| /books/:id | Book details | Arch 5 |
| /books/:id/progress | Progress tracking | Arch 5, QR 4 |
| /books/:id/status | Status management | Arch 5, QR 9 |
| /stats/* | Statistics | Arch 5, QR 6 |
| /streaks | Streak data | Arch 5 |
| /calibre/sync | Manual sync | Arch 5 |
| /calibre/status | Sync status | Arch 5 |
| /covers | Cover images | Arch 5, QR 8 |
| Page | Purpose | Documentation |
|---|---|---|
| page.tsx | Dashboard | Arch 6 |
| /library | Book browsing | Arch 6 |
| /books/[id] | Book detail | Arch 6 |
| /stats | Statistics | Arch 6 |
| /settings | Configuration | Arch 6 |
- File Watcher: Monitors Calibre database file for changes
- Debouncing: 2-second wait prevents excessive syncs
- Concurrency Control: isSyncing flag prevents concurrent syncs
- Initial Sync: Runs on server startup
See: ARCHITECTURE.md sections 3-4, QUICK_REFERENCE.md sections 2-3
- Books: From Calibre (read-only in app)
- ReadingSession: User's reading sessions per book (supports re-reading)
- ProgressLog: Individual reading progress entries
- Streak: Consistency metric from progress
See: ARCHITECTURE.md section 2
- Server Components: Dashboard, Stats, Settings load data server-side
- Client Components: Library, Book Detail are interactive
- Data Fetching: Patterns vary by component type
See: ARCHITECTURE.md section 6, QUICK_REFERENCE.md section 10
- Create MongoDB query or aggregation pipeline
- Add API endpoint in /app/api/stats/
- Create or update page in /app/stats or /app/
- Use StatsCard component for display
See: ARCHITECTURE.md section 11
- Check CALIBRE_DB_PATH is set correctly
- Verify file permissions on library folder
- Check server logs for instrumentation hook startup
- Verify file watcher debounce timing
See: QUICK_REFERENCE.md "Common Issues" section
- Use MongoDB aggregation pipeline for stats
- Add indexes for frequently filtered fields
- Use pagination for large result sets
- Fetch only needed fields with projection
See: QUICK_REFERENCE.md "Performance Optimization Tips"
- Update CalibreBook interface in calibre.ts
- Add column to SQL query with dynamic checking
- Include in sync process (sync-service.ts)
- Display in book detail page
See: ARCHITECTURE.md section 3, QUICK_REFERENCE.md section 7
- Understand the sync architecture (ARCHITECTURE.md 4)
- Understand the data model (ARCHITECTURE.md 2)
- Review the API routes (ARCHITECTURE.md 5)
- Review the frontend pages (ARCHITECTURE.md 6)
- Set up environment (.env with Calibre path)
- Run local MongoDB (docker-compose up)
- Start dev server (bun run dev)
- Manual sync from settings page
- Test each page and feature
Always Required Before Making Changes:
- /models/* - Understand the data structures
- /lib/sync-service.ts - Main sync logic
- /app/api/books/[id]/progress/route.ts - Progress tracking logic
- /lib/streaks.ts - Streak calculation logic
Frequently Modified:
- /app/api/books/ - Book endpoints
- /app/[page]/page.tsx - Frontend pages
- /components/ - UI components
Rarely Changed:
- /lib/db/ - Database connections
- /instrumentation.ts - Server initialization
- /next.config.js - Build configuration
- Sync Performance: Large Calibre libraries (10k+ books) take time to sync
- Query Performance: N+1 queries in /api/books need aggregation refactor
- Image Caching: Cover images are immutable cached (1 year)
- Streak Calculation: Uses date normalization for accuracy
See: QUICK_REFERENCE.md "Performance Optimization Tips"
- Docker image uses Bun runtime
- Requires separate MongoDB service
- CALIBRE_DB_PATH must be accessible from container
- File watcher works inside container if library mounted
See: ARCHITECTURE.md section 9
Problem: Sync not working
- Check CALIBRE_DB_PATH in .env
- Verify file permissions
- Check server logs for instrumentation errors
- See QUICK_REFERENCE.md "Common Issues"
Problem: Streak not calculating
- Verify updateStreaks() is called in progress endpoint
- Check date normalization with startOfDay()
- See QUICK_REFERENCE.md section 5
Problem: Cover images not showing
- Verify CALIBRE_DB_PATH points to metadata.db location
- Check library folder permissions from server process
- Look for path validation errors in logs
- See QUICK_REFERENCE.md section 8
- Check the relevant documentation section first
- Look for similar code patterns in the codebase
- Search the Quick Reference for your issue
- Review the "Common Issues" section
- Check server logs for error messages
When making changes to the codebase:
- Update relevant documentation sections
- Add new code snippets to Quick Reference if pattern is novel
- Update API routes table if endpoints change
- Update common workflows if adding new features
Generated: 2025-11-17 Last Updated: 2025-11-17 Scope: Book Tracker v0.1.0