electron-json-config
Simply set and get configuration from a json file for your Electron app
The config file (config.json
) is located in the path returned by app.getPath('userData')
.
This package can be used from browser and renderer process.
Usage
const config = require('electron-json-config');
config.set('foo', 'bar');
console.log(config.get('foo'));
Documentation
All key
can be a classic key (eg: foo
) or a multiple level key with levels separated by .
(eg: foo.bar
)
.file()
Returns the name of the file the config is stored in.
Returns: String
.set(key, value)
Sets a key with the specified value. Overwrites the value, if the key already exists..
Parameters | Type | Optional | Description |
---|
key | String | | The key to set |
value | * | | The value to set under the key |
Returns: void
Example:
config.set('foo', 'bar');
config.set('anArray', [1, 2]);
config.set('the.answer', 42);
.setBulk(items)
Like .set()
but sets multiple keys in a single call.
Parameters | Type | Optional | Description |
---|
items | Object | | An object whose attributes will become keys |
Returns: void
Example:
config.setBulk({
'foo': 'bar',
'the.answer': 42,
});
.has(key)
Checks if a key exists.
Parameters | Type | Optional | Description |
---|
key | String | | The name of a key to test existence |
Returns: Boolean
Example:
config.set('foo', 'bar');
config.has('foo');
config.has('bar');
.get(key[, defaultValue])
Returns the value associated with the key, undefined
otherwise.
You can specify a default value returned in case the key does not exists.
Parameters | Type | Optional | Description |
---|
key | String | | The name of the key to get |
defaultValue | * | ✓ | The value to return in case value does not exists |
Returns: *
Example:
config.set('foo', 'bar');
config.get('foo');
config.get('bar', 42);
.keys([key])
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 | Type | Optional | Description |
---|
key | String | ✓ | The name of a key to get sub keys |
Returns: Array<String>
Example:
config.setBulk({
'foo': 'bar',
'the.answer': 42,
});
config.keys();
config.keys('the');
.all()
Returns an object with all the data currently saved.
Returns: Object
Example:
config.setBulk({
'foo': 'bar',
'the.answer': 42,
});
config.all();
.delete(key)
Removes the key and its value from the config file.
Parameters | Type | Optional | Description |
---|
key | String | | The name of a key to delete |
Returns: void
Example:
config.set('foo', 'bar');
config.delete('foo');
.deleteBulk(keys)
Removes all the keys specified and theirs value from the config file.
Parameters | Type | Optional | Description |
---|
keys | Array<String> | | An array of keys to remove |
Returns: void
Example:
config.setBulk({
'foo': 'bar',
'the.answer': 42,
});
config.deleteBulk(['foo', 'answer']);
.purge()
Removes all data from the config file.
Returns: void
Example:
config.purge();