keyv 3.1.0 → 4.0.3

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 +17 -5
  2. package/package.json +5 -5
  3. package/src/index.js +21 -13
package/README.md CHANGED
@@ -142,6 +142,9 @@ The following are third-party storage adapters compatible with Keyv:
142
142
  - [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple "Least Recently Used" (LRU) cache
143
143
  - [keyv-file](https://github.com/zaaack/keyv-file) - File system storage adapter for Keyv
144
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
145
148
 
146
149
  ## Add Cache Support to your Module
147
150
 
@@ -253,23 +256,32 @@ Set a value.
253
256
 
254
257
  By default keys are persistent. You can set an expiry TTL in milliseconds.
255
258
 
256
- Returns `true`.
259
+ Returns a promise which resolves to `true`.
257
260
 
258
- #### .get(key)
261
+ #### .get(key, [options])
259
262
 
260
- Returns the value.
263
+ Returns a promise which resolves to the retrieved value.
264
+
265
+ ##### options.raw
266
+
267
+ Type: `Boolean`<br>
268
+ Default: `false`
269
+
270
+ If set to true the raw DB object Keyv stores internally will be returned instead of just the value.
271
+
272
+ This contains the TTL timestamp.
261
273
 
262
274
  #### .delete(key)
263
275
 
264
276
  Deletes an entry.
265
277
 
266
- Returns `true` if the key existed, `false` if not.
278
+ Returns a promise which resolves to `true` if the key existed, `false` if not.
267
279
 
268
280
  #### .clear()
269
281
 
270
282
  Delete all entries in the current namespace.
271
283
 
272
- Returns `undefined`.
284
+ Returns a promise which is resolved when the entries have been cleared.
273
285
 
274
286
  ## License
275
287
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keyv",
3
- "version": "3.1.0",
3
+ "version": "4.0.3",
4
4
  "description": "Simple key-value storage with support for multiple backends",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -29,10 +29,10 @@
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.25.0",
35
+ "ava": "^2.2.0",
36
36
  "coveralls": "^3.0.0",
37
37
  "eslint-config-xo-lukechilds": "^1.0.0",
38
38
  "@keyv/mongo": "*",
@@ -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
46
  "timekeeper": "^2.0.0",
47
- "xo": "^0.20.1"
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
 
@@ -49,52 +50,59 @@ class Keyv extends EventEmitter {
49
50
  return `${this.opts.namespace}:${key}`;
50
51
  }
51
52
 
52
- get(key) {
53
- key = this._getKeyPrefix(key);
54
- const store = this.opts.store;
53
+ get(key, opts) {
54
+ const keyPrefixed = this._getKeyPrefix(key);
55
+ const { store } = this.opts;
55
56
  return Promise.resolve()
56
- .then(() => store.get(key))
57
+ .then(() => store.get(keyPrefixed))
58
+ .then(data => {
59
+ return (typeof data === 'string') ? this.opts.deserialize(data) : data;
60
+ })
57
61
  .then(data => {
58
- data = (typeof data === 'string') ? this.opts.deserialize(data) : data;
59
62
  if (data === undefined) {
60
63
  return undefined;
61
64
  }
65
+
62
66
  if (typeof data.expires === 'number' && Date.now() > data.expires) {
63
67
  this.delete(key);
64
68
  return undefined;
65
69
  }
66
- return data.value;
70
+
71
+ return (opts && opts.raw) ? data : data.value;
67
72
  });
68
73
  }
69
74
 
70
75
  set(key, value, ttl) {
71
- key = this._getKeyPrefix(key);
76
+ const keyPrefixed = this._getKeyPrefix(key);
72
77
  if (typeof ttl === 'undefined') {
73
78
  ttl = this.opts.ttl;
74
79
  }
80
+
75
81
  if (ttl === 0) {
76
82
  ttl = undefined;
77
83
  }
78
- const store = this.opts.store;
84
+
85
+ const { store } = this.opts;
79
86
 
80
87
  return Promise.resolve()
81
88
  .then(() => {
82
89
  const expires = (typeof ttl === 'number') ? (Date.now() + ttl) : null;
83
90
  value = { value, expires };
84
- return store.set(key, this.opts.serialize(value), ttl);
91
+ return this.opts.serialize(value);
85
92
  })
93
+ .then(value => store.set(keyPrefixed, value, ttl))
86
94
  .then(() => true);
87
95
  }
88
96
 
89
97
  delete(key) {
90
- key = this._getKeyPrefix(key);
91
- const store = this.opts.store;
98
+ const keyPrefixed = this._getKeyPrefix(key);
99
+ const { store } = this.opts;
92
100
  return Promise.resolve()
93
- .then(() => store.delete(key));
101
+ .then(() => store.delete(keyPrefixed));
94
102
  }
95
103
 
96
104
  clear() {
97
- const store = this.opts.store;
105
+ const { store } = this.opts;
98
106
  return Promise.resolve()
99
107
  .then(() => store.clear());
100
108
  }