Simply set and get configuration from a json file for your Electron app
This is the 2.x.x tree.
For 1.x.x code and documentation please refer to the 1.x.x tree.
See UPGRADE.md for an upgrade guide.
This package can be used from main and renderer process.
npm install --save electron-json-config@beta
yarn add electron-json-config@beta
const config = require('electron-json-config').factory();
config.set('foo', 'bar');
console.log(config.get('foo')); // bar
import { factory } from 'electron-json-config';
const config = factory();
config.set('foo', 'bar');
console.log(config.get('foo')); // bar
type Key = string | Array<string>;
A key
can be :
- a classic string key
eg:'foo'
- a dotted string multi level key
eg:'foo.bar'
- an array of string representing a multi level key
eg:['foo', 'bar']
interface Storable {
[key: string]: Storable | any;
}
Description:
Create an instance of Config and returns it.
If an instance with the same key exist, returns this instance instead.
If file is specified, the configuration will be saved to that file instead of the default app.getPath('userData') + '/config.json'
.
If key is specified, the requested instance will be saved under une given key instead of the default userData
.
Examples:
// file: app.getPath('userData') + '/config.json'
// key: 'userData'
factory();
// file: '/data/test.json'
// key: '/data/test.json'
factory('/data/test.json');
// file: '/data/test.json'
// key: 'test'
factory('/data/test.json', 'test');
// file: app.getPath('userData') + '/config.json'
// key: 'test'
factory(undefined, 'test');
Parameters:
Name | Type | Default |
---|---|---|
file? |
string | app.getPath('userData') + '/config.json' |
key? |
string | `key |
Returns: void
The config class is a set of wrappers and helpers providing access to configuration and file synchronization.
Parameters:
Name | Type |
---|---|
file |
string |
data |
Storable |
Returns: Config
Description: Returns the name of the file the config is stored in.
Returns: string
Description: Returns all the data currently saved.
Returns: Storable
Description: Removes the key and its value from the config file.
Parameters:
Name | Type |
---|---|
key |
Key |
Returns: void
Description: Removes all the keys specified and theirs value from the config file.
Parameters:
Name | Type |
---|---|
keys |
Array<Key> |
Returns: void
Description: Returns the value associated with the key, undefined otherwise. You can specify a default value returned in case the key does not exists.
Parameters:
Name | Type |
---|---|
key |
Key |
defaultValue? |
T |
Returns: T | undefined
Description: Checks if a key exists.
Parameters:
Name | Type |
---|---|
key |
Key |
Returns: boolean
Description: If key
is omitted, returns an array containing all keys in the config file.
If key
is provided, returns an array containing all sub keys in the key object.
Parameters:
Name | Type |
---|---|
key? |
Key |
Description: Removes all data from the config file.
Returns: void
Description: Sets a key with the specified value. Overwrites the value, if the key already exists.
Parameters:
Name | Type |
---|---|
key |
Key |
value |
Storable | T |
Returns: void
Description: Like .set() but sets multiple keys in a single call.
Parameters:
Name | Type |
---|---|
items |
{ [key: string]: Storable | T } |
Returns: void