# memory-kv-store

> A simple in-memory key/value store.

Latest version **10.1.0** (published 2026-08-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install memory-kv-store
pnpm add memory-kv-store
yarn add memory-kv-store
bun add memory-kv-store
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 10.1.0 |
| Published | 2026-08-09 |
| First published | 2017-12-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=24.14.0 |
| Dependencies | 2 |
| Unpacked size | 59.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Nicolas Froidure |
| Maintainers | nfroidure |

## Links

- npm: https://www.npmjs.com/package/memory-kv-store
- Repository: https://github.com/nfroidure/memory-kv-store
- Homepage: https://github.com/nfroidure/memory-kv-store#readme
- Issues: https://github.com/nfroidure/memory-kv-store/issues
- Funding: https://github.com/sponsors/nfroidure
- npm.io page: https://npm.io/package/memory-kv-store

## Dependencies (2)

- [knifecycle](https://npm.io/package/knifecycle.md) 21.4.0
- [common-services](https://npm.io/package/common-services.md) ^20.0.0

## Recent versions

- 10.1.0 (latest) — 2026-08-09
- 10.0.2 — 2026-04-17
- 10.0.1 — 2026-04-07
- 10.0.0 — 2026-03-27
- 9.0.1 — 2025-11-06
- 9.0.0 — 2024-12-04
- 8.0.2 — 2024-07-16
- 8.0.1 — 2023-08-20
- 8.0.0 — 2023-08-17
- 7.0.0 — 2023-08-12
- 6.0.2 — 2023-01-05
- 6.0.1 — 2022-09-01
- 6.0.0 — 2022-06-12
- 5.0.1 — 2022-05-25
- 5.0.0 — 2021-11-02
- … 12 more at https://npm.io/package/memory-kv-store/versions

## README

[//]: # ( )
[//]: # (This file is automatically generated by a `metapak`)
[//]: # (module. Do not change it  except between the)
[//]: # (`content:start/end` flags, your changes would)
[//]: # (be overridden.)
[//]: # ( )
# memory-kv-store
> A simple in-memory key/value store.

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nfroidure/memory-kv-store/blob/main/LICENSE)


[//]: # (::contents:start)

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`](https://github.com/nfroidure/common-services)
 project.

[//]: # (::contents:end)

# API
## Classes

<dl>
<dt><a href="#KV">KV</a></dt>
<dd><p>Creates a key/value store</p>
</dd>
</dl>

## Functions

<dl>
<dt><a href="#initKV">initKV(services)</a> ⇒ <code><a href="#KV">Promise.&lt;KV&gt;</a></code></dt>
<dd><p>Instantiate the kv service</p>
</dd>
</dl>

<a name="KV"></a>

## KV
Creates a key/value store

**Kind**: global class  

* [KV](#KV)
    * [.set(key, value, [ttl])](#KV+set) ⇒ <code>Promise.&lt;void&gt;</code>
    * [.get(key)](#KV+get) ⇒ <code>Promise.&lt;\*&gt;</code>
    * [.delete(key)](#KV+delete) ⇒ <code>Promise.&lt;void&gt;</code>
    * [.getSet(key, value, [ttl])](#KV+getSet) ⇒ <code>Promise.&lt;\*&gt;</code>
    * [.getDelete(key)](#KV+getDelete) ⇒ <code>Promise.&lt;\*&gt;</code>
    * [.bulkSet(keys, values, [ttls])](#KV+bulkSet) ⇒ <code>Promise.&lt;void&gt;</code>
    * [.bulkGet(keys)](#KV+bulkGet) ⇒ <code>Promise.&lt;Array.&lt;\*&gt;&gt;</code>
    * [.bulkDelete(keys)](#KV+bulkDelete) ⇒ <code>Promise.&lt;void&gt;</code>

<a name="KV+set"></a>

### kV.set(key, value, [ttl]) ⇒ <code>Promise.&lt;void&gt;</code>
Set a value in the store

**Kind**: instance method of [<code>KV</code>](#KV)  
**Returns**: <code>Promise.&lt;void&gt;</code> - A promise to be resolved when the value is stored.  

| Param | Type | Description |
| --- | --- | --- |
| key | <code>String</code> | The key to store the value at |
| value | <code>\*</code> | The value to store |
| [ttl] | <code>number</code> | The duration in milliseconds the value remains valid |

**Example**  
```js
kv.set('hello', 'world');
.then(() => console.log('Stored!'));
// Prints: Stored!
```
<a name="KV+get"></a>

### kV.get(key) ⇒ <code>Promise.&lt;\*&gt;</code>
Get a value from the store

**Kind**: instance method of [<code>KV</code>](#KV)  
**Returns**: <code>Promise.&lt;\*&gt;</code> - A promise that resolve to the actual value.  

| Param | Type | Description |
| --- | --- | --- |
| key | <code>String</code> | The key that map to the value |

**Example**  
```js
kv.get('hello');
.then((value) => console.log(value));
// Prints: world
```
<a name="KV+delete"></a>

### kV.delete(key) ⇒ <code>Promise.&lt;void&gt;</code>
Delete a value from the store

**Kind**: instance method of [<code>KV</code>](#KV)  
**Returns**: <code>Promise.&lt;void&gt;</code> - A promise that resolve once the value is deleted.  

| Param | Type | Description |
| --- | --- | --- |
| key | <code>String</code> | The keyof the deleted value |

**Example**  
```js
kv.delete('hello');
.then((value) => console.log('Deleted!'));
// Prints: Deleted!
```
<a name="KV+getSet"></a>

### kV.getSet(key, value, [ttl]) ⇒ <code>Promise.&lt;\*&gt;</code>
Set a value and get the previous one

**Kind**: instance method of [<code>KV</code>](#KV)  
**Returns**: <code>Promise.&lt;\*&gt;</code> - A promise that resolve to the previous value.  

| Param | Type | Description |
| --- | --- | --- |
| key | <code>String</code> | The key that map to the value |
| value | <code>\*</code> | The new value to store |
| [ttl] | <code>number</code> | The duration in milliseconds the value remains valid |

**Example**  
```js
kv.getSet('hello', 'folks');
.then((value) => console.log(value));
// Prints: world
```
<a name="KV+getDelete"></a>

### kV.getDelete(key) ⇒ <code>Promise.&lt;\*&gt;</code>
Get a value from the store and delete it

**Kind**: instance method of [<code>KV</code>](#KV)  
**Returns**: <code>Promise.&lt;\*&gt;</code> - A promise that resolve to the actual value.  

| Param | Type | Description |
| --- | --- | --- |
| key | <code>String</code> | The key that map to the value |

**Example**  
```js
kv.getDelete('hello');
.then((value) => console.log(value));
// Prints: world
```
<a name="KV+bulkSet"></a>

### kV.bulkSet(keys, values, [ttls]) ⇒ <code>Promise.&lt;void&gt;</code>
Set a several values in the store

**Kind**: instance method of [<code>KV</code>](#KV)  
**Returns**: <code>Promise.&lt;void&gt;</code> - A promise to be resolved when the values are stored.  

| Param | Type | Description |
| --- | --- | --- |
| keys | <code>Array.String</code> | The keys to store the values at |
| values | <code>Array</code> | The values to store |
| [ttls] | <code>Array.number</code> | The duration in milliseconds each values remains valid |

**Example**  
```js
kv.bulkSet(['hello', 'foo'], ['world', 'bar']);
.then(() => console.log('Stored!'));
// Prints: Stored!
```
<a name="KV+bulkGet"></a>

### kV.bulkGet(keys) ⇒ <code>Promise.&lt;Array.&lt;\*&gt;&gt;</code>
Get a several values from the store

**Kind**: instance method of [<code>KV</code>](#KV)  
**Returns**: <code>Promise.&lt;Array.&lt;\*&gt;&gt;</code> - A promise to be resolved when the values
 are retrieved.  

| Param | Type | Description |
| --- | --- | --- |
| keys | <code>Array.String</code> | The keys to retrieve the values |

**Example**  
```js
kv.bulkGet(['hello', 'foo']);
.then((values) => console.log(values));
// Prints: ['world', 'bar']
```
<a name="KV+bulkDelete"></a>

### kV.bulkDelete(keys) ⇒ <code>Promise.&lt;void&gt;</code>
Delete values for several keys from the store

**Kind**: instance method of [<code>KV</code>](#KV)  
**Returns**: <code>Promise.&lt;void&gt;</code> - A promise to be resolved when the values
 are deleted.  

| Param | Type | Description |
| --- | --- | --- |
| keys | <code>Array.String</code> | The keys for which to delete the values |

**Example**  
```js
kv.bulkDelete(['hello', 'foo']);
.then((values) => console.log('Deleted!'));
// Prints: Deleted!
```
<a name="initKV"></a>

## initKV(services) ⇒ [<code>Promise.&lt;KV&gt;</code>](#KV)
Instantiate the kv service

**Kind**: global function  
**Returns**: [<code>Promise.&lt;KV&gt;</code>](#KV) - A promise of the kv service  

| Param | Type | Description |
| --- | --- | --- |
| services | <code>Object</code> | The services to inject |
| services.delay | <code>function</code> | A delaying function |
| services.time | <code>function</code> | A timing function |
| [services.log] | <code>function</code> | A logging function |
| [services.KV_TTL] | <code>Number</code> | The store time to live |
| [services.KV_STORE] | <code>Map</code> | The store for values as a simple object, it is useful  to get a synchronous access to the store in tests  for example. |

**Example**  
```js
import initKV from 'memory-kv-store';

const kv = await initKV({
  delay: Promise.delay.bind(Promise),
});
```

# Authors
- [Nicolas Froidure](http://insertafter.com/en/index.html)

# License
[MIT](https://github.com/nfroidure/memory-kv-store/blob/main/LICENSE)

---
_Source: https://npm.io/package/memory-kv-store · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
