keyv 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -2
- package/package.json +3 -3
- package/src/index.js +7 -3
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
[](https://travis-ci.org/lukechilds/keyv)
|
|
10
10
|
[](https://coveralls.io/github/lukechilds/keyv?branch=master)
|
|
11
|
+
[](https://www.npmjs.com/package/keyv)
|
|
11
12
|
[](https://www.npmjs.com/package/keyv)
|
|
12
13
|
|
|
13
14
|
Keyv provides a consistent interface for key-value storage across multiple backends via storage adapters. It supports TTL based expiry, making it suitable as a cache or a persistent key-value store.
|
|
@@ -21,7 +22,7 @@ There are a few existing modules similar to Keyv, however Keyv is different beca
|
|
|
21
22
|
- Suitable as a TTL based cache or persistent key-value store
|
|
22
23
|
- [Easily embeddable](#add-cache-support-to-your-module) inside another module
|
|
23
24
|
- Works with any storage that implements the [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) API
|
|
24
|
-
- Handles all
|
|
25
|
+
- Handles all JSON types plus `Buffer`
|
|
25
26
|
- Supports namespaces
|
|
26
27
|
- Wide range of [**efficient, well tested**](#official-storage-adapters) storage adapters
|
|
27
28
|
- Connection errors are passed through (db failures won't kill your app)
|
|
@@ -59,7 +60,7 @@ const keyv = new Keyv('postgresql://user:pass@localhost:5432/dbname');
|
|
|
59
60
|
const keyv = new Keyv('mysql://user:pass@localhost:3306/dbname');
|
|
60
61
|
|
|
61
62
|
// Handle DB connection errors
|
|
62
|
-
keyv.on('error' err => console.log('Connection Error', err));
|
|
63
|
+
keyv.on('error', err => console.log('Connection Error', err));
|
|
63
64
|
|
|
64
65
|
await keyv.set('foo', 'expires in 1 second', 1000); // true
|
|
65
66
|
await keyv.set('foo', 'never expires'); // true
|
|
@@ -85,6 +86,18 @@ await users.get('foo'); // undefined
|
|
|
85
86
|
await cache.get('foo'); // 'cache'
|
|
86
87
|
```
|
|
87
88
|
|
|
89
|
+
### Custom Serializers
|
|
90
|
+
|
|
91
|
+
Keyv uses [`json-buffer`](https://github.com/dominictarr/json-buffer) for data serialization to ensure consistency across different backends.
|
|
92
|
+
|
|
93
|
+
You can optionally provide your own serialization functions to support extra data types or to serialize to something other than JSON.
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
const keyv = new Keyv({ serialize: JSON.stringify, deserialize: JSON.parse });
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**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.
|
|
100
|
+
|
|
88
101
|
## Official Storage Adapters
|
|
89
102
|
|
|
90
103
|
The official storage adapters are covered by [over 150 integration tests](https://travis-ci.org/lukechilds/keyv/jobs/260418145) to guarantee consistent behaviour. They are lightweight, efficient wrappers over the DB clients making use of indexes and native TTLs where available.
|
|
@@ -124,6 +137,12 @@ const lru = new QuickLRU({ maxSize: 1000 });
|
|
|
124
137
|
const keyv = new Keyv({ store: lru });
|
|
125
138
|
```
|
|
126
139
|
|
|
140
|
+
The following are third-party storage adapters compatible with Keyv:
|
|
141
|
+
|
|
142
|
+
- [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple "Least Recently Used" (LRU) cache
|
|
143
|
+
- [keyv-file](https://github.com/zaaack/keyv-file) - File system storage adapter for Keyv
|
|
144
|
+
- [keyv-dynamodb](https://www.npmjs.com/package/keyv-dynamodb) - DynamoDB storage adapter for Keyv
|
|
145
|
+
|
|
127
146
|
## Add Cache Support to your Module
|
|
128
147
|
|
|
129
148
|
Keyv is designed to be easily embedded into other modules to add cache support. The recommended pattern is to expose a `cache` option in your modules options which is passed through to Keyv. Caching will work in memory by default and users have the option to also install a Keyv storage adapter and pass in a connection string, or any other storage that implements the `Map` API.
|
|
@@ -196,6 +215,20 @@ Default: `undefined`
|
|
|
196
215
|
|
|
197
216
|
Default TTL. Can be overridden by specififying a TTL on `.set()`.
|
|
198
217
|
|
|
218
|
+
#### options.serialize
|
|
219
|
+
|
|
220
|
+
Type: `Function`<br>
|
|
221
|
+
Default: `JSONB.stringify`
|
|
222
|
+
|
|
223
|
+
A custom serialization function.
|
|
224
|
+
|
|
225
|
+
#### options.deserialize
|
|
226
|
+
|
|
227
|
+
Type: `Function`<br>
|
|
228
|
+
Default: `JSONB.parse`
|
|
229
|
+
|
|
230
|
+
A custom deserialization function.
|
|
231
|
+
|
|
199
232
|
#### options.store
|
|
200
233
|
|
|
201
234
|
Type: `Storage adapter instance`<br>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keyv",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Simple key-value storage with support for multiple backends",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"json-buffer": "3.0.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"ava": "^0.
|
|
35
|
+
"ava": "^0.25.0",
|
|
36
36
|
"coveralls": "^3.0.0",
|
|
37
37
|
"eslint-config-xo-lukechilds": "^1.0.0",
|
|
38
38
|
"@keyv/mongo": "*",
|
|
@@ -44,6 +44,6 @@
|
|
|
44
44
|
"nyc": "^11.0.3",
|
|
45
45
|
"this": "^1.0.2",
|
|
46
46
|
"timekeeper": "^2.0.0",
|
|
47
|
-
"xo": "^0.
|
|
47
|
+
"xo": "^0.20.1"
|
|
48
48
|
}
|
|
49
49
|
}
|
package/src/index.js
CHANGED
|
@@ -24,7 +24,11 @@ class Keyv extends EventEmitter {
|
|
|
24
24
|
constructor(uri, opts) {
|
|
25
25
|
super();
|
|
26
26
|
this.opts = Object.assign(
|
|
27
|
-
{
|
|
27
|
+
{
|
|
28
|
+
namespace: 'keyv',
|
|
29
|
+
serialize: JSONB.stringify,
|
|
30
|
+
deserialize: JSONB.parse
|
|
31
|
+
},
|
|
28
32
|
(typeof uri === 'string') ? { uri } : uri,
|
|
29
33
|
opts
|
|
30
34
|
);
|
|
@@ -51,7 +55,7 @@ class Keyv extends EventEmitter {
|
|
|
51
55
|
return Promise.resolve()
|
|
52
56
|
.then(() => store.get(key))
|
|
53
57
|
.then(data => {
|
|
54
|
-
data = (typeof data === 'string') ?
|
|
58
|
+
data = (typeof data === 'string') ? this.opts.deserialize(data) : data;
|
|
55
59
|
if (data === undefined) {
|
|
56
60
|
return undefined;
|
|
57
61
|
}
|
|
@@ -77,7 +81,7 @@ class Keyv extends EventEmitter {
|
|
|
77
81
|
.then(() => {
|
|
78
82
|
const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
|
|
79
83
|
value = { value, expires };
|
|
80
|
-
return store.set(key,
|
|
84
|
+
return store.set(key, this.opts.serialize(value), ttl);
|
|
81
85
|
})
|
|
82
86
|
.then(() => true);
|
|
83
87
|
}
|