keyv 4.2.1 → 4.2.6
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 +75 -9
- 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.6",
|
|
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
|
@@ -1,23 +1,89 @@
|
|
|
1
1
|
import {EventEmitter} from 'events';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
type WithRequiredProperties<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
4
|
+
|
|
5
|
+
declare class Keyv<Value = any, Options extends Record<string, any> = Record<string, unknown>> extends EventEmitter {
|
|
6
|
+
/**
|
|
7
|
+
* `this.opts` is an object containing at least the properties listed
|
|
8
|
+
* below. However, `Keyv.Options` allows arbitrary properties as well.
|
|
9
|
+
* These properties can be specified as the second type parameter to `Keyv`.
|
|
10
|
+
*/
|
|
11
|
+
opts: WithRequiredProperties<
|
|
12
|
+
Keyv.Options<Value>,
|
|
13
|
+
'deserialize' | 'namespace' | 'serialize' | 'store' | 'uri'
|
|
14
|
+
> &
|
|
15
|
+
Options;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
|
|
19
|
+
*/
|
|
20
|
+
constructor(options?: Keyv.Options<Value> & Options);
|
|
21
|
+
/**
|
|
22
|
+
* @param uri The connection string URI.
|
|
23
|
+
*
|
|
24
|
+
* Merged into the options object as options.uri.
|
|
25
|
+
* @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
|
|
26
|
+
*/
|
|
27
|
+
constructor(uri?: string, options?: Keyv.Options<Value> & Options);
|
|
28
|
+
|
|
29
|
+
/** Returns the value. */
|
|
30
|
+
get<Raw extends boolean = false>(key: string | string[], options?: {raw?: Raw}):
|
|
31
|
+
Promise<(Raw extends false
|
|
32
|
+
? Value
|
|
33
|
+
: Keyv.DeserializedData<Value>) | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Set a value.
|
|
36
|
+
*
|
|
37
|
+
* By default keys are persistent. You can set an expiry TTL in milliseconds.
|
|
38
|
+
*/
|
|
39
|
+
set(key: string, value: Value, ttl?: number): Promise<true>;
|
|
40
|
+
/**
|
|
41
|
+
* Deletes an entry.
|
|
42
|
+
*
|
|
43
|
+
* Returns `true` if the key existed, `false` if not.
|
|
44
|
+
*/
|
|
45
|
+
delete(key: string | string[]): Promise<boolean>;
|
|
46
|
+
/** Delete all entries in the current namespace. */
|
|
8
47
|
clear(): Promise<void>;
|
|
48
|
+
/** Check if key exists in current namespace. */
|
|
9
49
|
has(key: string): Promise<boolean>;
|
|
50
|
+
/** Iterator */
|
|
51
|
+
iterator(namespace: string | undefined): AsyncGenerator<any, void, any>;
|
|
10
52
|
}
|
|
11
53
|
|
|
12
54
|
declare namespace Keyv {
|
|
13
|
-
interface Options {
|
|
55
|
+
interface Options<Value> {
|
|
56
|
+
[key: string]: any;
|
|
57
|
+
|
|
58
|
+
/** Namespace for the current instance. */
|
|
14
59
|
namespace?: string | undefined;
|
|
15
|
-
|
|
16
|
-
|
|
60
|
+
/** A custom serialization function. */
|
|
61
|
+
serialize?: ((data: DeserializedData<Value>) => string) | undefined;
|
|
62
|
+
/** A custom deserialization function. */
|
|
63
|
+
deserialize?: ((data: string) => DeserializedData<Value> | undefined) | undefined;
|
|
64
|
+
/** The connection string URI. */
|
|
65
|
+
uri?: string | undefined;
|
|
66
|
+
/** The storage adapter instance to be used by Keyv. */
|
|
67
|
+
store?: Store<Value> | undefined;
|
|
68
|
+
/** Default TTL. Can be overridden by specififying a TTL on `.set()`. */
|
|
17
69
|
ttl?: number | undefined;
|
|
70
|
+
/** Specify an adapter to use. e.g `'redis'` or `'mongodb'`. */
|
|
71
|
+
adapter?: 'redis' | 'mongodb' | 'mongo' | 'sqlite' | 'postgresql' | 'postgres' | 'mysql' | undefined;
|
|
72
|
+
/** Enable compression option **/
|
|
73
|
+
compress?: Record<string, unknown> | undefined;
|
|
18
74
|
}
|
|
19
75
|
|
|
20
|
-
|
|
76
|
+
interface DeserializedData<Value> {
|
|
77
|
+
value: Value; expires: number | undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface Store<Value> {
|
|
81
|
+
get(key: string): Value | Promise<Value | undefined> | undefined;
|
|
82
|
+
set(key: string, value: Value, ttl?: number): any;
|
|
83
|
+
delete(key: string): boolean | Promise<boolean>;
|
|
84
|
+
clear(): void | Promise<void>;
|
|
85
|
+
has(key: string): boolean | Promise<boolean>;
|
|
86
|
+
}
|
|
21
87
|
}
|
|
22
88
|
|
|
23
89
|
export = Keyv;
|
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;
|