Class: KeyValues ​
KeyValues is a class that provides a simple key-value storage system. It allows you to set, get, check existence, and remove key-value pairs in a JSON file. It supports nested keys and complex data types, and provides both asynchronous and synchronous methods
It is designed to be used in Node.js applications and can be easily configured to use a custom directory and file name for storing key-value pairs.
Author ​
Heliomar Marques
Example ​
// Create a new instance of KeyValues with custom options
const keyValues = new KeyValues({
fileName: 'config.json',
prettify: true
});
// Set a key-value pair
await keyValues.set('color.name', 'sapphire');
// Get the value at a specific key path
const value = await keyValues.get('color.name');
// Check if a key path exists
const exists = await keyValues.has('color.name');
// Remove a key-value pair
await keyValues.unset('color.name');
Constructors ​
Constructor ​
new KeyValues(options?): KeyValues;
Sets the configuration for KeyValues Storage's. To reset to defaults, use [[reset|reset()]].
Defaults:
{
atomicSave: true,
fileName: 'keyvalues.json',
numSpaces: 2,
prettify: false
}
Parameters ​
options? ​
Partial
<Options
>
Options The custom configuration to use.
Returns ​
KeyValues
Example ​
Update the filename to config.json
and prettify the output.
new KeyValues({
fileName: 'config.json',
prettify: true
});
Methods ​
file() ​
file(): string;
Returns the path to the json file.
In general, the json file is stored in then install location of your app's user data directory in a file called keyvalues.json
. The default user data directory varies by system.
- macOS -
~/Library/Application\ Support/<Your App>
- Windows -
%LOCALAPPDATA%/PROGRAMS/<Your App>
- Linux - Either
$XDG_CONFIG_HOME/<Your App>
or~/.config/<Your App>
Although it is not recommended, you may change the name or location of the keyvalues file using
new KeyValye({dir: 'newpath'})
Returns ​
string
The path to the keyvalues file. *
Example ​
Get the path to the keyvalues file.
keyValues.file();
// => c:/users/<userprofile>/appdata/local/programs/<AppName>/keyvalues.json
get() ​
Call Signature ​
get<T>(): Promise<T>;
Gets all key values. For sync, use [[getSync|getSync()]].
Type Parameters ​
T ​
T
extends valueTypes
Returns ​
Promise
<T
>
A promise which resolves with all key values.
Example ​
Gets all key values.
const obj = await get();
Call Signature ​
get<T>(keyPath): Promise<T>;
Gets the value at the given key path. For sync, use [[getSync|getSync()]].
Type Parameters ​
T ​
T
extends valueTypes
Parameters ​
keyPath ​
The key path of the property.
Returns ​
Promise
<T
>
A promise which resolves with the value at the given key path.
Examples ​
Get the value at color.name
.
// Given:
{
"color": {
"name": "cerulean",
"code": {
"rgb": [0, 179, 230],
"hex": "#003BE6"
}
}
}
const value = await keyValues.get('color.name');
// => "cerulean"
Get the value at color.code.hex
.
const hex = await keyValues.get('color.color.hex');
// => "#003BE6"
Get the value at color.hue
.
const h = 'hue';
const value = await keyValues.get(['color', h]);
// => undefined
Get the value at color.code.rgb[1]
.
const h = 'hue';
const value = await keyValues.get('color.code.rgb[1]');
// => 179
getSync() ​
Call Signature ​
getSync<T>(): T;
Gets all key values. For async, use [[get|get()]].
Type Parameters ​
T ​
T
extends valueTypes
Returns ​
T
All key values.
Example ​
Gets all key values.
const obj = getSync();
Call Signature ​
getSync<T>(keyPath): T;
Gets the value at the given key path. For async, use [[get|get()]].
Type Parameters ​
T ​
T
extends valueTypes
Parameters ​
keyPath ​
The key path of the property.
Returns ​
T
The value at the given key path.
Examples ​
Get the value at color.name
.
// Given:
{
"color": {
"name": "cerulean",
"code": {
"rgb": [0, 179, 230],
"hex": "#003BE6"
}
}
}
const value = keyValues.getSync('color.name');
// => "cerulean"
Get the value at color.code.hex
.
const hex = keyValues.getSync('color.color.hex');
// => "#003BE6"
Get the value at color.hue
.
const h = 'hue';
const value = keyValues.getSync(['color', h]);
// => undefined
Get the value at color.code.rgb[1]
.
const h = 'hue';
const value = keyValues.getSync('color.code.rgb[1]');
// => 179
has() ​
has(keyPath): Promise<boolean>;
Checks if the given key path exists. For sync, use [[hasSync|hasSync()]].
Parameters ​
keyPath ​
The key path to check.
Returns ​
Promise
<boolean
>
A promise which resolves to true
if the keyPath
exists, else false
.
Examples ​
Check if the value at color.name
exists.
// Given:
//
// {
// "color": {
// "name": "cerulean",
// "code": {
// "rgb": [0, 179, 230],
// "hex": "#003BE6"
// }
// }
// }
const exists = await keyValues.has('color.name');
// => true
Check if the value at color.hue
exists.
const h = 'hue';
const exists = await keyValues.has(['color', h]);
// => false
Check if the value at color.code.rgb[1]
exists.
const exists = await keyValues.has(color.code.rgb[1]);
// => true
hasSync() ​
hasSync(keyPath): boolean;
Checks if the given key path exists. For async, use [[hasSync|hasSync()]].
Parameters ​
keyPath ​
The key path to check.
Returns ​
boolean
true
if the keyPath
exists, else false
.
Examples ​
Check if the value at color.name
exists.
// Given:
//
// {
// "color": {
// "name": "cerulean",
// "code": {
// "rgb": [0, 179, 230],
// "hex": "#003BE6"
// }
// }
// }
const exists = keyValues.hasSync('color.name');
// => true
Check if the value at color.hue
exists.
const h = 'hue';
const exists = keyValues.hasSync(['color', h]);
// => false
Check if the value at color.code.rgb[1]
exists.
const exists = keyValues.hasSync(color.code.rgb[1]);
// => true
reset() ​
reset(): void;
Resets the KeyValues Storage's configuration to defaults.
Returns ​
void
Example ​
Reset configuration to defaults.
keyValues.reset();
set() ​
Call Signature ​
set<T>(obj): Promise<void>;
Sets all key values. For sync, use [[setSync|setSync()]].
Type Parameters ​
T ​
T
extends valueTypes
Parameters ​
obj ​
Types
<T
>
The new key value.
Returns ​
Promise
<void
>
A promise which resolves when the value have been set.
Example ​
Set all key values.
await keyValues.set({ aqpw: 'nice' });
Call Signature ​
set<T>(keyPath, value): Promise<void>;
Sets the value at the given key path. For sync, use [[setSync|setSync()]].
Type Parameters ​
T ​
T
extends valueTypes
Parameters ​
keyPath ​
The key path of the property.
value ​
T
The value to set.
Returns ​
Promise
<void
>
A promise which resolves when the setting has been set.
Examples ​
Change the value at color.name
to sapphire
.
// Given:
//
// {
// "color": {
// "name": "cerulean",
// "code": {
// "rgb": [0, 179, 230],
// "hex": "#003BE6"
// }
// }
// }
await keyValues.set('color.name', 'sapphire');
Set the value of color.hue
to blue-ish
.
const h = 'hue';
await keyValues.set(['color', h], 'blue-ish);
Change the value of color.code
.
await keyValues.set('color.code', {
rgb: [16, 31, 134],
hex: '#101F86'
});
setSync() ​
Call Signature ​
setSync<T>(obj): void;
Sets all key values. For async, use [[set|set()]].
Type Parameters ​
T ​
T
extends valueTypes
Parameters ​
obj ​
Types
<T
>
The new key values.
Returns ​
void
Example ​
Set all key values.
keyValues.setSync({ aqpw: 'nice' });
Call Signature ​
setSync<T>(keyPath, value): void;
Sets the value at the given key path. For async, use [[set|set()]].
Type Parameters ​
T ​
T
extends valueTypes
Parameters ​
keyPath ​
The key path of the property.
value ​
T
The value to set.
Returns ​
void
Examples ​
Change the value at color.name
to sapphire
.
// Given:
//
// {
// "color": {
// "name": "cerulean",
// "code": {
// "rgb": [0, 179, 230],
// "hex": "#003BE6"
// }
// }
// }
keyValues.setSync('color.name', 'sapphire');
Set the value of color.hue
to blue-ish
.
const h = 'hue';
keyValues.setSync(['color', h], 'blue-ish);
Change the value of color.code
.
keyValues.setSync('color.code', {
rgb: [16, 31, 134],
hex: '#101F86'
});
unset() ​
Call Signature ​
unset(): Promise<boolean>;
Unsets all key values. For sync, use [unsetSync()].
Returns ​
Promise
<boolean
>
A promise which resolves when the key values have been unset.
Example ​
Unsets all key values.
await keyValues.unset();
Call Signature ​
unset(keyPath): Promise<boolean>;
Unsets the property at the given key path. For sync, use [[unsetSync|unsetSync()]].
Parameters ​
keyPath ​
The key path of the property.
Returns ​
Promise
<boolean
>
A promise which resolves when the setting has been unset.
Examples ​
Unset the property color.name
.
// Given:
//
// {
// "color": {
// "name": "cerulean",
// "code": {
// "rgb": [0, 179, 230],
// "hex": "#003BE6"
// }
// }
// }
await keyValues.unset('color.name');
await keyValues.get('color.name');
// => undefined
Unset the property color.code.rgba[1]
.
await keyValues.unset('color.code.rgba[1]');
await keyValues.get('color.code.rgb');
// => [0, null, 230]
unsetSync() ​
Call Signature ​
unsetSync(): boolean;
Unsets all key values. For async, use [unset()].
Returns ​
boolean
Example ​
Unsets all key values.
keyValues.unsetSync();
Call Signature ​
unsetSync(keyPath): boolean;
Unsets the property at the given key path. For async, use [[unset|unset()]].
Parameters ​
keyPath ​
The key path of the property.
Returns ​
boolean
Examples ​
Unset the property color.name
.
// Given:
//
// {
// "color": {
// "name": "cerulean",
// "code": {
// "rgb": [0, 179, 230],
// "hex": "#003BE6"
// }
// }
// }
keyValues.unsetSync('color.name');
keyValues.getSync('color.name');
// => undefined
Unset the property color.code.rgba[1]
.
keyValues.unsetSync('color.code.rgba[1]');
keyValues.getSync('color.code.rgb');
// => [0, null, 230]