Easily write and read user settings in Electron apps
Electron lacks an easy way to persist and read user settings for your application. electron-json-storage implements an API somehow similar to localStorage to write and read JSON objects to/from the operating system application data directory, as defined by app.getPath('userData').
Related modules:
Install electron-json-storage by running:
$ npm install --save electron-json-storageYou can require this module from either the main or renderer process (with and without remote).
If the key doesn't exist in the user data, an empty object is returned.
Also notice that the .json extension is added automatically, but it's
ignored if you pass it yourself.
Passing an extension other than .json will result in a file created
with both extensions. For example, the key foo.data will result in a file
called foo.data.json.
Kind: static method of storage
Summary: Read user data
Access: public
| Param | Type | Description |
|---|---|---|
| key | String |
key |
| callback | function |
callback (error, data) |
Example
const storage = require('electron-json-storage');
storage.get('foobar', function(error, data) {
if (error) throw error;
console.log(data);
});This function returns an object with the data of all the passed keys. If one of the keys doesn't exist, an empty object is returned for it.
Kind: static method of storage
Summary: Read many user data keys
Access: public
| Param | Type | Description |
|---|---|---|
| keys | Array.<String> |
keys |
| callback | function |
callback (error, data) |
Example
const storage = require('electron-json-storage');
storage.getMany([ 'foobar', 'barbaz' ], function(error, data) {
if (error) throw error;
console.log(data.foobar);
console.log(data.barbaz);
});This function returns an empty object if there is no data to be read.
Kind: static method of storage
Summary: Read all user data
Access: public
| Param | Type | Description |
|---|---|---|
| callback | function |
callback (error, data) |
Example
const storage = require('electron-json-storage');
storage.getAll(function(error, data) {
if (error) throw error;
console.log(data);
});Kind: static method of storage
Summary: Write user data
Access: public
| Param | Type | Description |
|---|---|---|
| key | String |
key |
| json | Object |
json object |
| callback | function |
callback (error) |
Example
const storage = require('electron-json-storage');
storage.set('foobar', { foo: 'bar' }, function(error) {
if (error) throw error;
});Kind: static method of storage
Summary: Write multiple sets of user data
Access: public
| Param | Type | Description |
|---|---|---|
| keys | Object |
json object |
| callback | function |
callback (error, finished) |
Example
const storage = require('electron-json-storage');
storage.setMany({ foo: { bar: 'baz' }, baz: { bar: 'foo' } }, function(error, finished) {
if (error) {
console.log('Error saving ' + error.key);
throw error.error;
}
if (finished) console.log('finished');
});The error property of the callback is an object with two properties:
error(object), the error objectkey(string), the key that failed to save that failed to save).
This function will set each key one by one, for each error that occurs the callback will be invoked. Therefore the finished
parameter of the callback indicates if the function has finished saving all the data.
Kind: static method of storage
Summary: Check if a key exists
Access: public
| Param | Type | Description |
|---|---|---|
| key | String |
key |
| callback | function |
callback (error, hasKey) |
Example
const storage = require('electron-json-storage');
storage.has('foobar', function(error, hasKey) {
if (error) throw error;
if (hasKey) {
console.log('There is data stored as `foobar`');
}
});Kind: static method of storage
Summary: Get the list of saved keys
Access: public
| Param | Type | Description |
|---|---|---|
| callback | function |
callback (error, keys) |
Example
const storage = require('electron-json-storage');
storage.keys(function(error, keys) {
if (error) throw error;
for (var key of keys) {
console.log('There is a key called: ' + key);
}
});Notice this function does nothing, nor throws any error if the key doesn't exist.
Kind: static method of storage
Summary: Remove a key
Access: public
| Param | Type | Description |
|---|---|---|
| key | String |
key |
| callback | function |
callback (error) |
Example
const storage = require('electron-json-storage');
storage.remove('foobar', function(error) {
if (error) throw error;
});Kind: static method of storage
Summary: Clear all stored data
Access: public
| Param | Type | Description |
|---|---|---|
| callback | function |
callback (error) |
Example
const storage = require('electron-json-storage');
storage.clear(function(error) {
if (error) throw error;
});If you're having any problem, please raise an issue on GitHub and we'll be happy to help.
Run the test suite by doing:
$ npm test- Issue Tracker: github.com/electron-userland/electron-json-storage/issues
- Source Code: github.com/electron-userland/electron-json-storage
Before submitting a PR, please make sure that you include tests, and that jshint runs without any warning:
$ npm run-script lintThe project is licensed under the MIT license.