memory-kv-store
v8.0.2
Published
A simple in-memory key/value store.
Downloads
178
Readme
memory-kv-store
A simple in-memory key/value store.
This simple project is intended to mock real key value stores likes Redis or file system based stores. It can also be used in local scripts to run code that assume a key value store exists.
It requires a delay
services to be passed in, you can find an implementation
in the common-services
project.
API
Classes
Functions
KV
Creates a key/value store
Kind: global class
- KV
- .set(key, value, [ttl]) ⇒ Promise.<void>
- .get(key) ⇒ Promise.<*>
- .delete(key) ⇒ Promise.<void>
- .bulkSet(keys, values, [ttls]) ⇒ Promise.<void>
- .bulkGet(keys) ⇒ Promise.<Array.<*>>
- .bulkDelete(keys) ⇒ Promise.<void>
kV.set(key, value, [ttl]) ⇒ Promise.<void>
Set a value in the store
Kind: instance method of KV
Returns: Promise.<void> - A promise to be resolved when the value is stored.
| Param | Type | Description | | --- | --- | --- | | key | String | The key to store the value at | | value | * | The value to store | | [ttl] | number | The duration in milliseconds the value remains valid |
Example
kv.set('hello', 'world');
.then(() => console.log('Stored!'));
// Prints: Stored!
kV.get(key) ⇒ Promise.<*>
Get a value from the store
Kind: instance method of KV
Returns: Promise.<*> - A promise that resolve to the actual value.
| Param | Type | Description | | --- | --- | --- | | key | String | The key that map to the value |
Example
kv.get('hello');
.then((value) => console.log(value));
// Prints: world
kV.delete(key) ⇒ Promise.<void>
Delete a value from the store
Kind: instance method of KV
Returns: Promise.<void> - A promise that resolve once the value is deleted.
| Param | Type | Description | | --- | --- | --- | | key | String | The keyof the deleted value |
Example
kv.delete('hello');
.then((value) => console.log('Deleted!'));
// Prints: Deleted!
kV.bulkSet(keys, values, [ttls]) ⇒ Promise.<void>
Set a several values in the store
Kind: instance method of KV
Returns: Promise.<void> - A promise to be resolved when the values are stored.
| Param | Type | Description | | --- | --- | --- | | keys | Array.String | The keys to store the values at | | values | Array | The values to store | | [ttls] | Array.number | The duration in milliseconds each values remains valid |
Example
kv.bulkSet(['hello', 'foo'], ['world', 'bar']);
.then(() => console.log('Stored!'));
// Prints: Stored!
kV.bulkGet(keys) ⇒ Promise.<Array.<*>>
Get a several values from the store
Kind: instance method of KV
Returns: Promise.<Array.<*>> - A promise to be resolved when the values
are retrieved.
| Param | Type | Description | | --- | --- | --- | | keys | Array.String | The keys to retrieve the values |
Example
kv.bulkGet(['hello', 'foo']);
.then((values) => console.log(values));
// Prints: ['world', 'bar']
kV.bulkDelete(keys) ⇒ Promise.<void>
Delete values for several keys from the store
Kind: instance method of KV
Returns: Promise.<void> - A promise to be resolved when the values
are deleted.
| Param | Type | Description | | --- | --- | --- | | keys | Array.String | The keys for which to delete the values |
Example
kv.bulkDelete(['hello', 'foo']);
.then((values) => console.log('Deleted!'));
// Prints: Deleted!
initKV(services) ⇒ Promise.<KV>
Instantiate the kv service
Kind: global function
Returns: Promise.<KV> - A promise of the kv service
| Param | Type | Description | | --- | --- | --- | | services | Object | The services to inject | | services.delay | function | A delaying function | | services.time | function | A timing function | | [services.log] | function | A logging function | | [services.KV_TTL] | Number | The store time to live | | [services.KV_STORE] | Map | The store for values as a simple object, it is useful to get a synchronous access to the store in tests for example. |
Example
import initKV from 'memory-kv-store';
const kv = await initKV({
delay: Promise.delay.bind(Promise),
});