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.
const config = require('electron-json-config');
config.set('foo', 'bar');
console.log(config.get('foo')); // shows 'bar'All key can be a classic key (eg: foo) or a multiple level key with levels separated by . (eg: foo.bar)
Returns the name of the file the config is stored in.
Returns: String
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'); // Sets 'bar' under 'foo' key
config.set('anArray', [1, 2]); // Sets [1, 2] under 'anArray' key
config.set('the.answer', 42); // Sets 42 under 'answer' under 'the'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:
// Sets 'bar' under 'foo' key
// Sets 42 under 'answer' under 'the'
config.setBulk({
'foo': 'bar',
'the.answer': 42,
});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'); // true
config.has('bar'); // falseReturns 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'); // Sets 'bar' under 'foo' key
config.get('foo'); // Returns 'bar'
config.get('bar', 42); // Returns 42If 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(); // Returns ['foo', 'the']
config.keys('the'); // Returns ['answer']Returns an object with all the data currently saved.
Returns: Object
Example:
config.setBulk({
'foo': 'bar',
'the.answer': 42,
});
config.all();
/*
{
'foo': 'bar',
'the': {
'answer': 42
}
}
*/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'); // Sets 'bar' under 'foo' key
config.delete('foo'); // Removes key 'bar' and its valueRemoves 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,
});
// Remove keys 'foo' and 'answer'
config.deleteBulk(['foo', 'answer']);Removes all data from the config file.
Returns: void
Example:
config.purge(); // All keys are removed