keyv 2.0.2 → 4.0.1

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.
Files changed (3) hide show
  1. package/README.md +52 -7
  2. package/package.json +7 -7
  3. package/src/index.js +29 -19
package/README.md CHANGED
@@ -8,6 +8,7 @@
8
8
 
9
9
  [![Build Status](https://travis-ci.org/lukechilds/keyv.svg?branch=master)](https://travis-ci.org/lukechilds/keyv)
10
10
  [![Coverage Status](https://coveralls.io/repos/github/lukechilds/keyv/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/keyv?branch=master)
11
+ [![npm](https://img.shields.io/npm/dm/keyv.svg)](https://www.npmjs.com/package/keyv)
11
12
  [![npm](https://img.shields.io/npm/v/keyv.svg)](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 JavaScript types (values can be `Buffer`/`null`/`undefined`)
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,15 @@ 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
+ - [keyv-firestore ](https://github.com/goto-bus-stop/keyv-firestore) – Firebase Cloud Firestore adapter for Keyv
146
+ - [keyv-mssql](https://github.com/pmorgan3/keyv-mssql) - Microsoft Sql Server adapter for Keyv
147
+ - [keyv-memcache](https://github.com/jaredwray/keyv-memcache) - Memcache storage adapter for Keyv
148
+
127
149
  ## Add Cache Support to your Module
128
150
 
129
151
  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 +218,20 @@ Default: `undefined`
196
218
 
197
219
  Default TTL. Can be overridden by specififying a TTL on `.set()`.
198
220
 
221
+ #### options.serialize
222
+
223
+ Type: `Function`<br>
224
+ Default: `JSONB.stringify`
225
+
226
+ A custom serialization function.
227
+
228
+ #### options.deserialize
229
+
230
+ Type: `Function`<br>
231
+ Default: `JSONB.parse`
232
+
233
+ A custom deserialization function.
234
+
199
235
  #### options.store
200
236
 
201
237
  Type: `Storage adapter instance`<br>
@@ -220,23 +256,32 @@ Set a value.
220
256
 
221
257
  By default keys are persistent. You can set an expiry TTL in milliseconds.
222
258
 
223
- Returns `true`.
259
+ Returns a promise which resolves to `true`.
260
+
261
+ #### .get(key, [options])
262
+
263
+ Returns a promise which resolves to the retrieved value.
264
+
265
+ ##### options.raw
266
+
267
+ Type: `Boolean`<br>
268
+ Default: `false`
224
269
 
225
- #### .get(key)
270
+ If set to true the raw DB object Keyv stores internally will be returned instead of just the value.
226
271
 
227
- Returns the value.
272
+ This contains the TTL timestamp.
228
273
 
229
274
  #### .delete(key)
230
275
 
231
276
  Deletes an entry.
232
277
 
233
- Returns `true` if the key existed, `false` if not.
278
+ Returns a promise which resolves to `true` if the key existed, `false` if not.
234
279
 
235
280
  #### .clear()
236
281
 
237
282
  Delete all entries in the current namespace.
238
283
 
239
- Returns `undefined`.
284
+ Returns a promise which is resolved when the entries have been cleared.
240
285
 
241
286
  ## License
242
287
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keyv",
3
- "version": "2.0.2",
3
+ "version": "4.0.1",
4
4
  "description": "Simple key-value storage with support for multiple backends",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -29,11 +29,11 @@
29
29
  },
30
30
  "homepage": "https://github.com/lukechilds/keyv",
31
31
  "dependencies": {
32
- "json-buffer": "3.0.0"
32
+ "json-buffer": "3.0.1"
33
33
  },
34
34
  "devDependencies": {
35
- "ava": "^0.22.0",
36
- "coveralls": "^2.13.1",
35
+ "ava": "^2.2.0",
36
+ "coveralls": "^3.0.0",
37
37
  "eslint-config-xo-lukechilds": "^1.0.0",
38
38
  "@keyv/mongo": "*",
39
39
  "@keyv/mysql": "*",
@@ -41,9 +41,9 @@
41
41
  "@keyv/redis": "*",
42
42
  "@keyv/sqlite": "*",
43
43
  "@keyv/test-suite": "*",
44
- "nyc": "^11.0.3",
44
+ "nyc": "^14.1.1",
45
45
  "this": "^1.0.2",
46
- "timekeeper": "^1.0.0",
47
- "xo": "^0.19.0"
46
+ "timekeeper": "^2.0.0",
47
+ "xo": "^0.25.3"
48
48
  }
49
49
  }
package/src/index.js CHANGED
@@ -17,6 +17,7 @@ const loadStore = opts => {
17
17
  const adapter = opts.adapter || /^[^:]*/.exec(opts.uri)[0];
18
18
  return new (require(adapters[adapter]))(opts);
19
19
  }
20
+
20
21
  return new Map();
21
22
  };
22
23
 
@@ -24,7 +25,11 @@ class Keyv extends EventEmitter {
24
25
  constructor(uri, opts) {
25
26
  super();
26
27
  this.opts = Object.assign(
27
- { namespace: 'keyv' },
28
+ {
29
+ namespace: 'keyv',
30
+ serialize: JSONB.stringify,
31
+ deserialize: JSONB.parse
32
+ },
28
33
  (typeof uri === 'string') ? { uri } : uri,
29
34
  opts
30
35
  );
@@ -45,54 +50,59 @@ class Keyv extends EventEmitter {
45
50
  return `${this.opts.namespace}:${key}`;
46
51
  }
47
52
 
48
- get(key) {
49
- key = this._getKeyPrefix(key);
50
- const store = this.opts.store;
53
+ get(key, opts) {
54
+ const keyPrefixed = this._getKeyPrefix(key);
55
+ const { store } = this.opts;
51
56
  return Promise.resolve()
52
- .then(() => store.get(key))
57
+ .then(() => store.get(keyPrefixed))
58
+ .then(data => {
59
+ return (typeof data === 'string') ? this.opts.deserialize(data) : data;
60
+ })
53
61
  .then(data => {
54
- data = (typeof data === 'string') ? JSONB.parse(data) : data;
55
62
  if (data === undefined) {
56
63
  return undefined;
57
64
  }
58
- if (!store.ttlSupport && typeof data.expires === 'number' && Date.now() > data.expires) {
65
+
66
+ if (typeof data.expires === 'number' && Date.now() > data.expires) {
59
67
  this.delete(key);
60
68
  return undefined;
61
69
  }
62
- return store.ttlSupport ? data : data.value;
70
+
71
+ return (opts && opts.raw) ? data : data.value;
63
72
  });
64
73
  }
65
74
 
66
75
  set(key, value, ttl) {
67
- key = this._getKeyPrefix(key);
76
+ const keyPrefixed = this._getKeyPrefix(key);
68
77
  if (typeof ttl === 'undefined') {
69
78
  ttl = this.opts.ttl;
70
79
  }
80
+
71
81
  if (ttl === 0) {
72
82
  ttl = undefined;
73
83
  }
74
- const store = this.opts.store;
84
+
85
+ const { store } = this.opts;
75
86
 
76
87
  return Promise.resolve()
77
88
  .then(() => {
78
- if (!store.ttlSupport) {
79
- const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
80
- value = { value, expires };
81
- }
82
- return store.set(key, JSONB.stringify(value), ttl);
89
+ const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
90
+ value = { value, expires };
91
+ return this.opts.serialize(value);
83
92
  })
93
+ .then(value => store.set(keyPrefixed, value, ttl))
84
94
  .then(() => true);
85
95
  }
86
96
 
87
97
  delete(key) {
88
- key = this._getKeyPrefix(key);
89
- const store = this.opts.store;
98
+ const keyPrefixed = this._getKeyPrefix(key);
99
+ const { store } = this.opts;
90
100
  return Promise.resolve()
91
- .then(() => store.delete(key));
101
+ .then(() => store.delete(keyPrefixed));
92
102
  }
93
103
 
94
104
  clear() {
95
- const store = this.opts.store;
105
+ const { store } = this.opts;
96
106
  return Promise.resolve()
97
107
  .then(() => store.clear());
98
108
  }