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