keyv 5.1.3 → 5.2.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 +158 -36
- package/dist/index.cjs +225 -28
- package/dist/index.d.cts +125 -6
- package/dist/index.d.ts +125 -6
- package/dist/index.js +225 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">
|
|
2
|
+
<img width="250" src="https://jaredwray.com/images/keyv.svg" alt="keyv">
|
|
3
|
+
<br>
|
|
4
|
+
<br>
|
|
5
|
+
</h1>
|
|
2
6
|
|
|
3
7
|
> Simple key-value storage with support for multiple backends
|
|
4
8
|
|
|
@@ -9,7 +13,7 @@
|
|
|
9
13
|
|
|
10
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.
|
|
11
15
|
|
|
12
|
-
|
|
16
|
+
# Features
|
|
13
17
|
|
|
14
18
|
There are a few existing modules similar to Keyv, however Keyv is different because it:
|
|
15
19
|
|
|
@@ -24,7 +28,7 @@ There are a few existing modules similar to Keyv, however Keyv is different beca
|
|
|
24
28
|
- Connection errors are passed through (db failures won't kill your app)
|
|
25
29
|
- Supports the current active LTS version of Node.js or higher
|
|
26
30
|
|
|
27
|
-
|
|
31
|
+
# Usage
|
|
28
32
|
|
|
29
33
|
Install Keyv.
|
|
30
34
|
|
|
@@ -36,6 +40,7 @@ By default everything is stored in memory, you can optionally also install a sto
|
|
|
36
40
|
|
|
37
41
|
```
|
|
38
42
|
npm install --save @keyv/redis
|
|
43
|
+
npm install --save @keyv/valkey
|
|
39
44
|
npm install --save @keyv/mongo
|
|
40
45
|
npm install --save @keyv/sqlite
|
|
41
46
|
npm install --save @keyv/postgres
|
|
@@ -50,11 +55,11 @@ First, create a new Keyv instance.
|
|
|
50
55
|
import Keyv from 'keyv';
|
|
51
56
|
```
|
|
52
57
|
|
|
53
|
-
|
|
58
|
+
# Type-safe Usage
|
|
54
59
|
|
|
55
60
|
You can create a `Keyv` instance with a generic type to enforce type safety for the values stored. Additionally, both the `get` and `set` methods support specifying custom types for specific use cases.
|
|
56
61
|
|
|
57
|
-
|
|
62
|
+
## Example with Instance-level Generic Type:
|
|
58
63
|
|
|
59
64
|
```ts
|
|
60
65
|
const keyv = new Keyv<number>(); // Instance handles only numbers
|
|
@@ -62,7 +67,7 @@ await keyv.set('key1', 123);
|
|
|
62
67
|
const value = await keyv.get('key1'); // value is inferred as number
|
|
63
68
|
```
|
|
64
69
|
|
|
65
|
-
|
|
70
|
+
## Example with Method-level Generic Type:
|
|
66
71
|
|
|
67
72
|
You can also specify a type directly in the `get` or `set` methods, allowing flexibility for different types of values within the same instance.
|
|
68
73
|
|
|
@@ -78,7 +83,7 @@ const numValue = await keyv.get<number>('key3'); // Explicitly typed as number
|
|
|
78
83
|
|
|
79
84
|
This makes `Keyv` highly adaptable to different data types while maintaining type safety.
|
|
80
85
|
|
|
81
|
-
|
|
86
|
+
# Using Storage Adapters
|
|
82
87
|
|
|
83
88
|
Once you have created your Keyv instance you can use it as a simple key-value store with `in-memory` by default. To use a storage adapter, create an instance of the adapter and pass it to the Keyv constructor. Here are some examples:
|
|
84
89
|
|
|
@@ -124,7 +129,7 @@ await keyv.clear(); // undefined
|
|
|
124
129
|
|
|
125
130
|
It's is just that simple! Keyv is designed to be simple and easy to use.
|
|
126
131
|
|
|
127
|
-
|
|
132
|
+
# Namespaces
|
|
128
133
|
|
|
129
134
|
You can namespace your Keyv instance to avoid key collisions and allow you to clear only a certain namespace while using the same database.
|
|
130
135
|
|
|
@@ -141,7 +146,7 @@ await users.get('foo'); // undefined
|
|
|
141
146
|
await cache.get('foo'); // 'cache'
|
|
142
147
|
```
|
|
143
148
|
|
|
144
|
-
|
|
149
|
+
# Events
|
|
145
150
|
|
|
146
151
|
Keyv is a custom `EventEmitter` and will emit an `'error'` event if there is an error. In addition it will emit a `clear` and `disconnect` event when the corresponding methods are called.
|
|
147
152
|
|
|
@@ -156,7 +161,7 @@ keyv.on('clear', handleClear);
|
|
|
156
161
|
keyv.on('disconnect', handleDisconnect);
|
|
157
162
|
```
|
|
158
163
|
|
|
159
|
-
|
|
164
|
+
# Hooks
|
|
160
165
|
|
|
161
166
|
Keyv supports hooks for `get`, `set`, and `delete` methods. Hooks are useful for logging, debugging, and other custom functionality. Here is a list of all the hooks:
|
|
162
167
|
|
|
@@ -202,7 +207,7 @@ Now this key will have prefix- added to it before it is set.
|
|
|
202
207
|
In `PRE_DELETE` and `POST_DELETE` hooks, the value could be a single item or an `Array`. This is based on the fact that `delete` can accept a single key or an `Array` of keys.
|
|
203
208
|
|
|
204
209
|
|
|
205
|
-
|
|
210
|
+
# Custom Serializers
|
|
206
211
|
|
|
207
212
|
Keyv uses [`buffer`](https://nodejs.org/api/buffer.html) for data serialization to ensure consistency across different backends.
|
|
208
213
|
|
|
@@ -214,7 +219,15 @@ const keyv = new Keyv({ serialize: JSON.stringify, deserialize: JSON.parse });
|
|
|
214
219
|
|
|
215
220
|
**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.
|
|
216
221
|
|
|
217
|
-
|
|
222
|
+
If you do not want to use serialization you can set the `serialize` and `deserialize` functions to `undefined`. This will also turn off compression.
|
|
223
|
+
|
|
224
|
+
```js
|
|
225
|
+
const keyv = new Keyv();
|
|
226
|
+
keyv.serialize = undefined;
|
|
227
|
+
keyv.deserialize = undefined;
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
# Official Storage Adapters
|
|
218
231
|
|
|
219
232
|
The official storage adapters are covered by [over 150 integration tests](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml) to guarantee consistent behaviour. They are lightweight, efficient wrappers over the DB clients making use of indexes and native TTLs where available.
|
|
220
233
|
|
|
@@ -229,7 +242,7 @@ MySQL | [@keyv/mysql](https://github.com/jaredwray/keyv/tree/master/packages/mys
|
|
|
229
242
|
Etcd | [@keyv/etcd](https://github.com/jaredwray/keyv/tree/master/packages/etcd) | Yes
|
|
230
243
|
Memcache | [@keyv/memcache](https://github.com/jaredwray/keyv/tree/master/packages/memcache) | Yes
|
|
231
244
|
|
|
232
|
-
|
|
245
|
+
# Third-party Storage Adapters
|
|
233
246
|
|
|
234
247
|
You can also use third-party storage adapters or build your own. Keyv will wrap these storage adapters in TTL functionality and handle complex types internally.
|
|
235
248
|
|
|
@@ -269,7 +282,7 @@ The following are third-party storage adapters compatible with Keyv:
|
|
|
269
282
|
- [keyv-arango](https://github.com/TimMikeladze/keyv-arango) - ArangoDB storage adapter for Keyv
|
|
270
283
|
- [keyv-momento](https://github.com/momentohq/node-keyv-adaptor/) - Momento storage adapter for Keyv
|
|
271
284
|
|
|
272
|
-
|
|
285
|
+
# Add Cache Support to your Module
|
|
273
286
|
|
|
274
287
|
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.
|
|
275
288
|
|
|
@@ -304,7 +317,7 @@ const awesomeModule = new AwesomeModule({ cache: 'redis://localhost' });
|
|
|
304
317
|
const awesomeModule = new AwesomeModule({ cache: some3rdPartyStore });
|
|
305
318
|
```
|
|
306
319
|
|
|
307
|
-
|
|
320
|
+
# Compression
|
|
308
321
|
|
|
309
322
|
Keyv supports `gzip` and `brotli` compression. To enable compression, pass the `compress` option to the constructor.
|
|
310
323
|
|
|
@@ -318,7 +331,7 @@ const keyv = new Keyv({ compression: KeyvGzip });
|
|
|
318
331
|
|
|
319
332
|
You can also pass a custom compression function to the `compression` option. Following the pattern of the official compression adapters.
|
|
320
333
|
|
|
321
|
-
|
|
334
|
+
## Want to build your own CompressionAdapter?
|
|
322
335
|
|
|
323
336
|
Great! Keyv is designed to be easily extended. You can build your own compression adapter by following the pattern of the official compression adapters based on this interface:
|
|
324
337
|
|
|
@@ -340,15 +353,15 @@ import KeyvGzip from '@keyv/compress-gzip';
|
|
|
340
353
|
keyvCompresstionTests(test, new KeyvGzip());
|
|
341
354
|
```
|
|
342
355
|
|
|
343
|
-
|
|
356
|
+
# API
|
|
344
357
|
|
|
345
|
-
|
|
358
|
+
## new Keyv([storage-adapter], [options]) or new Keyv([options])
|
|
346
359
|
|
|
347
360
|
Returns a new Keyv instance.
|
|
348
361
|
|
|
349
362
|
The Keyv instance is also an `EventEmitter` that will emit an `'error'` event if the storage adapter connection fails.
|
|
350
363
|
|
|
351
|
-
|
|
364
|
+
## storage-adapter
|
|
352
365
|
|
|
353
366
|
Type: `KeyvStorageAdapter`<br />
|
|
354
367
|
Default: `undefined`
|
|
@@ -357,66 +370,66 @@ The connection string URI.
|
|
|
357
370
|
|
|
358
371
|
Merged into the options object as options.uri.
|
|
359
372
|
|
|
360
|
-
|
|
373
|
+
## .namespace
|
|
361
374
|
|
|
362
375
|
Type: `String`
|
|
363
376
|
Default: `'keyv'`
|
|
364
377
|
|
|
365
378
|
This is the namespace for the current instance. When you set it it will set it also on the storage adapter. This is the preferred way to set the namespace over `.opts.namespace`.
|
|
366
379
|
|
|
367
|
-
|
|
380
|
+
## options
|
|
368
381
|
|
|
369
382
|
Type: `Object`
|
|
370
383
|
|
|
371
384
|
The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
|
|
372
385
|
|
|
373
|
-
|
|
386
|
+
## options.namespace
|
|
374
387
|
|
|
375
388
|
Type: `String`<br />
|
|
376
389
|
Default: `'keyv'`
|
|
377
390
|
|
|
378
391
|
Namespace for the current instance.
|
|
379
392
|
|
|
380
|
-
|
|
393
|
+
## options.ttl
|
|
381
394
|
|
|
382
395
|
Type: `Number`<br />
|
|
383
396
|
Default: `undefined`
|
|
384
397
|
|
|
385
398
|
Default TTL. Can be overridden by specififying a TTL on `.set()`.
|
|
386
399
|
|
|
387
|
-
|
|
400
|
+
## options.compression
|
|
388
401
|
|
|
389
402
|
Type: `@keyv/compress-<compression_package_name>`<br />
|
|
390
403
|
Default: `undefined`
|
|
391
404
|
|
|
392
405
|
Compression package to use. See [Compression](#compression) for more details.
|
|
393
406
|
|
|
394
|
-
|
|
407
|
+
## options.serialize
|
|
395
408
|
|
|
396
409
|
Type: `Function`<br />
|
|
397
|
-
Default: `
|
|
410
|
+
Default: `JSON.stringify`
|
|
398
411
|
|
|
399
412
|
A custom serialization function.
|
|
400
413
|
|
|
401
|
-
|
|
414
|
+
## options.deserialize
|
|
402
415
|
|
|
403
416
|
Type: `Function`<br />
|
|
404
|
-
Default: `
|
|
417
|
+
Default: `JSON.parse`
|
|
405
418
|
|
|
406
419
|
A custom deserialization function.
|
|
407
420
|
|
|
408
|
-
|
|
421
|
+
## options.store
|
|
409
422
|
|
|
410
423
|
Type: `Storage adapter instance`<br />
|
|
411
424
|
Default: `new Map()`
|
|
412
425
|
|
|
413
426
|
The storage adapter instance to be used by Keyv.
|
|
414
427
|
|
|
415
|
-
|
|
428
|
+
# Keyv Instance
|
|
416
429
|
|
|
417
430
|
Keys must always be strings. Values can be of any type.
|
|
418
431
|
|
|
419
|
-
|
|
432
|
+
## .set(key, value, [ttl])
|
|
420
433
|
|
|
421
434
|
Set a value.
|
|
422
435
|
|
|
@@ -424,11 +437,11 @@ By default keys are persistent. You can set an expiry TTL in milliseconds.
|
|
|
424
437
|
|
|
425
438
|
Returns a promise which resolves to `true`.
|
|
426
439
|
|
|
427
|
-
|
|
440
|
+
## .get(key, [options])
|
|
428
441
|
|
|
429
442
|
Returns a promise which resolves to the retrieved value.
|
|
430
443
|
|
|
431
|
-
|
|
444
|
+
### options.raw
|
|
432
445
|
|
|
433
446
|
Type: `Boolean`<br />
|
|
434
447
|
Default: `false`
|
|
@@ -437,19 +450,19 @@ If set to true the raw DB object Keyv stores internally will be returned instead
|
|
|
437
450
|
|
|
438
451
|
This contains the TTL timestamp.
|
|
439
452
|
|
|
440
|
-
|
|
453
|
+
## .delete(key)
|
|
441
454
|
|
|
442
455
|
Deletes an entry.
|
|
443
456
|
|
|
444
457
|
Returns a promise which resolves to `true` if the key existed, `false` if not.
|
|
445
458
|
|
|
446
|
-
|
|
459
|
+
## .clear()
|
|
447
460
|
|
|
448
461
|
Delete all entries in the current namespace.
|
|
449
462
|
|
|
450
463
|
Returns a promise which is resolved when the entries have been cleared.
|
|
451
464
|
|
|
452
|
-
|
|
465
|
+
## .iterator()
|
|
453
466
|
|
|
454
467
|
Iterate over all entries of the current namespace.
|
|
455
468
|
|
|
@@ -462,6 +475,115 @@ for await (const [key, value] of this.keyv.iterator()) {
|
|
|
462
475
|
};
|
|
463
476
|
```
|
|
464
477
|
|
|
478
|
+
# API - Properties
|
|
479
|
+
|
|
480
|
+
## .namespace
|
|
481
|
+
|
|
482
|
+
Type: `String`
|
|
483
|
+
|
|
484
|
+
The namespace for the current instance. This will define the namespace for the current instance and the storage adapter. If you set the namespace to `undefined` it will no longer do key prefixing.
|
|
485
|
+
|
|
486
|
+
```js
|
|
487
|
+
const keyv = new Keyv({ namespace: 'my-namespace' });
|
|
488
|
+
console.log(keyv.namespace); // 'my-namespace'
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
here is an example of setting the namespace to `undefined`:
|
|
492
|
+
|
|
493
|
+
```js
|
|
494
|
+
const keyv = new Keyv();
|
|
495
|
+
console.log(keyv.namespace); // 'keyv' which is default
|
|
496
|
+
keyv.namespace = undefined;
|
|
497
|
+
console.log(keyv.namespace); // undefined
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
## .ttl
|
|
501
|
+
|
|
502
|
+
Type: `Number`<br />
|
|
503
|
+
Default: `undefined`
|
|
504
|
+
|
|
505
|
+
Default TTL. Can be overridden by specififying a TTL on `.set()`. If set to `undefined` it will never expire.
|
|
506
|
+
|
|
507
|
+
```js
|
|
508
|
+
const keyv = new Keyv({ ttl: 5000 });
|
|
509
|
+
console.log(keyv.ttl); // 5000
|
|
510
|
+
keyv.ttl = undefined;
|
|
511
|
+
console.log(keyv.ttl); // undefined (never expires)
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
## .store
|
|
515
|
+
|
|
516
|
+
Type: `Storage adapter instance`<br />
|
|
517
|
+
Default: `new Map()`
|
|
518
|
+
|
|
519
|
+
The storage adapter instance to be used by Keyv. This will wire up the iterator, events, and more when a set happens. If it is not a valid Map or Storage Adapter it will throw an error.
|
|
520
|
+
|
|
521
|
+
```js
|
|
522
|
+
import KeyvSqlite from '@keyv/sqlite';
|
|
523
|
+
const keyv = new Keyv();
|
|
524
|
+
console.log(keyv.store instanceof Map); // true
|
|
525
|
+
keyv.store = new KeyvSqlite('sqlite://path/to/database.sqlite');
|
|
526
|
+
console.log(keyv.store instanceof KeyvSqlite); // true
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
## .serialize
|
|
530
|
+
|
|
531
|
+
Type: `Function`<br />
|
|
532
|
+
Default: `JSON.stringify`
|
|
533
|
+
|
|
534
|
+
A custom serialization function used for any value.
|
|
535
|
+
|
|
536
|
+
```js
|
|
537
|
+
const keyv = new Keyv();
|
|
538
|
+
console.log(keyv.serialize); // JSON.stringify
|
|
539
|
+
keyv.serialize = value => value.toString();
|
|
540
|
+
console.log(keyv.serialize); // value => value.toString()
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
## .deserialize
|
|
544
|
+
|
|
545
|
+
Type: `Function`<br />
|
|
546
|
+
Default: `JSON.parse`
|
|
547
|
+
|
|
548
|
+
A custom deserialization function used for any value.
|
|
549
|
+
|
|
550
|
+
```js
|
|
551
|
+
const keyv = new Keyv();
|
|
552
|
+
console.log(keyv.deserialize); // JSON.parse
|
|
553
|
+
keyv.deserialize = value => parseInt(value);
|
|
554
|
+
console.log(keyv.deserialize); // value => parseInt(value)
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
## .compression
|
|
558
|
+
|
|
559
|
+
Type: `CompressionAdapter`<br />
|
|
560
|
+
Default: `undefined`
|
|
561
|
+
|
|
562
|
+
this is the compression package to use. See [Compression](#compression) for more details. If it is undefined it will not compress (default).
|
|
563
|
+
|
|
564
|
+
```js
|
|
565
|
+
import KeyvGzip from '@keyv/compress-gzip';
|
|
566
|
+
|
|
567
|
+
const keyv = new Keyv();
|
|
568
|
+
console.log(keyv.compression); // undefined
|
|
569
|
+
keyv.compression = new KeyvGzip();
|
|
570
|
+
console.log(keyv.compression); // KeyvGzip
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
## .useKeyPrefix
|
|
574
|
+
|
|
575
|
+
Type: `Boolean`<br />
|
|
576
|
+
Default: `true`
|
|
577
|
+
|
|
578
|
+
If set to `true` Keyv will prefix all keys with the namespace. This is useful if you want to avoid collisions with other data in your storage.
|
|
579
|
+
|
|
580
|
+
```js
|
|
581
|
+
const keyv = new Keyv({ useKeyPrefix: false });
|
|
582
|
+
console.log(keyv.useKeyPrefix); // false
|
|
583
|
+
keyv.useKeyPrefix = true;
|
|
584
|
+
console.log(keyv.useKeyPrefix); // true
|
|
585
|
+
```
|
|
586
|
+
|
|
465
587
|
# How to Contribute
|
|
466
588
|
|
|
467
589
|
We welcome contributions to Keyv! 🎉 Here are some guides to get you started with contributing:
|