keyv 4.2.2 → 4.2.7
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/package.json +12 -12
- package/src/index.d.ts +15 -2
- package/src/index.js +31 -24
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keyv",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.7",
|
|
4
4
|
"description": "Simple key-value storage with support for multiple backends",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -32,26 +32,26 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/jaredwray/keyv",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"compress-brotli": "^1.3.
|
|
35
|
+
"compress-brotli": "^1.3.8",
|
|
36
36
|
"json-buffer": "3.0.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@keyv/test-suite": "*",
|
|
40
|
-
"ava": "^4.
|
|
40
|
+
"ava": "^4.2.0",
|
|
41
41
|
"eslint-plugin-promise": "^6.0.0",
|
|
42
42
|
"nyc": "^15.1.0",
|
|
43
43
|
"pify": "5.0.0",
|
|
44
44
|
"this": "^1.1.0",
|
|
45
45
|
"timekeeper": "^2.2.0",
|
|
46
46
|
"tsd": "^0.20.0",
|
|
47
|
-
"typescript": "^4.6.
|
|
47
|
+
"typescript": "^4.6.4",
|
|
48
48
|
"xo": "^0.48.0"
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
},
|
|
50
|
+
"tsd": {
|
|
51
|
+
"directory": "test"
|
|
52
|
+
},
|
|
53
|
+
"types": "./src/index.d.ts",
|
|
54
|
+
"files": [
|
|
55
|
+
"src"
|
|
56
|
+
]
|
|
57
57
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ declare class Keyv<Value = any, Options extends Record<string, any> = Record<str
|
|
|
27
27
|
constructor(uri?: string, options?: Keyv.Options<Value> & Options);
|
|
28
28
|
|
|
29
29
|
/** Returns the value. */
|
|
30
|
-
get<Raw extends boolean = false>(key: string, options?: {raw?: Raw}):
|
|
30
|
+
get<Raw extends boolean = false>(key: string | string[], options?: {raw?: Raw}):
|
|
31
31
|
Promise<(Raw extends false
|
|
32
32
|
? Value
|
|
33
33
|
: Keyv.DeserializedData<Value>) | undefined>;
|
|
@@ -42,9 +42,19 @@ declare class Keyv<Value = any, Options extends Record<string, any> = Record<str
|
|
|
42
42
|
*
|
|
43
43
|
* Returns `true` if the key existed, `false` if not.
|
|
44
44
|
*/
|
|
45
|
-
delete(key: string): Promise<boolean>;
|
|
45
|
+
delete(key: string | string[]): Promise<boolean>;
|
|
46
46
|
/** Delete all entries in the current namespace. */
|
|
47
47
|
clear(): Promise<void>;
|
|
48
|
+
/** Check if key exists in current namespace. */
|
|
49
|
+
has(key: string): Promise<boolean>;
|
|
50
|
+
/** Iterator */
|
|
51
|
+
iterator(namespace: string | undefined): AsyncGenerator<any, void, any>;
|
|
52
|
+
/**
|
|
53
|
+
* Closes the connection.
|
|
54
|
+
*
|
|
55
|
+
* Returns `undefined` when the connection closes.
|
|
56
|
+
*/
|
|
57
|
+
disconnect(): Promise<void>;
|
|
48
58
|
}
|
|
49
59
|
|
|
50
60
|
declare namespace Keyv {
|
|
@@ -65,6 +75,8 @@ declare namespace Keyv {
|
|
|
65
75
|
ttl?: number | undefined;
|
|
66
76
|
/** Specify an adapter to use. e.g `'redis'` or `'mongodb'`. */
|
|
67
77
|
adapter?: 'redis' | 'mongodb' | 'mongo' | 'sqlite' | 'postgresql' | 'postgres' | 'mysql' | undefined;
|
|
78
|
+
/** Enable compression option **/
|
|
79
|
+
compress?: Record<string, unknown> | undefined;
|
|
68
80
|
}
|
|
69
81
|
|
|
70
82
|
interface DeserializedData<Value> {
|
|
@@ -76,6 +88,7 @@ declare namespace Keyv {
|
|
|
76
88
|
set(key: string, value: Value, ttl?: number): any;
|
|
77
89
|
delete(key: string): boolean | Promise<boolean>;
|
|
78
90
|
clear(): void | Promise<void>;
|
|
91
|
+
has(key: string): boolean | Promise<boolean>;
|
|
79
92
|
}
|
|
80
93
|
}
|
|
81
94
|
|
package/src/index.js
CHANGED
|
@@ -14,9 +14,11 @@ const loadStore = options => {
|
|
|
14
14
|
postgres: '@keyv/postgres',
|
|
15
15
|
mysql: '@keyv/mysql',
|
|
16
16
|
etcd: '@keyv/etcd',
|
|
17
|
+
offline: '@keyv/offline',
|
|
18
|
+
tiered: '@keyv/tiered',
|
|
17
19
|
};
|
|
18
20
|
if (options.adapter || options.uri) {
|
|
19
|
-
const adapter = options.adapter || /^[
|
|
21
|
+
const adapter = options.adapter || /^[^:+]*/.exec(options.uri)[0];
|
|
20
22
|
return new (require(adapters[adapter]))(options);
|
|
21
23
|
}
|
|
22
24
|
|
|
@@ -29,6 +31,7 @@ const iterableAdapters = [
|
|
|
29
31
|
'mysql',
|
|
30
32
|
'mongo',
|
|
31
33
|
'redis',
|
|
34
|
+
'tiered',
|
|
32
35
|
];
|
|
33
36
|
|
|
34
37
|
class Keyv extends EventEmitter {
|
|
@@ -62,24 +65,23 @@ class Keyv extends EventEmitter {
|
|
|
62
65
|
|
|
63
66
|
this.opts.store.namespace = this.opts.namespace;
|
|
64
67
|
|
|
65
|
-
const generateIterator = iterator =>
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (typeof data.expires === 'number' && Date.now() > data.expires) {
|
|
76
|
-
this.delete(key);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
68
|
+
const generateIterator = iterator => async function * () {
|
|
69
|
+
for await (const [key, raw] of typeof iterator === 'function'
|
|
70
|
+
? iterator(this.opts.store.namespace)
|
|
71
|
+
: iterator) {
|
|
72
|
+
const data = this.opts.deserialize(raw);
|
|
73
|
+
if (this.opts.store.namespace && !key.includes(this.opts.store.namespace)) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
79
76
|
|
|
80
|
-
|
|
77
|
+
if (typeof data.expires === 'number' && Date.now() > data.expires) {
|
|
78
|
+
this.delete(key);
|
|
79
|
+
continue;
|
|
81
80
|
}
|
|
82
|
-
|
|
81
|
+
|
|
82
|
+
yield [this._getKeyUnprefix(key), data.value];
|
|
83
|
+
}
|
|
84
|
+
};
|
|
83
85
|
|
|
84
86
|
// Attach iterators
|
|
85
87
|
if (typeof this.opts.store[Symbol.iterator] === 'function' && this.opts.store instanceof Map) {
|
|
@@ -104,12 +106,10 @@ class Keyv extends EventEmitter {
|
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
_getKeyUnprefix(key) {
|
|
107
|
-
return
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
.join(':')
|
|
112
|
-
: key;
|
|
109
|
+
return key
|
|
110
|
+
.split(':')
|
|
111
|
+
.splice(1)
|
|
112
|
+
.join(':');
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
get(key, options) {
|
|
@@ -151,13 +151,13 @@ class Keyv extends EventEmitter {
|
|
|
151
151
|
.then(() => isArray ? store.getMany(keyPrefixed) : store.get(keyPrefixed))
|
|
152
152
|
.then(data => (typeof data === 'string') ? this.opts.deserialize(data) : data)
|
|
153
153
|
.then(data => {
|
|
154
|
-
// Console.log('get', data);
|
|
155
154
|
if (data === undefined || data === null) {
|
|
156
155
|
return undefined;
|
|
157
156
|
}
|
|
158
157
|
|
|
159
158
|
if (isArray) {
|
|
160
159
|
const result = [];
|
|
160
|
+
|
|
161
161
|
if (data.length === 0) {
|
|
162
162
|
return [];
|
|
163
163
|
}
|
|
@@ -259,6 +259,13 @@ class Keyv extends EventEmitter {
|
|
|
259
259
|
return value !== undefined;
|
|
260
260
|
});
|
|
261
261
|
}
|
|
262
|
+
|
|
263
|
+
disconnect() {
|
|
264
|
+
const {store} = this.opts;
|
|
265
|
+
if (typeof store.disconnect === 'function') {
|
|
266
|
+
return store.disconnect();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
262
269
|
}
|
|
263
270
|
|
|
264
271
|
module.exports = Keyv;
|