What is electron-store?
The electron-store npm package is a simple and efficient way to manage persistent user data in Electron applications. It provides a straightforward API for reading and writing JSON data, making it easy to store settings, preferences, and other types of data.
What are electron-store's main functionalities?
Basic Usage
This feature allows you to set, get, and delete key-value pairs in the store. It's useful for storing simple settings or preferences.
const Store = require('electron-store');
const store = new Store();
// Set a value
store.set('unicorn', '🦄');
// Get a value
console.log(store.get('unicorn'));
//=> '🦄'
// Delete a value
store.delete('unicorn');
Default Values
You can define default values for your store. These values will be used if no other value is set for the given key.
const Store = require('electron-store');
const store = new Store({
defaults: {
foo: 'bar'
}
});
console.log(store.get('foo'));
//=> 'bar'
Schema Validation
You can define a schema to validate the data being stored. This ensures that the data conforms to the expected format and constraints.
const Store = require('electron-store');
const store = new Store({
schema: {
foo: {
type: 'string',
maxLength: 10,
default: 'bar'
}
}
});
store.set('foo', 'baz');
console.log(store.get('foo'));
//=> 'baz'
Encryption
You can encrypt the data stored in the store by providing an encryption key. This adds a layer of security to sensitive data.
const Store = require('electron-store');
const store = new Store({
encryptionKey: 'mySecretEncryptionKey'
});
store.set('unicorn', '🦄');
console.log(store.get('unicorn'));
//=> '🦄'
Other packages similar to electron-store
conf
The 'conf' package is similar to electron-store in that it provides a simple way to manage configuration data. It supports schema validation, encryption, and default values. However, it is not specifically designed for Electron and can be used in any Node.js application.
lowdb
The 'lowdb' package is a small local JSON database for Node.js. It provides a simple API for reading and writing JSON data, similar to electron-store. However, it is more flexible and can be used for more complex data structures and queries.
node-persist
The 'node-persist' package is a simple, zero-dependency, key-value storage library for Node.js. It provides similar functionality to electron-store, including support for JSON data and encryption. However, it does not include schema validation or default values.
electron-store
Simple data persistence for your Electron app or module - Save and load user preferences, app state, cache, etc
Electron doesn't have a built-in way to persist user preferences and other data. This module handles that for you, so you can focus on building your app. The data is saved in a JSON file in app.getPath('userData')
.
You can use this module directly in both the main and renderer process.
This project was recently renamed from electron-config
.
Install
$ npm install electron-store
Usage
const Store = require('electron-store');
const store = new Store();
store.set('unicorn', '🦄');
console.log(store.get('unicorn'));
store.set('foo.bar', true);
console.log(store.get('foo'));
store.delete('unicorn');
console.log(store.get('unicorn'));
API
Store([options])
Returns a new instance.
options
defaults
Type: Object
Default data.
name
Type: string
Default: config
Name of the storage file (without extension).
This is useful if you want multiple storage files for your app. Or if you're making a reusable Electron module that persists some data, in which case you should not use the name config
.
cwd
Type: string
Default: app.getPath('userData')
Storage file location. Don't specify this unless absolutely necessary!
If a relative path, it's relative to the default cwd. For example, {cwd: 'unicorn'}
would result in a storage file in ~/Library/Application Support/App Name/unicorn
.
Instance
You can use dot-notation in a key
to access nested properties.
The instance is iterable
so you can use it directly in a for…of
loop.
.set(key, value)
Set an item.
.set(object)
Set multiple items at once.
.get(key, [defaultValue])
Get an item or defaultValue
if the item does not exist.
.has(key)
Check if an item exists.
.delete(key)
Delete an item.
.clear()
Delete all items.
.size
Get the item count.
.store
Get all the data as an object or replace the current data with an object:
conf.store = {
hello: 'world'
};
.path
Get the path to the storage file.
Related
License
MIT © Sindre Sorhus