### Electron-Store Version Migrations Source: https://context7.com/sindresorhus/electron-store/llms.txt Demonstrates how to set up version migrations for electron-store to transform stored data when the app version changes. It includes examples for specific versions and semver ranges. Dependencies: electron-store. ```javascript import Store from 'electron-store'; const store = new Store({ migrations: { '1.0.0': store => { // Migrate from pre-1.0 structure const oldUsername = store.get('username'); if (oldUsername) { store.set('user.name', oldUsername); store.delete('username'); } }, '2.0.0': store => { // Rename settings key const oldSettings = store.get('settings'); if (oldSettings) { store.set('preferences', oldSettings); store.delete('settings'); } }, '>=2.1.0': store => { // Semver ranges work too store.set('configVersion', '2.1+'); } }, beforeEachMigration: (store, context) => { console.log(`Migrating from ${context.fromVersion} to ${context.toVersion}`); } }); ``` -------------------------------- ### Initialize Renderer Process Communication Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Required setup for using electron-store within an Electron renderer process by initializing IPC channels. ```javascript // Main process import Store from 'electron-store'; Store.initRenderer(); // Renderer process import Store from 'electron-store'; const store = new Store(); store.set('unicorn', '🦄'); ``` -------------------------------- ### Create Electron-Store Instance Source: https://context7.com/sindresorhus/electron-store/llms.txt Demonstrates how to create a new electron-store instance with optional defaults. The store persists data to a JSON file in the application's user data directory. It shows basic usage of getting a value and accessing the store path. ```javascript import Store from 'electron-store'; // Basic store with defaults const store = new Store({ defaults: { windowBounds: { width: 800, height: 600 }, theme: 'light', recentFiles: [] } }); console.log(store.get('theme')); //=> 'light' console.log(store.path); //=> '/Users/username/Library/Application Support/MyApp/config.json' ``` -------------------------------- ### Install electron-store Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Command to install the electron-store package via npm. ```shell npm install electron-store ``` -------------------------------- ### Electron-Store: beforeEachMigration Callback Example Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Demonstrates the usage of the `beforeEachMigration` option in electron-store. This callback function is executed before each migration step, receiving the store instance and migration context. It's useful for logging or preparing data during migrations. ```javascript import Store from 'electron-store'; console.log = someLogger.log; const mainConfig = new Store({ beforeEachMigration: (store, context) => { console.log(`[main-config] migrate from ${context.fromVersion} → ${context.toVersion}`); }, migrations: { '0.4.0': store => { store.set('debugPhase', true); } } }); const secondConfig = new Store({ beforeEachMigration: (store, context) => { console.log(`[second-config] migrate from ${context.fromVersion} → ${context.toVersion}`); }, migrations: { '1.0.1': store => { store.set('debugPhase', true); } } }); ``` -------------------------------- ### Getting Values with `.get()` Source: https://context7.com/sindresorhus/electron-store/llms.txt Details how to retrieve values from the store using `.get()`, including the use of optional default values and accessing nested properties. ```APIDOC ## Getting Values with `.get()` ### Description Retrieves values from the store. Accepts an optional default value to return if the key doesn't exist. Supports dot-notation for nested properties. ### Method `store.get(key: string, defaultValue?: any): any` ### Parameters #### Path Parameters - **key** (string) - Required - The key of the value to retrieve. - **defaultValue** (any) - Optional - The value to return if the key does not exist. ### Request Example ```javascript import Store from 'electron-store'; const store = new Store({ defaults: { volume: 0.8 } }); // Get existing value console.log(store.get('volume')); //=> 0.8 // Get with default for non-existent key const username = store.get('username', 'Anonymous'); console.log(username); //=> 'Anonymous' // Get nested property store.set('settings.audio.muted', false); console.log(store.get('settings.audio.muted')); //=> false // Get entire nested object console.log(store.get('settings')); //=> { audio: { muted: false } } ``` ### Response #### Success Response (200) - **value** (any) - The retrieved value or the default value if the key is not found. #### Response Example ```javascript // Example output from request example //=> 0.8 //=> 'Anonymous' //=> false //=> { audio: { muted: false } } ``` ``` -------------------------------- ### Get Values from Electron-Store Source: https://context7.com/sindresorhus/electron-store/llms.txt Explains how to retrieve values from the electron-store using the `.get()` method. It supports fetching existing values, using a default value for non-existent keys, and accessing nested properties or entire nested objects. This is essential for loading saved configurations. ```javascript import Store from 'electron-store'; const store = new Store({ defaults: { volume: 0.8 } }); // Get existing value console.log(store.get('volume')); //=> 0.8 // Get with default for non-existent key const username = store.get('username', 'Anonymous'); console.log(username); //=> 'Anonymous' // Get nested property store.set('settings.audio.muted', false); console.log(store.get('settings.audio.muted')); //=> false // Get entire nested object console.log(store.get('settings')); //=> { audio: { muted: false } } ``` -------------------------------- ### Basic Store Usage Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Demonstrates initializing a store, setting values, accessing nested properties with dot-notation, and deleting keys. ```javascript import Store from 'electron-store'; const store = new Store(); store.set('unicorn', '🦄'); console.log(store.get('unicorn')); //=> '🦄' // Use dot-notation to access nested properties store.set('foo.bar', true); console.log(store.get('foo')); //=> {bar: true} store.delete('unicorn'); console.log(store.get('unicorn')); //=> undefined ``` -------------------------------- ### Store Instance Creation Source: https://context7.com/sindresorhus/electron-store/llms.txt Demonstrates how to create a new electron-store instance, optionally configuring defaults. ```APIDOC ## Store Instance Creation ### Description Creates a new store instance that persists data to a JSON file. Supports configuration of defaults, schema validation, encryption, and custom storage locations. ### Method `new Store(options?: Options)` ### Parameters #### Options - **defaults** (object) - Optional - Default values for the store. - **schema** (object) - Optional - JSON schema for validation. - **encryptionKey** (string | Buffer) - Optional - Key for data encryption. - **name** (string) - Optional - Name of the config file. - **cwd** (string) - Optional - Custom directory for the config file. ### Request Example ```javascript import Store from 'electron-store'; const store = new Store({ defaults: { windowBounds: { width: 800, height: 600 }, theme: 'light' } }); ``` ### Response #### Success Response (200) - **storeInstance** (object) - An instance of the Store class. #### Response Example ```javascript // store instance is returned console.log(store.get('theme')); //=> 'light' console.log(store.path); //=> '/Users/username/Library/Application Support/MyApp/config.json' ``` ``` -------------------------------- ### Electron-Store Renderer Process Usage Source: https://context7.com/sindresorhus/electron-store/llms.txt Shows how to use electron-store in the renderer process by first initializing IPC communication in the main process using `Store.initRenderer()`. The store then functions identically in both processes. Dependencies: electron-store, electron. ```javascript // main.js (Main Process) import { app, BrowserWindow } from 'electron'; import Store from 'electron-store'; // Initialize IPC handlers for renderer process stores Store.initRenderer(); app.whenReady().then(() => { const win = new BrowserWindow({ webPreferences: { nodeIntegration: true, contextIsolation: false } }); win.loadFile('index.html'); }); // renderer.js (Renderer Process) import Store from 'electron-store'; const store = new Store(); // Works identically to main process usage store.set('windowState', { maximized: false, fullscreen: false }); console.log(store.get('windowState')); //=> { maximized: false, fullscreen: false } ``` -------------------------------- ### Electron-Store IPC for Main-Renderer Sync Source: https://context7.com/sindresorhus/electron-store/llms.txt Demonstrates how to synchronize store values between the main and renderer processes using Electron's IPC `invoke`/`handle` pattern when the store is initialized in the main process. Dependencies: electron-store, electron. ```javascript // main.js import { app, BrowserWindow, ipcMain } from 'electron'; import Store from 'electron-store'; const store = new Store({ defaults: { theme: 'system', fontSize: 14 } }); // Handle get requests from renderer ipcMain.handle('store:get', (event, key) => { return store.get(key); }); // Handle set requests from renderer ipcMain.handle('store:set', (event, key, value) => { store.set(key, value); }); // Handle getting all values ipcMain.handle('store:getAll', () => { return store.store; }); // renderer.js (with contextBridge/preload) const theme = await window.electronAPI.storeGet('theme'); console.log(theme); //=> 'system' await window.electronAPI.storeSet('fontSize', 16); const allSettings = await window.electronAPI.storeGetAll(); console.log(allSettings); //=> { theme: 'system', fontSize: 16 } ``` -------------------------------- ### Setting Values with `.set()` Source: https://context7.com/sindresorhus/electron-store/llms.txt Explains how to store values in the config file using the `.set()` method, supporting single key-value pairs, multiple values, and dot-notation for nested properties. ```APIDOC ## Setting Values with `.set()` ### Description Stores values in the config file. Accepts either a key-value pair or an object for setting multiple values at once. Supports dot-notation for nested properties. ### Method `store.set(key: string | object, value?: any): void` ### Parameters #### Path Parameters - **key** (string | object) - Required - The key or an object of key-value pairs to set. - **value** (any) - Optional - The value to set if the key is a string. ### Request Example ```javascript import Store from 'electron-store'; const store = new Store(); // Set a single value store.set('unicorn', '🦄'); // Set nested properties using dot notation store.set('user.name', 'Alice'); store.set('user.preferences.notifications', true); // Set multiple values at once store.set({ windowBounds: { x: 100, y: 100, width: 1200, height: 800 }, lastOpened: Date.now(), isFirstLaunch: false }); ``` ### Response #### Success Response (200) - **void** - This method does not return a value. #### Response Example ```javascript console.log(store.get('user')); //=> { name: 'Alice', preferences: { notifications: true } } ``` ``` -------------------------------- ### Resetting Store Values to Defaults Source: https://context7.com/sindresorhus/electron-store/llms.txt Demonstrates how to use the .reset() method to revert specific configuration keys back to their defined default values. This is useful for restoring user settings without clearing the entire store. ```javascript import Store from 'electron-store'; const store = new Store({ defaults: { volume: 0.8, theme: 'light', notifications: true, language: 'en' } }); store.set('volume', 0.2); store.set('theme', 'dark'); store.set('notifications', false); store.reset('volume', 'notifications'); console.log(store.get('volume')); console.log(store.get('theme')); console.log(store.get('notifications')); ``` -------------------------------- ### Electron-Store Property Access Source: https://context7.com/sindresorhus/electron-store/llms.txt Explains how to access useful properties and methods of the electron-store instance, including `.store` for all data, `.path` for file location, `.size` for item count, and `.openInEditor()` to open the config file. Dependencies: electron-store. ```javascript import Store from 'electron-store'; const store = new Store({ name: 'user-preferences' }); store.set('theme', 'dark'); store.set('language', 'en'); // Get the file path console.log(store.path); //=> '/Users/username/Library/Application Support/MyApp/user-preferences.json' // Get item count console.log(store.size); //=> 2 // Get all data as an object console.log(store.store); //=> { theme: 'dark', language: 'en' } // Replace all data store.store = { theme: 'light', notifications: true }; console.log(store.store); //=> { theme: 'light', notifications: true } // Open config file in user's default editor await store.openInEditor(); // Iterate over all entries for (const [key, value] of store) { console.log(`${key}: ${value}`); } //=> theme: light //=> notifications: true ``` -------------------------------- ### Securing Data with Encryption Source: https://context7.com/sindresorhus/electron-store/llms.txt Illustrates how to enable AES encryption for the store file. While this obscures data on disk, it is intended for privacy rather than high-security encryption since the key resides in the source code. ```javascript import Store from 'electron-store'; const store = new Store({ encryptionKey: 'my-secret-encryption-key', encryptionAlgorithm: 'aes-256-gcm' }); store.set('apiToken', 'sensitive-token-value'); console.log(store.get('apiToken')); ``` -------------------------------- ### Set Values in Electron-Store Source: https://context7.com/sindresorhus/electron-store/llms.txt Illustrates how to use the `.set()` method to store values in the electron-store. It supports setting single key-value pairs, multiple values at once using an object, and nested properties via dot-notation. This is useful for saving user preferences or application state. ```javascript import Store from 'electron-store'; const store = new Store(); // Set a single value store.set('unicorn', '🦄'); // Set nested properties using dot notation store.set('user.name', 'Alice'); store.set('user.preferences.notifications', true); // Set multiple values at once store.set({ windowBounds: { x: 100, y: 100, width: 1200, height: 800 }, lastOpened: Date.now(), isFirstLaunch: false }); console.log(store.get('user')); //=> { name: 'Alice', preferences: { notifications: true } } ``` -------------------------------- ### Watching for Key-Specific Changes Source: https://context7.com/sindresorhus/electron-store/llms.txt Explains how to use .onDidChange() to monitor a specific configuration key for updates. It returns an unsubscribe function to stop listening when no longer needed. ```javascript import Store from 'electron-store'; const store = new Store(); const unsubscribe = store.onDidChange('theme', (newValue, oldValue) => { console.log(`Theme changed from ${oldValue} to ${newValue}`); }); store.set('theme', 'light'); store.set('theme', 'dark'); unsubscribe(); ``` -------------------------------- ### Customize Serialization Format Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Shows how to use custom serialization formats like YAML by providing custom serialize and deserialize functions. ```javascript import Store from 'electron-store'; import yaml from 'js-yaml'; const store = new Store({ fileExtension: 'yaml', serialize: yaml.safeDump, deserialize: yaml.safeLoad }); ``` -------------------------------- ### Access and Replace Store Data Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Demonstrates how to retrieve the entire store object or replace it entirely. ```javascript import Store from 'electron-store'; const store = new Store(); store.store = {hello: 'world'}; ``` -------------------------------- ### Subscribe to Store Changes Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Explains how to watch for changes on specific keys or the entire store object using callback functions. ```javascript const unsubscribeKey = store.onDidChange(key, (newValue, oldValue) => {}); // To stop watching: unsubscribeKey(); const unsubscribeAll = store.onDidAnyChange((newValue, oldValue) => {}); // To stop watching: unsubscribeAll(); ``` -------------------------------- ### Accessing Store Values in Renderer Process with Electron IPC Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Demonstrates how to set up an IPC handler in the main process to retrieve store values and how to invoke this handler from the renderer process. This is useful for sharing data between Electron's main and renderer threads. ```javascript ipcMain.handle('getStoreValue', (event, key) => { return store.get(key); }); ``` ```javascript const foo = await ipcRenderer.invoke('getStoreValue', 'foo'); ``` -------------------------------- ### Watching Global Store Changes Source: https://context7.com/sindresorhus/electron-store/llms.txt Demonstrates the .onDidAnyChange() method, which notifies the application whenever any part of the configuration object is modified, providing both old and new states. ```javascript import Store from 'electron-store'; const store = new Store({ defaults: { count: 0 } }); const unsubscribe = store.onDidAnyChange((newValue, oldValue) => { console.log('Config changed:', JSON.stringify(oldValue), JSON.stringify(newValue)); }); store.set('count', 1); store.set('user', 'Alice'); unsubscribe(); ``` -------------------------------- ### Configure Dot Notation Access Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Demonstrates how to toggle the ability to access nested object properties using dot notation in keys. ```javascript import Store from 'electron-store'; const store = new Store(); store.set({foo: {bar: {foobar: '🦄'}}}); console.log(store.get('foo.bar.foobar')); const storeNoDot = new Store({accessPropertiesByDotNotation: false}); storeNoDot.set({'foo.bar.foobar': '🦄'}); console.log(storeNoDot.get('foo.bar.foobar')); ``` -------------------------------- ### Append Items to Arrays Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Shows how to append values to an array within the store, creating the array if it does not already exist. ```javascript store.set('items', [{name: 'foo'}]); store.appendToArray('items', {name: 'bar'}); console.log(store.get('items')); store.appendToArray('newItems', 'first'); console.log(store.get('newItems')); ``` -------------------------------- ### Watching All Changes with .onDidAnyChange() Source: https://context7.com/sindresorhus/electron-store/llms.txt The `.onDidAnyChange()` method monitors the entire store for any modifications, providing the complete configuration object before and after the change. ```APIDOC ## Watching All Changes with `.onDidAnyChange()` ### Description The `.onDidAnyChange()` method watches the entire store for any changes, providing the complete config object before and after the change. ### Method ```javascript store.onDidAnyChange(callback: (newValue: object, oldValue: object) => void): () => void ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import Store from 'electron-store'; const store = new Store({ defaults: { count: 0 } }); const unsubscribe = store.onDidAnyChange((newValue, oldValue) => { console.log('Config changed:'); console.log('Old:', JSON.stringify(oldValue)); console.log('New:', JSON.stringify(newValue)); }); store.set('count', 1); //=> Config changed: //=> Old: {"count":0} //=> New: {"count":1} store.set('user', 'Alice'); //=> Config changed: //=> Old: {"count":1} //=> New: {"count":1,"user":"Alice"} // Stop watching unsubscribe(); ``` ### Response #### Success Response (200) N/A (This is a method call, not an HTTP request) #### Response Example N/A ``` -------------------------------- ### Store Migrations Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Defines migration handlers to update store data when the application version changes. ```javascript import Store from 'electron-store'; const store = new Store({ migrations: { '0.0.1': store => { store.set('debugPhase', true); }, '1.0.0': store => { store.delete('debugPhase'); store.set('phase', '1.0.0'); }, '1.0.2': store => { store.set('phase', '1.0.2'); }, '>=2.0.0': store => { store.set('phase', '>=2.0.0'); } } }); ``` -------------------------------- ### Resetting Values with .reset() Source: https://context7.com/sindresorhus/electron-store/llms.txt The `.reset()` method allows you to reset specific keys to their default values, as defined in the `defaults` or `schema` options. ```APIDOC ## Resetting Values with `.reset()` ### Description The `.reset()` method resets specific keys to their default values as defined by the `defaults` or `schema` option. ### Method ```javascript store.reset(...keys: string[]) ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import Store from 'electron-store'; const store = new Store({ defaults: { volume: 0.8, theme: 'light', notifications: true, language: 'en' } }); // User changes settings store.set('volume', 0.2); store.set('theme', 'dark'); store.set('notifications', false); // Reset specific settings to defaults store.reset('volume', 'notifications'); console.log(store.get('volume')); //=> 0.8 console.log(store.get('theme')); //=> 'dark' (unchanged) console.log(store.get('notifications')); //=> true ``` ### Response #### Success Response (200) N/A (This is a method call, not an HTTP request) #### Response Example N/A ``` -------------------------------- ### Enforcing Data Integrity with JSON Schema Source: https://context7.com/sindresorhus/electron-store/llms.txt Shows how to define a schema to validate configuration values before they are saved. Invalid data will trigger an error, ensuring the store maintains expected types and constraints. ```javascript import Store from 'electron-store'; const store = new Store({ schema: { volume: { type: 'number', minimum: 0, maximum: 1, default: 0.5 }, theme: { type: 'string', enum: ['light', 'dark', 'system'], default: 'system' }, windowBounds: { type: 'object', properties: { width: { type: 'integer', minimum: 400 }, height: { type: 'integer', minimum: 300 } }, required: ['width', 'height'] }, email: { type: 'string', format: 'email' } } }); store.set('volume', 0.75); try { store.set('volume', 1.5); } catch (error) { console.error(error.message); } ``` -------------------------------- ### Electron-Store Custom Serialization Source: https://context7.com/sindresorhus/electron-store/llms.txt Illustrates how to use custom serialization and deserialization functions with electron-store to store data in formats other than JSON, such as YAML. Dependencies: electron-store, js-yaml. ```javascript import Store from 'electron-store'; import yaml from 'js-yaml'; const store = new Store({ fileExtension: 'yaml', serialize: yaml.dump, deserialize: yaml.load }); store.set('database', { host: 'localhost', port: 5432, name: 'myapp' }); // config.yaml contains: // database: // host: localhost // port: 5432 // name: myapp console.log(store.get('database.host')); //=> 'localhost' ``` -------------------------------- ### Check and Delete Values in Electron-Store Source: https://context7.com/sindresorhus/electron-store/llms.txt Details the methods for managing data within the electron-store: `.has()` to check for key existence, `.delete()` to remove specific keys (including nested ones), and `.clear()` to remove all data and reset to defaults. This is crucial for data management and cleanup. ```javascript import Store from 'electron-store'; const store = new Store({ defaults: { theme: 'system' } }); store.set('apiKey', 'secret-key-123'); store.set('user.session', 'abc123'); // Check if keys exist console.log(store.has('apiKey')); //=> true console.log(store.has('nonExistent')); //=> false // Delete specific keys store.delete('apiKey'); console.log(store.has('apiKey')); //=> false // Delete nested key store.delete('user.session'); // Clear all data (resets to defaults) store.clear(); console.log(store.get('theme')); //=> 'system' (reset to default) ``` -------------------------------- ### Append to Arrays in Electron-Store Source: https://context7.com/sindresorhus/electron-store/llms.txt Demonstrates the `.appendToArray()` method for adding items to an array stored under a specific key in electron-store. If the key does not exist, it initializes it as a new array. This is useful for managing lists like recent files or tags. ```javascript import Store from 'electron-store'; const store = new Store(); // Initialize with an array store.set('recentFiles', [ { path: '/documents/file1.txt', opened: Date.now() } ]); // Append new items store.appendToArray('recentFiles', { path: '/documents/file2.txt', opened: Date.now() }); console.log(store.get('recentFiles')); //=> [ // { path: '/documents/file1.txt', opened: 1699123456789 }, // { path: '/documents/file2.txt', opened: 1699123456790 } // ] // Creates array if key doesn't exist store.appendToArray('tags', 'important'); store.appendToArray('tags', 'work'); console.log(store.get('tags')); //=> ['important', 'work'] ``` -------------------------------- ### Watching for Changes with .onDidChange() Source: https://context7.com/sindresorhus/electron-store/llms.txt The `.onDidChange()` method allows you to watch a specific key for changes and execute a callback function when its value is modified. It returns an unsubscribe function. ```APIDOC ## Watching for Changes with `.onDidChange()` ### Description The `.onDidChange()` method watches a specific key for changes and calls a callback when the value changes. Returns an unsubscribe function. ### Method ```javascript store.onDidChange(key: string, callback: (newValue: any, oldValue: any) => void): () => void ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import Store from 'electron-store'; const store = new Store(); // Watch a specific key const unsubscribe = store.onDidChange('theme', (newValue, oldValue) => { console.log(`Theme changed from ${oldValue} to ${newValue}`); // Update UI, apply theme, etc. }); store.set('theme', 'light'); //=> Theme changed from undefined to light store.set('theme', 'dark'); //=> Theme changed from light to dark // Stop watching unsubscribe(); store.set('theme', 'system'); // No callback triggered ``` ### Response #### Success Response (200) N/A (This is a method call, not an HTTP request) #### Response Example N/A ``` -------------------------------- ### Checking and Deleting Values Source: https://context7.com/sindresorhus/electron-store/llms.txt Covers methods for checking key existence (`.has()`), removing specific keys (`.delete()`), and clearing all data (`.clear()`). ```APIDOC ## Checking and Deleting Values ### Description Provides methods to check if a key exists (`.has()`), remove a specific key (`.delete()`), and remove all data (`.clear()`), which resets the store to its defaults if defined. ### Methods - `store.has(key: string): boolean` - `store.delete(key: string): void` - `store.clear(): void` ### Parameters #### Path Parameters - **key** (string) - Required - The key to check or delete. ### Request Example ```javascript import Store from 'electron-store'; const store = new Store({ defaults: { theme: 'system' } }); store.set('apiKey', 'secret-key-123'); store.set('user.session', 'abc123'); // Check if keys exist console.log(store.has('apiKey')); //=> true console.log(store.has('nonExistent')); //=> false // Delete specific keys store.delete('apiKey'); console.log(store.has('apiKey')); //=> false // Delete nested key store.delete('user.session'); // Clear all data (resets to defaults) store.clear(); console.log(store.get('theme')); //=> 'system' (reset to default) ``` ### Response #### Success Response (200) - **void** - These methods do not return a value, except for `.has()` which returns a boolean. #### Response Example ```javascript // Example outputs from request example: //=> true //=> false //=> false //=> 'system' ``` ``` -------------------------------- ### Appending to Arrays with `.appendToArray()` Source: https://context7.com/sindresorhus/electron-store/llms.txt Illustrates the use of `.appendToArray()` to add items to an array stored at a specific key, creating the array if it doesn't exist. ```APIDOC ## Appending to Arrays with `.appendToArray()` ### Description Adds items to an array stored at a key. If the key doesn't exist, it creates a new array. This is useful for managing lists of items like recent files or tags. ### Method `store.appendToArray(key: string, value: any): void` ### Parameters #### Path Parameters - **key** (string) - Required - The key of the array to append to. - **value** (any) - Required - The item to append to the array. ### Request Example ```javascript import Store from 'electron-store'; const store = new Store(); // Initialize with an array store.set('recentFiles', [ { path: '/documents/file1.txt', opened: Date.now() } ]); // Append new items store.appendToArray('recentFiles', { path: '/documents/file2.txt', opened: Date.now() }); // Creates array if key doesn't exist store.appendToArray('tags', 'important'); store.appendToArray('tags', 'work'); ``` ### Response #### Success Response (200) - **void** - This method does not return a value. #### Response Example ```javascript console.log(store.get('recentFiles')); //=> [ // { path: '/documents/file1.txt', opened: 1699123456789 }, // { path: '/documents/file2.txt', opened: 1699123456790 } // ] console.log(store.get('tags')); //=> ['important', 'work'] ``` ``` -------------------------------- ### Store Schema Validation Source: https://github.com/sindresorhus/electron-store/blob/main/readme.md Configures a store with a JSON schema to enforce data types and constraints on stored properties. ```javascript import Store from 'electron-store'; const schema = { foo: { type: 'number', maximum: 100, minimum: 1, default: 50 }, bar: { type: 'string', format: 'url' } }; const store = new Store({schema}); console.log(store.get('foo')); //=> 50 store.set('foo', '1'); // [Error: Config schema violation: `foo` should be number] ``` -------------------------------- ### Data Encryption Source: https://context7.com/sindresorhus/electron-store/llms.txt Data encryption obscures stored data using AES encryption. It's important to note that this is for obscurity rather than strict security, as the encryption key is typically included in the source code. ```APIDOC ## Data Encryption ### Description Encryption obscures stored data using AES encryption. Note: This is for obscurity, not security, as the key is in the source code. ### Method ```javascript new Store({ encryptionKey: string, encryptionAlgorithm?: string }) ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import Store from 'electron-store'; const store = new Store({ encryptionKey: 'my-secret-encryption-key', encryptionAlgorithm: 'aes-256-gcm' // Options: 'aes-256-cbc', 'aes-256-gcm', 'aes-256-ctr' }); // Data is encrypted on disk but accessed normally in code store.set('apiToken', 'sensitive-token-value'); store.set('user', { id: 12345, email: 'user@example.com' }); console.log(store.get('apiToken')); //=> 'sensitive-token-value' // The config.json file contains encrypted data instead of plain JSON // With aes-256-gcm, any tampering with the file will cause decryption to fail ``` ### Response #### Success Response (200) N/A (This is a method call, not an HTTP request) #### Response Example N/A ``` -------------------------------- ### Schema Validation Source: https://context7.com/sindresorhus/electron-store/llms.txt JSON Schema validation ensures data integrity by validating values before they are stored. Invalid data will throw an error. ```APIDOC ## Schema Validation ### Description JSON Schema validation ensures data integrity by validating values before they're stored. Invalid data throws an error. ### Method ```javascript new Store({ schema: object }) store.set(key: string, value: any) ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```javascript import Store from 'electron-store'; const store = new Store({ schema: { volume: { type: 'number', minimum: 0, maximum: 1, default: 0.5 }, theme: { type: 'string', enum: ['light', 'dark', 'system'], default: 'system' }, windowBounds: { type: 'object', properties: { width: { type: 'integer', minimum: 400 }, height: { type: 'integer', minimum: 300 } }, required: ['width', 'height'] }, email: { type: 'string', format: 'email' } } }); // Valid values work fine store.set('volume', 0.75); store.set('theme', 'dark'); // Invalid values throw errors try { store.set('volume', 1.5); // Exceeds maximum } catch (error) { console.error(error.message); //=> 'Config schema violation: `volume` should be <= 1' } try { store.set('theme', 'invalid'); } catch (error) { console.error(error.message); //=> 'Config schema violation: `theme` should be equal to one of the allowed values' } ``` ### Response #### Success Response (200) N/A (This is a method call, not an HTTP request) #### Response Example N/A ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.