keyv 2.0.1 → 4.0.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 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.1",
3
+ "version": "4.0.0",
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.20.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.24.0"
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,21 +50,25 @@ class Keyv extends EventEmitter {
45
50
  return `${this.opts.namespace}:${key}`;
46
51
  }
47
52
 
48
- get(key) {
53
+ get(key, opts) {
49
54
  key = this._getKeyPrefix(key);
50
- const store = this.opts.store;
55
+ const { store } = this.opts;
51
56
  return Promise.resolve()
52
57
  .then(() => store.get(key))
53
58
  .then(data => {
54
- data = (typeof data === 'string') ? JSONB.parse(data) : data;
59
+ return (typeof data === 'string') ? this.opts.deserialize(data) : data;
60
+ })
61
+ .then(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
 
@@ -68,31 +77,32 @@ class Keyv extends EventEmitter {
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(key, value, ttl))
84
94
  .then(() => true);
85
95
  }
86
96
 
87
97
  delete(key) {
88
98
  key = this._getKeyPrefix(key);
89
- const store = this.opts.store;
99
+ const { store } = this.opts;
90
100
  return Promise.resolve()
91
101
  .then(() => store.delete(key));
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
  }
package/.npmignore DELETED
@@ -1,77 +0,0 @@
1
- test/testdb.sqlite
2
-
3
- ## Node
4
-
5
- #npm5
6
- package-lock.json
7
-
8
- # Logs
9
- logs
10
- *.log
11
- npm-debug.log*
12
-
13
- # Runtime data
14
- pids
15
- *.pid
16
- *.seed
17
- *.pid.lock
18
-
19
- # Directory for instrumented libs generated by jscoverage/JSCover
20
- lib-cov
21
-
22
- # Coverage directory used by tools like istanbul
23
- coverage
24
-
25
- # nyc test coverage
26
- .nyc_output
27
-
28
- # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
29
- .grunt
30
-
31
- # node-waf configuration
32
- .lock-wscript
33
-
34
- # Compiled binary addons (http://nodejs.org/api/addons.html)
35
- build/Release
36
-
37
- # Dependency directories
38
- node_modules
39
- jspm_packages
40
-
41
- # Optional npm cache directory
42
- .npm
43
-
44
- # Optional eslint cache
45
- .eslintcache
46
-
47
- # Optional REPL history
48
- .node_repl_history
49
-
50
- ## OS X
51
-
52
- *.DS_Store
53
- .AppleDouble
54
- .LSOverride
55
-
56
- # Icon must end with two \r
57
- Icon
58
-
59
-
60
- # Thumbnails
61
- ._*
62
-
63
- # Files that might appear in the root of a volume
64
- .DocumentRevisions-V100
65
- .fseventsd
66
- .Spotlight-V100
67
- .TemporaryItems
68
- .Trashes
69
- .VolumeIcon.icns
70
- .com.apple.timemachine.donotpresent
71
-
72
- # Directories potentially created on remote AFP share
73
- .AppleDB
74
- .AppleDesktop
75
- Network Trash Folder
76
- Temporary Items
77
- .apdisk
package/.travis.yml DELETED
@@ -1,21 +0,0 @@
1
- dist: trusty
2
- language: node_js
3
- node_js:
4
- - '8'
5
- - '6'
6
- - '4'
7
- services:
8
- - redis-server
9
- - mongodb
10
- - mysql
11
- addons:
12
- postgresql: '9.5'
13
- before_script:
14
- - psql -c 'create database keyv_test;' -U postgres
15
- - mysql -u root -e 'CREATE DATABASE keyv_test;'
16
- - mysql -u root -e 'GRANT ALL PRIVILEGES ON keyv_test.* TO 'mysql'@'localhost';'
17
- script: npm run test:full
18
- after_success: npm run coverage
19
- notifications:
20
- email:
21
- on_success: never
package/media/logo.svg DELETED
@@ -1,10 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
- <svg width="512" height="512" viewBox="0 0 512 512" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
3
- <defs>
4
- <linearGradient x1="30%" y1="20%" x2="65%" y2="50%" id="gradient">
5
- <stop stop-color="#24C6DC" offset="0%"></stop>
6
- <stop stop-color="#C86DD7" offset="100%"></stop>
7
- </linearGradient>
8
- </defs>
9
- <path fill="url(#gradient)" fill-rule="nonzero" d="M189 196v-17.4c13.7 10.2 40.2 15 66.5 15 26.3 0 52.8-4.8 66.5-15V196c0 12-30 21.8-66.5 21.8-36.6 0-66.5-9.8-66.5-21.8zm133 16.6v19.8c-3.7 11-32.3 19.4-66.5 19.4s-62.8-8.5-66.5-19.4v-20c13.7 10.4 40.2 15 66.5 15 26.3 0 52.8-4.6 66.5-14.8zM189 162v-17.4c13.7 10.2 40.2 15 66.5 15 26.3 0 52.8-4.8 66.5-15V162c0 12-30 22-66.5 22-36.6 0-66.5-10-66.5-22zm66.5-12c-36.7 0-66.5-10-66.5-22s29.8-21.8 66.5-21.8S322 116 322 128s-29.8 22-66.5 22zm.5 362C114.6 512 0 397.4 0 256S114.6 0 256 0s256 114.6 256 256-114.6 256-256 256zm0-3c139.7 0 253-113.3 253-253S395.6 3 256 3 3 116.4 3 256s113.3 253 253 253zM122 351.2l56-51h5l-56.5 51 59.3 60h-5l-58.7-59.7v59.8h-3.5v-111h3.4v51zm77 23.3c0 4.4.7 8.7 2 13 1.3 4.3 3.3 8 6 11.5 2.6 3.5 6 6.3 10 8.4 4 2 9 3 14.5 3 5.7 0 10.7-1 15-3.3 4.5-2 8.3-5.4 11.5-9.7l2.5 2.2c-3.5 4.6-7.7 8-12.4 10.5-4.7 2.4-10.2 3.6-16.5 3.6-5.3 0-10.2-1-14.6-3-4.5-1.8-8.3-4.5-11.5-8-3.2-3.4-5.6-7.5-7.4-12.3-1.6-5-2.5-10-2.5-15.7 0-5.5 1-10.5 2.7-15 1.8-4.7 4.2-8.7 7.3-12 3-3.4 6.7-6 11-7.8 4-2 8.6-2.8 13.4-2.8 5.6 0 10.5 1 14.7 3 4.2 2 7.7 4.6 10.4 8 3 3 5 6.8 6.3 11 1.4 4 2 8.3 2 12.6v3H199zm61-3c0-4.7-.8-9-2.4-13-1.5-3.8-3.5-7-6.2-10-2.6-2.6-5.7-4.8-9.4-6.3-3.6-1.5-7.7-2.3-12.2-2.3-5.5 0-10 1-14 3.4-4 2.2-7 5-9.5 8.3-2.5 3.3-4.3 6.8-5.4 10.4-1.3 3.7-1.8 7-1.8 9.6h61zm40.7 63.7c-1.6 4.5-4 8-6.6 10.5-2.7 2.4-6.3 3.6-10.7 3.6-1 0-2 0-3.3-.3-1.2 0-2.3-.3-3-.7l.8-3c.6.3 1.5.5 2.6.7 1 .2 2.2.3 3.2.3 2.2 0 4-.4 5.8-1.3 1.6-.8 3-2 4-3.2 1.3-1.3 2.2-2.8 3-4.4.8-1.7 1.5-3.4 2-5l7-19-28.5-74.2h3.6l26.6 70.4 25.6-70.4h3.7l-35.8 96zm76.3-24h-4.5l-28.4-72h3.8l27 68.7h.2l26-68.8h3.7L377 411.4z"></path>
10
- </svg>
package/test/keyv.js DELETED
@@ -1,84 +0,0 @@
1
- import test from 'ava';
2
- import tk from 'timekeeper';
3
- import keyvTestSuite from '@keyv/test-suite';
4
- import Keyv from 'this';
5
-
6
- test.serial('Keyv is a class', t => {
7
- t.is(typeof Keyv, 'function');
8
- t.throws(() => Keyv()); // eslint-disable-line new-cap
9
- t.notThrows(() => new Keyv());
10
- });
11
-
12
- test.serial('Keyv accepts storage adapters', async t => {
13
- const store = new Map();
14
- const keyv = new Keyv({ store });
15
- t.is(store.size, 0);
16
- await keyv.set('foo', 'bar');
17
- t.is(await keyv.get('foo'), 'bar');
18
- t.is(store.size, 1);
19
- });
20
-
21
- test.serial('Keyv hands tll functionality over to ttl supporting stores', async t => {
22
- t.plan(3);
23
- const store = new Map();
24
- store.ttlSupport = true;
25
- const storeSet = store.set;
26
- store.set = (key, val, ttl) => {
27
- t.is(ttl, 100);
28
- storeSet.call(store, key, val, ttl);
29
- };
30
- const keyv = new Keyv({ store });
31
- await keyv.set('foo', 'bar', 100);
32
- t.is(await keyv.get('foo'), 'bar');
33
- tk.freeze(Date.now() + 150);
34
- t.is(await keyv.get('foo'), 'bar');
35
- tk.reset();
36
- });
37
-
38
- test.serial('Keyv respects default tll option', async t => {
39
- const store = new Map();
40
- const keyv = new Keyv({ store, ttl: 100 });
41
- await keyv.set('foo', 'bar');
42
- t.is(await keyv.get('foo'), 'bar');
43
- tk.freeze(Date.now() + 150);
44
- t.is(await keyv.get('foo'), undefined);
45
- tk.reset();
46
- });
47
-
48
- test.serial('.set(key, val, ttl) overwrites default tll option', async t => {
49
- const startTime = Date.now();
50
- tk.freeze(startTime);
51
- const store = new Map();
52
- const keyv = new Keyv({ store, ttl: 200 });
53
- await keyv.set('foo', 'bar');
54
- await keyv.set('fizz', 'buzz', 100);
55
- await keyv.set('ping', 'pong', 300);
56
- t.is(await keyv.get('foo'), 'bar');
57
- t.is(await keyv.get('fizz'), 'buzz');
58
- t.is(await keyv.get('ping'), 'pong');
59
- tk.freeze(startTime + 150);
60
- t.is(await keyv.get('foo'), 'bar');
61
- t.is(await keyv.get('fizz'), undefined);
62
- t.is(await keyv.get('ping'), 'pong');
63
- tk.freeze(startTime + 250);
64
- t.is(await keyv.get('foo'), undefined);
65
- t.is(await keyv.get('ping'), 'pong');
66
- tk.freeze(startTime + 350);
67
- t.is(await keyv.get('ping'), undefined);
68
- tk.reset();
69
- });
70
-
71
- test.serial('.set(key, val, ttl) where ttl is "0" overwrites default tll option and sets key to never expire', async t => {
72
- const startTime = Date.now();
73
- tk.freeze(startTime);
74
- const store = new Map();
75
- const keyv = new Keyv({ store, ttl: 200 });
76
- await keyv.set('foo', 'bar', 0);
77
- t.is(await keyv.get('foo'), 'bar');
78
- tk.freeze(startTime + 250);
79
- t.is(await keyv.get('foo'), 'bar');
80
- tk.reset();
81
- });
82
-
83
- const store = () => new Map();
84
- keyvTestSuite(test, Keyv, store);
@@ -1,9 +0,0 @@
1
- import test from 'ava';
2
- import keyvTestSuite, { keyvOfficialTests } from '@keyv/test-suite';
3
- import Keyv from 'this';
4
- import KeyvMongo from '@keyv/mongo';
5
-
6
- keyvOfficialTests(test, Keyv, 'mongodb://127.0.0.1:27017', 'mongodb://127.0.0.1:1234');
7
-
8
- const store = () => new KeyvMongo('mongodb://127.0.0.1:27017');
9
- keyvTestSuite(test, Keyv, store);
@@ -1,9 +0,0 @@
1
- import test from 'ava';
2
- import keyvTestSuite, { keyvOfficialTests } from '@keyv/test-suite';
3
- import Keyv from 'this';
4
- import KeyvMysql from '@keyv/mysql';
5
-
6
- keyvOfficialTests(test, Keyv, 'mysql://mysql@localhost/keyv_test', 'mysql://foo');
7
-
8
- const store = () => new KeyvMysql('mysql://mysql@localhost/keyv_test');
9
- keyvTestSuite(test, Keyv, store);
@@ -1,9 +0,0 @@
1
- import test from 'ava';
2
- import keyvTestSuite, { keyvOfficialTests } from '@keyv/test-suite';
3
- import Keyv from 'this';
4
- import KeyvPostgres from '@keyv/postgres';
5
-
6
- keyvOfficialTests(test, Keyv, 'postgresql://postgres@localhost:5432/keyv_test', 'postgresql://foo');
7
-
8
- const store = () => new KeyvPostgres('postgresql://postgres@localhost:5432/keyv_test');
9
- keyvTestSuite(test, Keyv, store);
@@ -1,9 +0,0 @@
1
- import test from 'ava';
2
- import keyvTestSuite, { keyvOfficialTests } from '@keyv/test-suite';
3
- import Keyv from 'this';
4
- import KeyvRedis from '@keyv/redis';
5
-
6
- keyvOfficialTests(test, Keyv, 'redis://localhost', 'redis://foo');
7
-
8
- const store = () => new KeyvRedis('redis://localhost');
9
- keyvTestSuite(test, Keyv, store);
@@ -1,9 +0,0 @@
1
- import test from 'ava';
2
- import keyvTestSuite, { keyvOfficialTests } from '@keyv/test-suite';
3
- import Keyv from 'this';
4
- import KeyvSqlite from '@keyv/sqlite';
5
-
6
- keyvOfficialTests(test, Keyv, 'sqlite://test/testdb.sqlite', 'sqlite://non/existent/database.sqlite');
7
-
8
- const store = () => new KeyvSqlite('sqlite://test/testdb.sqlite');
9
- keyvTestSuite(test, Keyv, store);