keyv 5.4.0 → 5.5.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 CHANGED
@@ -49,6 +49,8 @@ There are a few existing modules similar to Keyv, however Keyv is different beca
49
49
  - [.setMany(entries)](#setmanyentries)
50
50
  - [.get(key, [options])](#getkey-options)
51
51
  - [.getMany(keys, [options])](#getmanykeys-options)
52
+ - [.getRaw(key)](#getrawkey)
53
+ - [.getManyRaw(keys)](#getmanyrawkeys)
52
54
  - [.delete(key)](#deletekey)
53
55
  - [.deleteMany(keys)](#deletemanykeys)
54
56
  - [.clear()](#clear)
@@ -469,7 +471,7 @@ Returns a promise which resolves to the retrieved value.
469
471
 
470
472
  Returns a promise which resolves to an array of retrieved values.
471
473
 
472
- ### options.raw
474
+ ### options.raw - (Will be deprecated in v6)
473
475
 
474
476
  Type: `Boolean`<br />
475
477
  Default: `false`
@@ -478,6 +480,16 @@ If set to true the raw DB object Keyv stores internally will be returned instead
478
480
 
479
481
  This contains the TTL timestamp.
480
482
 
483
+ NOTE: This option will be deprecated in v6 and replaced with `.getRaw()` and `.getManyRaw()` methods.
484
+
485
+ ## .getRaw(key)
486
+
487
+ Returns a promise which resolves to the raw stored data for the key or `undefined` if the key does not exist or is expired.
488
+
489
+ ## .getManyRaw(keys)
490
+
491
+ Returns a promise which resolves to an array of raw stored data for the keys or `undefined` if the key does not exist or is expired.
492
+
481
493
  ## .delete(key)
482
494
 
483
495
  Deletes an entry.
package/dist/index.cjs CHANGED
@@ -185,6 +185,15 @@ var StatsManager = class extends event_manager_default {
185
185
  this.deletes++;
186
186
  }
187
187
  }
188
+ hitsOrMisses(array) {
189
+ for (const item of array) {
190
+ if (item === void 0) {
191
+ this.miss();
192
+ } else {
193
+ this.hit();
194
+ }
195
+ }
196
+ }
188
197
  reset() {
189
198
  this.hits = 0;
190
199
  this.misses = 0;
@@ -203,6 +212,10 @@ var KeyvHooks = /* @__PURE__ */ ((KeyvHooks2) => {
203
212
  KeyvHooks2["POST_GET"] = "postGet";
204
213
  KeyvHooks2["PRE_GET_MANY"] = "preGetMany";
205
214
  KeyvHooks2["POST_GET_MANY"] = "postGetMany";
215
+ KeyvHooks2["PRE_GET_RAW"] = "preGetRaw";
216
+ KeyvHooks2["POST_GET_RAW"] = "postGetRaw";
217
+ KeyvHooks2["PRE_GET_MANY_RAW"] = "preGetManyRaw";
218
+ KeyvHooks2["POST_GET_MANY_RAW"] = "postGetManyRaw";
206
219
  KeyvHooks2["PRE_DELETE"] = "preDelete";
207
220
  KeyvHooks2["POST_DELETE"] = "postDelete";
208
221
  return KeyvHooks2;
@@ -572,6 +585,80 @@ var Keyv = class extends event_manager_default {
572
585
  }
573
586
  return result;
574
587
  }
588
+ /**
589
+ * Get the raw value of a key. This is the replacement for setting raw to true in the get() method.
590
+ * @param {string} key the key to get
591
+ * @returns {Promise<StoredDataRaw<Value> | undefined>} will return a StoredDataRaw<Value> or undefined if the key does not exist or is expired.
592
+ */
593
+ async getRaw(key) {
594
+ const { store } = this.opts;
595
+ const keyPrefixed = this._getKeyPrefix(key);
596
+ this.hooks.trigger("preGetRaw" /* PRE_GET_RAW */, { key: keyPrefixed });
597
+ const rawData = await store.get(keyPrefixed);
598
+ if (rawData === void 0 || rawData === null) {
599
+ this.stats.miss();
600
+ return void 0;
601
+ }
602
+ const deserializedData = typeof rawData === "string" || this.opts.compression ? await this.deserializeData(rawData) : rawData;
603
+ if (deserializedData !== void 0 && deserializedData.expires !== void 0 && deserializedData.expires !== null && deserializedData.expires < Date.now()) {
604
+ this.stats.miss();
605
+ await this.delete(key);
606
+ return void 0;
607
+ }
608
+ this.stats.hit();
609
+ this.hooks.trigger("postGetRaw" /* POST_GET_RAW */, { key: keyPrefixed, value: deserializedData });
610
+ return deserializedData;
611
+ }
612
+ /**
613
+ * Get the raw values of many keys. This is the replacement for setting raw to true in the getMany() method.
614
+ * @param {string[]} keys the keys to get
615
+ * @returns {Promise<Array<StoredDataRaw<Value>>>} will return an array of StoredDataRaw<Value> or undefined if the key does not exist or is expired.
616
+ */
617
+ async getManyRaw(keys) {
618
+ const { store } = this.opts;
619
+ const keyPrefixed = this._getKeyPrefixArray(keys);
620
+ if (keys.length === 0) {
621
+ const result2 = Array.from({ length: keys.length }).fill(void 0);
622
+ this.stats.misses += keys.length;
623
+ this.hooks.trigger("postGetManyRaw" /* POST_GET_MANY_RAW */, { keys: keyPrefixed, values: result2 });
624
+ return result2;
625
+ }
626
+ let result = [];
627
+ if (store.getMany === void 0) {
628
+ const promises = keyPrefixed.map(async (key) => {
629
+ const rawData = await store.get(key);
630
+ if (rawData !== void 0 && rawData !== null) {
631
+ return this.deserializeData(rawData);
632
+ }
633
+ return void 0;
634
+ });
635
+ const deserializedRows = await Promise.allSettled(promises);
636
+ result = deserializedRows.map((row) => row.value);
637
+ } else {
638
+ const rawData = await store.getMany(keyPrefixed);
639
+ for (const row of rawData) {
640
+ if (row !== void 0 && row !== null) {
641
+ result.push(await this.deserializeData(row));
642
+ } else {
643
+ result.push(void 0);
644
+ }
645
+ }
646
+ }
647
+ const expiredKeys = [];
648
+ const isDataExpired = (data) => typeof data.expires === "number" && Date.now() > data.expires;
649
+ for (const [index, row] of result.entries()) {
650
+ if (row !== void 0 && isDataExpired(row)) {
651
+ expiredKeys.push(keyPrefixed[index]);
652
+ result[index] = void 0;
653
+ }
654
+ }
655
+ if (expiredKeys.length > 0) {
656
+ await this.deleteMany(expiredKeys);
657
+ }
658
+ this.stats.hitsOrMisses(result);
659
+ this.hooks.trigger("postGetManyRaw" /* POST_GET_MANY_RAW */, { keys: keyPrefixed, values: result });
660
+ return result;
661
+ }
575
662
  /**
576
663
  * Set an item to the store
577
664
  * @param {string | Array<KeyvEntry>} key the key to use. If you pass in an array of KeyvEntry it will set many items
package/dist/index.d.cts CHANGED
@@ -37,6 +37,7 @@ declare class StatsManager extends EventManager {
37
37
  miss(): void;
38
38
  set(): void;
39
39
  delete(): void;
40
+ hitsOrMisses<T>(array: Array<T | undefined>): void;
40
41
  reset(): void;
41
42
  }
42
43
 
@@ -59,6 +60,10 @@ declare enum KeyvHooks {
59
60
  POST_GET = "postGet",
60
61
  PRE_GET_MANY = "preGetMany",
61
62
  POST_GET_MANY = "postGetMany",
63
+ PRE_GET_RAW = "preGetRaw",
64
+ POST_GET_RAW = "postGetRaw",
65
+ PRE_GET_MANY_RAW = "preGetManyRaw",
66
+ POST_GET_MANY_RAW = "postGetManyRaw",
62
67
  PRE_DELETE = "preDelete",
63
68
  POST_DELETE = "postDelete"
64
69
  }
@@ -304,6 +309,18 @@ declare class Keyv<GenericValue = any> extends EventManager {
304
309
  getMany<Value = GenericValue>(keys: string[], options?: {
305
310
  raw: true;
306
311
  }): Promise<Array<StoredDataRaw<Value>>>;
312
+ /**
313
+ * Get the raw value of a key. This is the replacement for setting raw to true in the get() method.
314
+ * @param {string} key the key to get
315
+ * @returns {Promise<StoredDataRaw<Value> | undefined>} will return a StoredDataRaw<Value> or undefined if the key does not exist or is expired.
316
+ */
317
+ getRaw<Value = GenericValue>(key: string): Promise<StoredDataRaw<Value> | undefined>;
318
+ /**
319
+ * Get the raw values of many keys. This is the replacement for setting raw to true in the getMany() method.
320
+ * @param {string[]} keys the keys to get
321
+ * @returns {Promise<Array<StoredDataRaw<Value>>>} will return an array of StoredDataRaw<Value> or undefined if the key does not exist or is expired.
322
+ */
323
+ getManyRaw<Value = GenericValue>(keys: string[]): Promise<Array<StoredDataRaw<Value>>>;
307
324
  /**
308
325
  * Set an item to the store
309
326
  * @param {string | Array<KeyvEntry>} key the key to use. If you pass in an array of KeyvEntry it will set many items
package/dist/index.d.ts CHANGED
@@ -37,6 +37,7 @@ declare class StatsManager extends EventManager {
37
37
  miss(): void;
38
38
  set(): void;
39
39
  delete(): void;
40
+ hitsOrMisses<T>(array: Array<T | undefined>): void;
40
41
  reset(): void;
41
42
  }
42
43
 
@@ -59,6 +60,10 @@ declare enum KeyvHooks {
59
60
  POST_GET = "postGet",
60
61
  PRE_GET_MANY = "preGetMany",
61
62
  POST_GET_MANY = "postGetMany",
63
+ PRE_GET_RAW = "preGetRaw",
64
+ POST_GET_RAW = "postGetRaw",
65
+ PRE_GET_MANY_RAW = "preGetManyRaw",
66
+ POST_GET_MANY_RAW = "postGetManyRaw",
62
67
  PRE_DELETE = "preDelete",
63
68
  POST_DELETE = "postDelete"
64
69
  }
@@ -304,6 +309,18 @@ declare class Keyv<GenericValue = any> extends EventManager {
304
309
  getMany<Value = GenericValue>(keys: string[], options?: {
305
310
  raw: true;
306
311
  }): Promise<Array<StoredDataRaw<Value>>>;
312
+ /**
313
+ * Get the raw value of a key. This is the replacement for setting raw to true in the get() method.
314
+ * @param {string} key the key to get
315
+ * @returns {Promise<StoredDataRaw<Value> | undefined>} will return a StoredDataRaw<Value> or undefined if the key does not exist or is expired.
316
+ */
317
+ getRaw<Value = GenericValue>(key: string): Promise<StoredDataRaw<Value> | undefined>;
318
+ /**
319
+ * Get the raw values of many keys. This is the replacement for setting raw to true in the getMany() method.
320
+ * @param {string[]} keys the keys to get
321
+ * @returns {Promise<Array<StoredDataRaw<Value>>>} will return an array of StoredDataRaw<Value> or undefined if the key does not exist or is expired.
322
+ */
323
+ getManyRaw<Value = GenericValue>(keys: string[]): Promise<Array<StoredDataRaw<Value>>>;
307
324
  /**
308
325
  * Set an item to the store
309
326
  * @param {string | Array<KeyvEntry>} key the key to use. If you pass in an array of KeyvEntry it will set many items
package/dist/index.js CHANGED
@@ -159,6 +159,15 @@ var StatsManager = class extends event_manager_default {
159
159
  this.deletes++;
160
160
  }
161
161
  }
162
+ hitsOrMisses(array) {
163
+ for (const item of array) {
164
+ if (item === void 0) {
165
+ this.miss();
166
+ } else {
167
+ this.hit();
168
+ }
169
+ }
170
+ }
162
171
  reset() {
163
172
  this.hits = 0;
164
173
  this.misses = 0;
@@ -177,6 +186,10 @@ var KeyvHooks = /* @__PURE__ */ ((KeyvHooks2) => {
177
186
  KeyvHooks2["POST_GET"] = "postGet";
178
187
  KeyvHooks2["PRE_GET_MANY"] = "preGetMany";
179
188
  KeyvHooks2["POST_GET_MANY"] = "postGetMany";
189
+ KeyvHooks2["PRE_GET_RAW"] = "preGetRaw";
190
+ KeyvHooks2["POST_GET_RAW"] = "postGetRaw";
191
+ KeyvHooks2["PRE_GET_MANY_RAW"] = "preGetManyRaw";
192
+ KeyvHooks2["POST_GET_MANY_RAW"] = "postGetManyRaw";
180
193
  KeyvHooks2["PRE_DELETE"] = "preDelete";
181
194
  KeyvHooks2["POST_DELETE"] = "postDelete";
182
195
  return KeyvHooks2;
@@ -546,6 +559,80 @@ var Keyv = class extends event_manager_default {
546
559
  }
547
560
  return result;
548
561
  }
562
+ /**
563
+ * Get the raw value of a key. This is the replacement for setting raw to true in the get() method.
564
+ * @param {string} key the key to get
565
+ * @returns {Promise<StoredDataRaw<Value> | undefined>} will return a StoredDataRaw<Value> or undefined if the key does not exist or is expired.
566
+ */
567
+ async getRaw(key) {
568
+ const { store } = this.opts;
569
+ const keyPrefixed = this._getKeyPrefix(key);
570
+ this.hooks.trigger("preGetRaw" /* PRE_GET_RAW */, { key: keyPrefixed });
571
+ const rawData = await store.get(keyPrefixed);
572
+ if (rawData === void 0 || rawData === null) {
573
+ this.stats.miss();
574
+ return void 0;
575
+ }
576
+ const deserializedData = typeof rawData === "string" || this.opts.compression ? await this.deserializeData(rawData) : rawData;
577
+ if (deserializedData !== void 0 && deserializedData.expires !== void 0 && deserializedData.expires !== null && deserializedData.expires < Date.now()) {
578
+ this.stats.miss();
579
+ await this.delete(key);
580
+ return void 0;
581
+ }
582
+ this.stats.hit();
583
+ this.hooks.trigger("postGetRaw" /* POST_GET_RAW */, { key: keyPrefixed, value: deserializedData });
584
+ return deserializedData;
585
+ }
586
+ /**
587
+ * Get the raw values of many keys. This is the replacement for setting raw to true in the getMany() method.
588
+ * @param {string[]} keys the keys to get
589
+ * @returns {Promise<Array<StoredDataRaw<Value>>>} will return an array of StoredDataRaw<Value> or undefined if the key does not exist or is expired.
590
+ */
591
+ async getManyRaw(keys) {
592
+ const { store } = this.opts;
593
+ const keyPrefixed = this._getKeyPrefixArray(keys);
594
+ if (keys.length === 0) {
595
+ const result2 = Array.from({ length: keys.length }).fill(void 0);
596
+ this.stats.misses += keys.length;
597
+ this.hooks.trigger("postGetManyRaw" /* POST_GET_MANY_RAW */, { keys: keyPrefixed, values: result2 });
598
+ return result2;
599
+ }
600
+ let result = [];
601
+ if (store.getMany === void 0) {
602
+ const promises = keyPrefixed.map(async (key) => {
603
+ const rawData = await store.get(key);
604
+ if (rawData !== void 0 && rawData !== null) {
605
+ return this.deserializeData(rawData);
606
+ }
607
+ return void 0;
608
+ });
609
+ const deserializedRows = await Promise.allSettled(promises);
610
+ result = deserializedRows.map((row) => row.value);
611
+ } else {
612
+ const rawData = await store.getMany(keyPrefixed);
613
+ for (const row of rawData) {
614
+ if (row !== void 0 && row !== null) {
615
+ result.push(await this.deserializeData(row));
616
+ } else {
617
+ result.push(void 0);
618
+ }
619
+ }
620
+ }
621
+ const expiredKeys = [];
622
+ const isDataExpired = (data) => typeof data.expires === "number" && Date.now() > data.expires;
623
+ for (const [index, row] of result.entries()) {
624
+ if (row !== void 0 && isDataExpired(row)) {
625
+ expiredKeys.push(keyPrefixed[index]);
626
+ result[index] = void 0;
627
+ }
628
+ }
629
+ if (expiredKeys.length > 0) {
630
+ await this.deleteMany(expiredKeys);
631
+ }
632
+ this.stats.hitsOrMisses(result);
633
+ this.hooks.trigger("postGetManyRaw" /* POST_GET_MANY_RAW */, { keys: keyPrefixed, values: result });
634
+ return result;
635
+ }
549
636
  /**
550
637
  * Set an item to the store
551
638
  * @param {string | Array<KeyvEntry>} key the key to use. If you pass in an array of KeyvEntry it will set many items
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keyv",
3
- "version": "5.4.0",
3
+ "version": "5.5.0",
4
4
  "description": "Simple key-value storage with support for multiple backends",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -63,14 +63,14 @@
63
63
  "timekeeper": "^2.3.1",
64
64
  "tsd": "^0.32.0",
65
65
  "vitest": "^3.2.4",
66
- "xo": "^1.1.1",
67
- "@keyv/memcache": "^2.0.2",
66
+ "xo": "^1.2.0",
67
+ "@keyv/compress-brotli": "^2.0.5",
68
68
  "@keyv/compress-gzip": "^2.0.3",
69
+ "@keyv/memcache": "^2.0.2",
69
70
  "@keyv/compress-lz4": "^1.0.1",
70
- "@keyv/compress-brotli": "^2.0.5",
71
71
  "@keyv/mongo": "^3.0.3",
72
- "@keyv/test-suite": "^2.0.9",
73
- "@keyv/sqlite": "^4.0.5"
72
+ "@keyv/sqlite": "^4.0.5",
73
+ "@keyv/test-suite": "^2.1.0"
74
74
  },
75
75
  "tsd": {
76
76
  "directory": "test"