/** * Memorio Store Advanced Example * * This example shows advanced store (localStorage) operations. * * Run: npx ts-node examples/store-advanced.ts */ import 'memorio' // ============================================ // CHECK PERSISTENCE // ============================================ console.debug('=== Store Persistence Check ===') console.debug('Is persistent (survives restart):', store.isPersistent) // In browser: true (localStorage) // In Node.js/Deno: false (memory fallback) if (!store.isPersistent) { console.debug('⚠️ Warning: Using in-memory storage. Data will be lost on restart!') } // ============================================ // PERSIST USER PREFERENCES // ============================================ // Save user preferences store.set('preferences', { theme: 'dark', language: 'en', notifications: true, fontSize: 16 }) // ============================================ // CHECK AND LOAD PREFERENCES // ============================================ const savedPrefs = store.get('preferences') if (savedPrefs) { console.debug('Loaded preferences:', savedPrefs) } else { console.debug('No preferences found, using defaults') store.set('preferences', { theme: 'light', language: 'en' }) } // ============================================ // STORAGE QUOTA // ============================================ // Get storage size const currentSize = store.size() console.debug('Current storage size:', currentSize, 'bytes') // ============================================ // ALIAS METHODS // ============================================ // store.delete() is alias for store.remove() store.set('temp', 'value') store.delete('temp') // store.clearAll() is alias for store.removeAll() // store.clearAll() // ============================================ // ERROR HANDLING // ============================================ // Try-catch for large data try { // Store large data const largeData = { items: Array(1000).fill(null).map((_, i) => ({ id: i, data: 'x'.repeat(100) })) } store.set('largeData', largeData) console.debug('Large data stored successfully') } catch (error) { console.error('Storage full:', error) } // ============================================ // DATA SERIALIZATION // ============================================ // Store supports all JSON-serializable types store.set('string', 'hello') store.set('number', 42) store.set('boolean', true) store.set('array', [1, 2, 3]) store.set('object', { nested: { value: 'deep' } }) store.set('null', null) // Functions are not supported (logged as error) store.set('function', () => { }) // logs: "It's not secure to store functions." // ============================================ // PRACTICAL EXAMPLE: APP STATE // ============================================ // Save app state const appState = { lastPage: '/dashboard', sidebarOpen: true, recentFiles: ['file1.txt', 'file2.pdf'], lastSaved: Date.now() } store.set('appState', appState) // Load on next visit const restored = store.get('appState') if (restored) { console.debug('Restored app state:', restored.lastPage) } console.debug('Store advanced example complete!')