memory-kv-store
TypeScript icon, indicating that this package has built-in type declarations

8.0.1 • Public • Published

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

KV

Creates a key/value store

Functions

initKV(services)Promise.<KV>

Instantiate the kv service

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.

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),
});

Authors

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i memory-kv-store

Weekly Downloads

197

Version

8.0.1

License

MIT

Unpacked Size

52.4 kB

Total Files

12

Last publish

Collaborators

  • nfroidure