|
| 1 | +# Notes API - Modular Structure |
| 2 | + |
| 3 | +The notes API has been modularized to improve maintainability and make it easier to find and fix bugs. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +``` |
| 8 | +services/api/notes/ |
| 9 | +├── index.ts # Main export, combines all modules |
| 10 | +├── crud.ts # Create, update, delete operations (~400 lines) |
| 11 | +├── core.ts # Query and other operations (to be further extracted) |
| 12 | +├── cache-sync.ts # Cache synchronization utilities |
| 13 | +├── shared.ts # Shared helper functions |
| 14 | +└── README.md # This file |
| 15 | +``` |
| 16 | + |
| 17 | +## Modules |
| 18 | + |
| 19 | +### `crud.ts` - Create, Update, Delete Operations |
| 20 | +**~400 lines** | ✅ Modularized |
| 21 | + |
| 22 | +Contains: |
| 23 | +- `createNote()` - Create new notes with offline support |
| 24 | +- `updateNote()` - Update existing notes with cache sync |
| 25 | +- `deleteNote()` - Delete notes |
| 26 | + |
| 27 | +**Key Features:** |
| 28 | +- Automatic cache synchronization (SQLite + AsyncStorage) |
| 29 | +- Offline support with optimistic updates |
| 30 | +- Proper error handling and logging |
| 31 | + |
| 32 | +### `cache-sync.ts` - Cache Synchronization |
| 33 | +**~50 lines** | ✅ Modularized |
| 34 | + |
| 35 | +Contains: |
| 36 | +- `updateSQLiteCache()` - Update local database cache |
| 37 | +- `clearAsyncStorageCaches()` - Clear preview caches |
| 38 | +- `syncAllCaches()` - Sync all cache layers |
| 39 | + |
| 40 | +**Purpose:** Centralized cache management to prevent stale data issues. |
| 41 | + |
| 42 | +### `shared.ts` - Shared Utilities |
| 43 | +**~40 lines** | ✅ Modularized |
| 44 | + |
| 45 | +Contains: |
| 46 | +- `invalidateCountsCache()` - Invalidate counts and database caches |
| 47 | + |
| 48 | +### `core.ts` - Core Operations |
| 49 | +**~1000 lines** | 🔄 To be further modularized |
| 50 | + |
| 51 | +Currently contains: |
| 52 | +- Query operations (`getNotes`, `getNote`, `getCounts`) |
| 53 | +- Visibility operations (`hideNote`, `unhideNote`) |
| 54 | +- Attachment operations (file upload/download) |
| 55 | +- Trash operations (`emptyTrash`) |
| 56 | + |
| 57 | +**Next Steps:** Extract these into separate modules: |
| 58 | +- `queries.ts` - Query operations |
| 59 | +- `visibility.ts` - Hide/unhide operations |
| 60 | +- `attachments.ts` - File operations |
| 61 | + |
| 62 | +## Benefits of Modularization |
| 63 | + |
| 64 | +### 1. Easier Bug Fixes |
| 65 | +**Before:** Search through 1269 lines to find delete logic |
| 66 | +**After:** Go directly to `crud.ts` (~400 lines) |
| 67 | + |
| 68 | +### 2. Better Code Organization |
| 69 | +Each module has a single responsibility: |
| 70 | +- CRUD operations → `crud.ts` |
| 71 | +- Cache management → `cache-sync.ts` |
| 72 | +- Shared utilities → `shared.ts` |
| 73 | + |
| 74 | +### 3. Easier Testing |
| 75 | +Each module can be tested independently: |
| 76 | +```typescript |
| 77 | +import { createCrudOperations } from './crud'; |
| 78 | +// Test just CRUD operations |
| 79 | +``` |
| 80 | + |
| 81 | +### 4. Cache Issue Resolution |
| 82 | +The cache synchronization bugs were caused by mixing cache logic throughout the file. Now it's centralized in `cache-sync.ts`, making it obvious when caches need to sync. |
| 83 | + |
| 84 | +## Cache Architecture |
| 85 | + |
| 86 | +The app has **3 cache layers:** |
| 87 | + |
| 88 | +1. **SQLite Database** (`databaseCache.ts`) |
| 89 | + - Persistent local storage |
| 90 | + - Survives app restarts |
| 91 | + - Updated by: `updateSQLiteCache()` |
| 92 | + |
| 93 | +2. **AsyncStorage** (`useNotesLoader.ts`) |
| 94 | + - UI preview cache |
| 95 | + - Fast rendering |
| 96 | + - Cleared by: `clearAsyncStorageCaches()` |
| 97 | + |
| 98 | +3. **In-Memory Refs** (`useNotesLoader.ts`) |
| 99 | + - Ultra-fast mode (< 30 seconds) |
| 100 | + - Skip decryption |
| 101 | + - Auto-invalidated on count mismatch |
| 102 | + |
| 103 | +### Cache Sync Strategy |
| 104 | + |
| 105 | +**Create/Update Operations:** |
| 106 | +```typescript |
| 107 | +await syncAllCaches(note, shouldClearAsyncStorage); |
| 108 | +// 1. Always update SQLite cache |
| 109 | +// 2. Optionally clear AsyncStorage (for delete/archive/move) |
| 110 | +``` |
| 111 | + |
| 112 | +**When to Clear AsyncStorage:** |
| 113 | +- ✅ Note deleted (`deleted: true`) |
| 114 | +- ✅ Note archived (`archived: true`) |
| 115 | +- ✅ Note moved to different folder (`folderId` changed) |
| 116 | +- ❌ Note content edited (keep cache for performance) |
| 117 | + |
| 118 | +## Migration Guide |
| 119 | + |
| 120 | +No changes needed! The modular structure maintains the same API: |
| 121 | + |
| 122 | +```typescript |
| 123 | +const api = useApiService(); |
| 124 | + |
| 125 | +// All operations work the same |
| 126 | +await api.createNote({ title, content }); |
| 127 | +await api.updateNote(noteId, { deleted: true }); |
| 128 | +await api.deleteNote(noteId); |
| 129 | +``` |
| 130 | + |
| 131 | +## Future Enhancements |
| 132 | + |
| 133 | +1. **Extract Remaining Modules:** |
| 134 | + - [ ] `queries.ts` - Query operations |
| 135 | + - [ ] `visibility.ts` - Hide/unhide operations |
| 136 | + - [ ] `attachments.ts` - File operations |
| 137 | + |
| 138 | +2. **Add Unit Tests:** |
| 139 | + - [ ] Test CRUD operations independently |
| 140 | + - [ ] Test cache synchronization logic |
| 141 | + - [ ] Mock network/database for faster tests |
| 142 | + |
| 143 | +3. **Performance Optimizations:** |
| 144 | + - [ ] Batch cache updates |
| 145 | + - [ ] Optimize background refresh |
| 146 | + - [ ] Add request deduplication |
| 147 | + |
| 148 | +## Troubleshooting |
| 149 | + |
| 150 | +### "Deleted notes reappearing" |
| 151 | +**Root Cause:** Cache synchronization issue |
| 152 | +**Fixed in:** `crud.ts` + `cache-sync.ts` |
| 153 | +- `updateNote()` now syncs all caches |
| 154 | +- AsyncStorage cleared for location changes |
| 155 | +- SQLite always updated |
| 156 | + |
| 157 | +### "New notes not appearing" |
| 158 | +**Root Cause:** Ultra-fast mode using stale cache |
| 159 | +**Fixed in:** `useNotesLoader.ts` |
| 160 | +- Count mismatch detection |
| 161 | +- Skip ultra-fast mode if counts differ |
| 162 | + |
| 163 | +### "Notes showing [ENCRYPTED]" |
| 164 | +**Root Cause:** Background refresh storing encrypted content |
| 165 | +**Fixed in:** `core.ts` |
| 166 | +- Always cache decrypted content |
| 167 | +- Removed preference check |
0 commit comments