Comparing version 3.0.0 to 3.1.0
{ | ||
"name": "keyv", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Simple key-value storage with support for multiple backends", | ||
@@ -35,3 +35,3 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"ava": "^0.22.0", | ||
"ava": "^0.25.0", | ||
"coveralls": "^3.0.0", | ||
@@ -48,4 +48,4 @@ "eslint-config-xo-lukechilds": "^1.0.0", | ||
"timekeeper": "^2.0.0", | ||
"xo": "^0.19.0" | ||
"xo": "^0.20.1" | ||
} | ||
} |
@@ -11,2 +11,3 @@ <h1 align="center"> | ||
[![Coverage Status](https://coveralls.io/repos/github/lukechilds/keyv/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/keyv?branch=master) | ||
[![npm](https://img.shields.io/npm/dm/keyv.svg)](https://www.npmjs.com/package/keyv) | ||
[![npm](https://img.shields.io/npm/v/keyv.svg)](https://www.npmjs.com/package/keyv) | ||
@@ -25,3 +26,3 @@ | ||
- Works with any storage that implements the [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) API | ||
- Handles all JavaScript types (values can be `Buffer`/`null`/`undefined`) | ||
- Handles all JSON types plus `Buffer` | ||
- Supports namespaces | ||
@@ -64,3 +65,3 @@ - Wide range of [**efficient, well tested**](#official-storage-adapters) storage adapters | ||
// Handle DB connection errors | ||
keyv.on('error' err => console.log('Connection Error', err)); | ||
keyv.on('error', err => console.log('Connection Error', err)); | ||
@@ -91,2 +92,14 @@ await keyv.set('foo', 'expires in 1 second', 1000); // true | ||
### Custom Serializers | ||
Keyv uses [`json-buffer`](https://github.com/dominictarr/json-buffer) for data serialization to ensure consistency across different backends. | ||
You can optionally provide your own serialization functions to support extra data types or to serialize to something other than JSON. | ||
```js | ||
const keyv = new Keyv({ serialize: JSON.stringify, deserialize: JSON.parse }); | ||
``` | ||
**Warning:** Using custom serializers means you lose any guarantee of data consistency. You should do extensive testing with your serialisation functions and chosen storage engine. | ||
## Official Storage Adapters | ||
@@ -131,2 +144,8 @@ | ||
The following are third-party storage adapters compatible with Keyv: | ||
- [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple "Least Recently Used" (LRU) cache | ||
- [keyv-file](https://github.com/zaaack/keyv-file) - File system storage adapter for Keyv | ||
- [keyv-dynamodb](https://www.npmjs.com/package/keyv-dynamodb) - DynamoDB storage adapter for Keyv | ||
## Add Cache Support to your Module | ||
@@ -204,2 +223,16 @@ | ||
#### options.serialize | ||
Type: `Function`<br> | ||
Default: `JSONB.stringify` | ||
A custom serialization function. | ||
#### options.deserialize | ||
Type: `Function`<br> | ||
Default: `JSONB.parse` | ||
A custom deserialization function. | ||
#### options.store | ||
@@ -206,0 +239,0 @@ |
@@ -27,3 +27,7 @@ 'use strict'; | ||
this.opts = Object.assign( | ||
{ namespace: 'keyv' }, | ||
{ | ||
namespace: 'keyv', | ||
serialize: JSONB.stringify, | ||
deserialize: JSONB.parse | ||
}, | ||
(typeof uri === 'string') ? { uri } : uri, | ||
@@ -55,3 +59,3 @@ opts | ||
.then(data => { | ||
data = (typeof data === 'string') ? JSONB.parse(data) : data; | ||
data = (typeof data === 'string') ? this.opts.deserialize(data) : data; | ||
if (data === undefined) { | ||
@@ -82,3 +86,3 @@ return undefined; | ||
value = { value, expires }; | ||
return store.set(key, JSONB.stringify(value), ttl); | ||
return store.set(key, this.opts.serialize(value), ttl); | ||
}) | ||
@@ -85,0 +89,0 @@ .then(() => true); |
14661
90
277