8.0.1 • Published 9 months ago

memory-kv-store v8.0.1

Weekly downloads
45
License
MIT
Repository
github
Last release
9 months ago

memory-kv-store

A simple in-memory key/value store.

GitHub license Coverage Status

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>

Set a value in the store

Kind: instance method of KV
Returns: Promise.<void> - A promise to be resolved when the value is stored.

ParamTypeDescription
keyStringThe key to store the value at
value*The value to store
ttlnumberThe 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.

ParamTypeDescription
keyStringThe 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.

ParamTypeDescription
keyStringThe 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.

ParamTypeDescription
keysArray.StringThe keys to store the values at
valuesArrayThe values to store
ttlsArray.numberThe 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.

ParamTypeDescription
keysArray.StringThe 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.

ParamTypeDescription
keysArray.StringThe 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

ParamTypeDescription
servicesObjectThe services to inject
services.delayfunctionA delaying function
services.timefunctionA timing function
services.logfunctionA logging function
services.KV_TTLNumberThe store time to live
services.KV_STOREMapThe 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),
});

Authors

License

MIT

8.0.1

9 months ago

8.0.0

9 months ago

7.0.0

9 months ago

6.0.2

1 year ago

6.0.1

2 years ago

5.0.1

2 years ago

6.0.0

2 years ago

5.0.0

3 years ago

4.2.0

3 years ago

4.1.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.0

6 years ago