@keyv/redis 4.2.0 → 4.3.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 CHANGED
@@ -32,6 +32,7 @@ Redis storage adapter for [Keyv](https://github.com/jaredwray/keyv).
32
32
  * [Using Cacheable with Redis](#using-cacheable-with-redis)
33
33
  * [Clustering and TLS Support](#clustering-and-tls-support)
34
34
  * [API](#api)
35
+ * [Using Custom Redis Client Events](#using-custom-redis-client-events)
35
36
  * [Migrating from v3 to v4](#migrating-from-v3-to-v4)
36
37
  * [About Redis Sets and its Support in v4](#about-redis-sets-and-its-support-in-v4)
37
38
  * [License](#license)
@@ -254,9 +255,34 @@ const keyv = new Keyv({ store: new KeyvRedis(tlsOptions) });
254
255
  * **delete** - Delete a key.
255
256
  * **deleteMany** - Delete multiple keys.
256
257
  * **clear** - Clear all keys in the namespace. If the namespace is not set it will clear all keys that are not prefixed with a namespace unless `noNamespaceAffectsAll` is set to `true`.
257
- * **disconnect** - Disconnect from the Redis server.
258
+ * **disconnect** - Disconnect from the Redis server using `Quit` command. If you set `force` to `true` it will force the disconnect.
258
259
  * **iterator** - Create a new iterator for the keys. If the namespace is not set it will iterate over all keys that are not prefixed with a namespace unless `noNamespaceAffectsAll` is set to `true`.
259
260
 
261
+ # Using Custom Redis Client Events
262
+
263
+ Keyv by default supports the `error` event across all storage adapters. If you want to listen to other events you can do so by accessing the `client` property of the `KeyvRedis` instance. Here is an example of how to do that:
264
+
265
+ ```js
266
+ import {createKeyv} from '@keyv/redis';
267
+
268
+ const keyv = createKeyv('redis://user:pass@localhost:6379');
269
+ const redisClient = keyv.store.client;
270
+
271
+ redisClient.on('connect', () => {
272
+ console.log('Redis client connected');
273
+ });
274
+
275
+ redisClient.on('reconnecting', () => {
276
+ console.log('Redis client reconnecting');
277
+ });
278
+
279
+ redisClient.on('end', () => {
280
+ console.log('Redis client disconnected');
281
+ });
282
+ ```
283
+
284
+ Here are some of the events you can listen to: https://www.npmjs.com/package/redis#events
285
+
260
286
  # Migrating from v3 to v4
261
287
 
262
288
  Overall the API is the same as v3 with additional options and performance improvements. Here are the main changes:
package/dist/index.cjs CHANGED
@@ -28,15 +28,15 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
30
  // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
33
  Keyv: () => import_keyv2.Keyv,
34
34
  createClient: () => import_redis2.createClient,
35
35
  createCluster: () => import_redis2.createCluster,
36
36
  createKeyv: () => createKeyv,
37
37
  default: () => KeyvRedis
38
38
  });
39
- module.exports = __toCommonJS(src_exports);
39
+ module.exports = __toCommonJS(index_exports);
40
40
  var import_node_events = __toESM(require("events"), 1);
41
41
  var import_redis = require("redis");
42
42
  var import_keyv = require("keyv");
@@ -199,7 +199,7 @@ var KeyvRedis = class extends import_node_events.default {
199
199
  }
200
200
  /**
201
201
  * Will set many key value pairs in the store. TTL is in milliseconds. This will be done as a single transaction.
202
- * @param {Array<KeyvRedisEntry<string>>} entries - the key value pairs to set with optional ttl
202
+ * @param {KeyvEntry[]} entries - the key value pairs to set with optional ttl
203
203
  */
204
204
  async setMany(entries) {
205
205
  const client = await this.getClient();
@@ -307,10 +307,12 @@ var KeyvRedis = class extends import_node_events.default {
307
307
  /**
308
308
  * Disconnect from the Redis server.
309
309
  * @returns {Promise<void>}
310
+ * @param {boolean} [force] - it will send a quit command if false, otherwise it will send a disconnect command to forcefully disconnect.
311
+ * @see {@link https://github.com/redis/node-redis/tree/master/packages/redis#disconnecting}
310
312
  */
311
- async disconnect() {
313
+ async disconnect(force) {
312
314
  if (this._client.isOpen) {
313
- await this._client.disconnect();
315
+ await (force ? this._client.disconnect() : this._client.quit());
314
316
  }
315
317
  }
316
318
  /**
@@ -515,6 +517,7 @@ var KeyvRedis = class extends import_node_events.default {
515
517
  }
516
518
  };
517
519
  function createKeyv(connect, options) {
520
+ connect ??= "redis://localhost:6379";
518
521
  const adapter = new KeyvRedis(connect, options);
519
522
  const keyv = new import_keyv.Keyv({ store: adapter, namespace: options?.namespace, useKeyPrefix: false });
520
523
  return keyv;
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import EventEmitter from 'node:events';
2
2
  import { RedisClientType, RedisClusterType, RedisModules, RedisFunctions, RedisScripts, RedisClientOptions, RedisClusterOptions } from 'redis';
3
3
  export { RedisClientOptions, RedisClientType, RedisClusterOptions, RedisClusterType, createClient, createCluster } from 'redis';
4
- import { KeyvStoreAdapter, Keyv } from 'keyv';
4
+ import { KeyvStoreAdapter, KeyvEntry, Keyv } from 'keyv';
5
5
  export { Keyv } from 'keyv';
6
6
 
7
7
  type KeyvRedisOptions = {
@@ -142,9 +142,9 @@ declare class KeyvRedis<T> extends EventEmitter implements KeyvStoreAdapter {
142
142
  set(key: string, value: string, ttl?: number): Promise<void>;
143
143
  /**
144
144
  * Will set many key value pairs in the store. TTL is in milliseconds. This will be done as a single transaction.
145
- * @param {Array<KeyvRedisEntry<string>>} entries - the key value pairs to set with optional ttl
145
+ * @param {KeyvEntry[]} entries - the key value pairs to set with optional ttl
146
146
  */
147
- setMany(entries: Array<KeyvRedisEntry<string>>): Promise<void>;
147
+ setMany(entries: KeyvEntry[]): Promise<void>;
148
148
  /**
149
149
  * Check if a key exists in the store.
150
150
  * @param {string} key - the key to check
@@ -184,8 +184,10 @@ declare class KeyvRedis<T> extends EventEmitter implements KeyvStoreAdapter {
184
184
  /**
185
185
  * Disconnect from the Redis server.
186
186
  * @returns {Promise<void>}
187
+ * @param {boolean} [force] - it will send a quit command if false, otherwise it will send a disconnect command to forcefully disconnect.
188
+ * @see {@link https://github.com/redis/node-redis/tree/master/packages/redis#disconnecting}
187
189
  */
188
- disconnect(): Promise<void>;
190
+ disconnect(force?: boolean): Promise<void>;
189
191
  /**
190
192
  * Helper function to create a key with a namespace.
191
193
  * @param {string} key - the key to prefix
@@ -252,8 +254,8 @@ declare class KeyvRedis<T> extends EventEmitter implements KeyvStoreAdapter {
252
254
  }
253
255
  /**
254
256
  * Will create a Keyv instance with the Redis adapter. This will also set the namespace and useKeyPrefix to false.
255
- * @param connect
256
- * @param options
257
+ * @param connect - How to connect to the Redis server. If string pass in the url, if object pass in the options, if RedisClient pass in the client. If nothing is passed in, it will default to 'redis://localhost:6379'.
258
+ * @param {KeyvRedisOptions} options - Options for the adapter such as namespace, keyPrefixSeparator, and clearBatchSize.
257
259
  * @returns {Keyv} - Keyv instance with the Redis adapter
258
260
  */
259
261
  declare function createKeyv(connect?: string | RedisClientOptions | RedisClientType, options?: KeyvRedisOptions): Keyv;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import EventEmitter from 'node:events';
2
2
  import { RedisClientType, RedisClusterType, RedisModules, RedisFunctions, RedisScripts, RedisClientOptions, RedisClusterOptions } from 'redis';
3
3
  export { RedisClientOptions, RedisClientType, RedisClusterOptions, RedisClusterType, createClient, createCluster } from 'redis';
4
- import { KeyvStoreAdapter, Keyv } from 'keyv';
4
+ import { KeyvStoreAdapter, KeyvEntry, Keyv } from 'keyv';
5
5
  export { Keyv } from 'keyv';
6
6
 
7
7
  type KeyvRedisOptions = {
@@ -142,9 +142,9 @@ declare class KeyvRedis<T> extends EventEmitter implements KeyvStoreAdapter {
142
142
  set(key: string, value: string, ttl?: number): Promise<void>;
143
143
  /**
144
144
  * Will set many key value pairs in the store. TTL is in milliseconds. This will be done as a single transaction.
145
- * @param {Array<KeyvRedisEntry<string>>} entries - the key value pairs to set with optional ttl
145
+ * @param {KeyvEntry[]} entries - the key value pairs to set with optional ttl
146
146
  */
147
- setMany(entries: Array<KeyvRedisEntry<string>>): Promise<void>;
147
+ setMany(entries: KeyvEntry[]): Promise<void>;
148
148
  /**
149
149
  * Check if a key exists in the store.
150
150
  * @param {string} key - the key to check
@@ -184,8 +184,10 @@ declare class KeyvRedis<T> extends EventEmitter implements KeyvStoreAdapter {
184
184
  /**
185
185
  * Disconnect from the Redis server.
186
186
  * @returns {Promise<void>}
187
+ * @param {boolean} [force] - it will send a quit command if false, otherwise it will send a disconnect command to forcefully disconnect.
188
+ * @see {@link https://github.com/redis/node-redis/tree/master/packages/redis#disconnecting}
187
189
  */
188
- disconnect(): Promise<void>;
190
+ disconnect(force?: boolean): Promise<void>;
189
191
  /**
190
192
  * Helper function to create a key with a namespace.
191
193
  * @param {string} key - the key to prefix
@@ -252,8 +254,8 @@ declare class KeyvRedis<T> extends EventEmitter implements KeyvStoreAdapter {
252
254
  }
253
255
  /**
254
256
  * Will create a Keyv instance with the Redis adapter. This will also set the namespace and useKeyPrefix to false.
255
- * @param connect
256
- * @param options
257
+ * @param connect - How to connect to the Redis server. If string pass in the url, if object pass in the options, if RedisClient pass in the client. If nothing is passed in, it will default to 'redis://localhost:6379'.
258
+ * @param {KeyvRedisOptions} options - Options for the adapter such as namespace, keyPrefixSeparator, and clearBatchSize.
257
259
  * @returns {Keyv} - Keyv instance with the Redis adapter
258
260
  */
259
261
  declare function createKeyv(connect?: string | RedisClientOptions | RedisClientType, options?: KeyvRedisOptions): Keyv;
package/dist/index.js CHANGED
@@ -169,7 +169,7 @@ var KeyvRedis = class extends EventEmitter {
169
169
  }
170
170
  /**
171
171
  * Will set many key value pairs in the store. TTL is in milliseconds. This will be done as a single transaction.
172
- * @param {Array<KeyvRedisEntry<string>>} entries - the key value pairs to set with optional ttl
172
+ * @param {KeyvEntry[]} entries - the key value pairs to set with optional ttl
173
173
  */
174
174
  async setMany(entries) {
175
175
  const client = await this.getClient();
@@ -277,10 +277,12 @@ var KeyvRedis = class extends EventEmitter {
277
277
  /**
278
278
  * Disconnect from the Redis server.
279
279
  * @returns {Promise<void>}
280
+ * @param {boolean} [force] - it will send a quit command if false, otherwise it will send a disconnect command to forcefully disconnect.
281
+ * @see {@link https://github.com/redis/node-redis/tree/master/packages/redis#disconnecting}
280
282
  */
281
- async disconnect() {
283
+ async disconnect(force) {
282
284
  if (this._client.isOpen) {
283
- await this._client.disconnect();
285
+ await (force ? this._client.disconnect() : this._client.quit());
284
286
  }
285
287
  }
286
288
  /**
@@ -485,6 +487,7 @@ var KeyvRedis = class extends EventEmitter {
485
487
  }
486
488
  };
487
489
  function createKeyv(connect, options) {
490
+ connect ??= "redis://localhost:6379";
488
491
  const adapter = new KeyvRedis(connect, options);
489
492
  const keyv = new Keyv({ store: adapter, namespace: options?.namespace, useKeyPrefix: false });
490
493
  return keyv;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keyv/redis",
3
- "version": "4.2.0",
3
+ "version": "4.3.1",
4
4
  "description": "Redis storage adapter for Keyv",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -36,16 +36,16 @@
36
36
  "dependencies": {
37
37
  "cluster-key-slot": "^1.1.2",
38
38
  "redis": "^4.7.0",
39
- "keyv": "^5.2.2"
39
+ "keyv": "^5.3.1"
40
40
  },
41
41
  "devDependencies": {
42
- "@vitest/coverage-v8": "^2.1.8",
42
+ "@vitest/coverage-v8": "^3.0.7",
43
43
  "rimraf": "^6.0.1",
44
44
  "timekeeper": "^2.3.1",
45
45
  "tsd": "^0.31.2",
46
- "vitest": "^2.1.8",
46
+ "vitest": "^3.0.7",
47
47
  "xo": "^0.60.0",
48
- "@keyv/test-suite": "^2.0.3"
48
+ "@keyv/test-suite": "^2.0.5"
49
49
  },
50
50
  "tsd": {
51
51
  "directory": "test"