Skip to content

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 ​

ts
import { KeyValues } from '@heliomarpm/kvs';

// Create a new instance of KeyValues with custom options
const keyValues = new KeyValues({
	fileName: 'config.json',
	prettify: true,
	numSpaces: 4
});

// 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');
// output: 'sapphire'

// Get the nested key
const value = await keyValues.get('color');
// output: { name: 'sapphire' }

// Get all
const value = await keyValues.get();
// output: { color: { name: 'sapphire' } }

// 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 ​

ts
new KeyValues(options?): KeyValues;

Sets the configuration for KeyValues Storage's.

To reset to defaults, use [reset()].

js
Defaults:
{
	atomicSave: true,
	dir: 'localdb',
	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.

js
new KeyValues({ fileName: 'config.json', prettify: true });

Auxiliary Methods ​

file() ​

ts
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.

js
keyValues.file();
// => c:/users/<userprofile>/appdata/local/programs/<AppName>/keyvalues.json

resetOptions() ​

ts
resetOptions(): void;

Resets the KeyValues Storage's configuration to defaults.

Returns ​

void

Example ​

Reset configuration to defaults.

js
keyValues.resetOptions();

See ​

Options

Get Methods ​

get() ​

Call Signature ​

ts
get<T>(): Promise<T>;

Gets all key values.

For sync method, use [getSync()].

Type Parameters ​
T ​

T extends ValueType

Returns ​

Promise<T>

A promise which resolves with all key values.

Example ​

Gets all key values.

js
const obj = await get();

Call Signature ​

ts
get<T>(keyPath): Promise<T>;

Gets the value at the given key path.

For sync method, use [getSync()].

Type Parameters ​
T ​

T extends ValueType

Parameters ​
keyPath ​

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.

js
// 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.

js
const hex = await keyValues.get('color.color.hex');
// => "#003BE6"

Get the value not existent at color.hue.

js
const value = await keyValues.get(['color', 'hue']);
// => undefined

Get the value at color.code.rgb[1].

js
const value = await keyValues.get('color.code.rgb[1]');
// => 179

getSync() ​

Call Signature ​

ts
getSync<T>(): T;

Gets all key values.

For async method, use [get()].

Type Parameters ​
T ​

T extends ValueType

Returns ​

T

All key values.

Example ​

Gets all key values.

js
const obj = getSync();

Call Signature ​

ts
getSync<T>(keyPath): T;

Gets the value at the given key path.

For async method, use [get()].

Type Parameters ​
T ​

T extends ValueType

Parameters ​
keyPath ​

KeyPath

The key path of the property.

Returns ​

T

The value at the given key path.

Examples ​

Get the value at color.name.

js
// 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.

js
const hex = keyValues.getSync('color.color.hex');
// => "#003BE6"

Get the value at color.hue.

js
const h = 'hue';
const value = keyValues.getSync(['color', h]);
// => undefined

Get the value at color.code.rgb[1].

js
const h = 'hue';
const value = keyValues.getSync('color.code.rgb[1]');
// => 179

Get all values

js
 const values = keyValues.getSync();

Has Methods ​

has() ​

ts
has(keyPath): Promise<boolean>;

Checks if the given key path exists.

For sync method, use [hasSync()].

Parameters ​

keyPath ​

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.

js
// 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.

js
const exists = await keyValues.has(['color', 'hue']);
// => false

Check if the value at color.code.rgb[1] exists.

js
const exists = await keyValues.has(color.code.rgb[1]);
// => true

hasSync() ​

ts
hasSync(keyPath): boolean;

Checks if the given key path exists.

For async method, use [has()].

Parameters ​

keyPath ​

KeyPath

The key path to check.

Returns ​

boolean

true if the keyPath exists, else false.

Examples ​

Check if the value at color.name exists.

js
// 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.

js
const exists = keyValues.hasSync(['color', 'hue']);
// => false

Check if the value at color.code.rgb[1] exists.

js
const exists = keyValues.hasSync(color.code.rgb[1]);
// => true

Set Methods ​

set() ​

Call Signature ​

ts
set<T>(obj): Promise<void>;

Sets all key values.

For sync method, use [setSync()].

Type Parameters ​
T ​

T extends ValueType

Parameters ​
obj ​

RecordType<T>

The new key value.

Returns ​

Promise<void>

A promise which resolves when the value have been set.

Example ​

Set all key values.

js
await keyValues.set({ aqpw: 'nice' });

Call Signature ​

ts
set<T>(keyPath, value): Promise<void>;

Sets the value at the given key path.

For sync method, use [setSync()].

Type Parameters ​
T ​

T extends ValueType

Parameters ​
keyPath ​

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.

js
// Given:
{
	"color": {
		"name": "cerulean",
		"code": {
			"rgb": [0, 179, 230],
			"hex": "#003BE6"
		}
	}
}

await keyValues.set('color.name', 'sapphire');

Set the value of color.hue to bluish.

js
await keyValues.set(['color', 'hue'], 'bluish');

Change the value of color.code.

js
await keyValues.set('color.code', { rgb: [16, 31, 134], hex: '#101F86' });

setSync() ​

Call Signature ​

ts
setSync<T>(obj): void;

Sets all key values.

For async method, use [set()].

Type Parameters ​
T ​

T extends ValueType

Parameters ​
obj ​

RecordType<T>

The new key values.

Returns ​

void

Example ​

Set all key values.

js
keyValues.setSync({ aqpw: 'nice' });

Call Signature ​

ts
setSync<T>(keyPath, value): void;

Sets the value at the given key path.

For async method, use [set()].

Type Parameters ​
T ​

T extends ValueType

Parameters ​
keyPath ​

KeyPath

The key path of the property.

value ​

T

The value to set.

Returns ​

void

Examples ​

Change the value at color.name to sapphire.

js
// Given:
{
	"color": {
		"name": "cerulean",
		"code": {
			"rgb": [0, 179, 230],
			"hex": "#003BE6"
		}
	}
}

keyValues.setSync('color.name', 'sapphire');

Set the value of color.hue to bluish.

js
keyValues.setSync(['color', 'hue'], 'bluish);

Change the value of color.code.

js
keyValues.setSync('color.code', { rgb: [16, 31, 134], hex: '#101F86' });

Unset Methods ​

unset() ​

Call Signature ​

ts
unset(): Promise<boolean>;

Unsets all key values.

For sync method, use [unsetSync()].

Returns ​

Promise<boolean>

A promise which resolves when the key values have been unset.

Example ​

Unsets all key values.

js
await keyValues.unset();
await keyValues.get();
// => undefined

Call Signature ​

ts
unset(keyPath): Promise<boolean>;

Unsets the property at the given key path.

For sync method, use [unsetSync()].

Parameters ​
keyPath ​

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.

js
// 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].

js
await keyValues.unset('color.code.rgba[1]');
await keyValues.get('color.code.rgb');
// => [0, null, 230]

unsetSync() ​

Call Signature ​

ts
unsetSync(): boolean;

Unsets all key values.

For async method, use [unset()].

Returns ​

boolean

Example ​

Unsets all key values.

js
keyValues.unsetSync();

Call Signature ​

ts
unsetSync(keyPath): boolean;

Unsets the property at the given key path.

For async method, use [unset()].

Parameters ​
keyPath ​

KeyPath

The key path of the property.

Returns ​

boolean

Examples ​

Unset the property color.name.

js
// 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].

js
keyValues.unsetSync('color.code.rgba[1]');
keyValues.getSync('color.code.rgb');
// => [0, null, 230]