Narwhals Prolong Mischief
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

memory-kv-store

2.0.2 • Public • Published

memory-kv-store

A simple in-memory key/value store.

NPM version Build status Dependency Status devDependency Status Coverage Status Code Climate Dependency 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) ⇒ Promise

Set a value in the store

Kind: instance method of KV
Returns: Promise - 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

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.bulkSet(keys, values) ⇒ Promise

Set a several values in the store

Kind: instance method of KV
Returns: Promise - 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

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']

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

License

MIT

Keywords

none

install

npm i memory-kv-store

Downloadsweekly downloads

22

version

2.0.2

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability