@keyv/redis 3.0.1 → 4.0.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/dist/index.d.cts CHANGED
@@ -1,33 +1,210 @@
1
1
  import EventEmitter from 'events';
2
- import { KeyvStoreAdapter, StoredData } from 'keyv';
3
- import { Redis, Cluster } from 'ioredis';
2
+ import { RedisClientOptions, RedisClientType } from 'redis';
3
+ export { RedisClientOptions, RedisClientType, createClient, createCluster } from 'redis';
4
+ import { KeyvStoreAdapter, Keyv } from 'keyv';
5
+ export { Keyv } from 'keyv';
4
6
 
5
7
  type KeyvRedisOptions = {
6
- [K in keyof Redis]?: Redis[K];
7
- } & {
8
- uri?: string;
9
- dialect?: string;
10
- useRedisSets?: boolean;
8
+ /**
9
+ * Namespace for the current instance.
10
+ */
11
+ namespace?: string;
12
+ /**
13
+ * Separator to use between namespace and key.
14
+ */
15
+ keyPrefixSeparator?: string;
16
+ /**
17
+ * Number of keys to delete in a single batch.
18
+ */
19
+ clearBatchSize?: number;
20
+ /**
21
+ * Enable Unlink instead of using Del for clearing keys. This is more performant but may not be supported by all Redis versions.
22
+ */
23
+ useUnlink?: boolean;
24
+ };
25
+ type KeyvRedisPropertyOptions = KeyvRedisOptions & {
26
+ /**
27
+ * Dialect used by the adapter. This is legacy so Keyv knows what is iteratable.
28
+ */
29
+ dialect: 'redis';
30
+ /**
31
+ * URL used to connect to the Redis server. This is legacy so Keyv knows what is iteratable.
32
+ */
33
+ url: string;
34
+ };
35
+ type KeyvRedisEntry<T> = {
36
+ /**
37
+ * Key to set.
38
+ */
39
+ key: string;
40
+ /**
41
+ * Value to set.
42
+ */
43
+ value: T;
44
+ /**
45
+ * Time to live in milliseconds.
46
+ */
47
+ ttl?: number;
11
48
  };
12
- type KeyvUriOptions = string | KeyvRedisOptions | Redis | Cluster;
13
-
14
49
  declare class KeyvRedis extends EventEmitter implements KeyvStoreAdapter {
15
- ttlSupport: boolean;
16
- namespace?: string;
17
- opts: Record<string, unknown>;
18
- redis: any;
19
- constructor(uri: KeyvRedisOptions | KeyvUriOptions, options?: KeyvRedisOptions);
20
- _getNamespace(): string;
21
- _getKeyName: (key: string) => string;
22
- get<Value>(key: string): Promise<StoredData<Value> | undefined>;
23
- getMany<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>;
24
- set(key: string, value: any, ttl?: number): Promise<undefined>;
50
+ private _client;
51
+ private _namespace;
52
+ private _keyPrefixSeparator;
53
+ private _clearBatchSize;
54
+ private _useUnlink;
55
+ /**
56
+ * KeyvRedis constructor.
57
+ * @param {string | RedisClientOptions | RedisClientType} [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.
58
+ * @param {KeyvRedisOptions} [options] Options for the adapter such as namespace, keyPrefixSeparator, and clearBatchSize.
59
+ */
60
+ constructor(connect?: string | RedisClientOptions | RedisClientType, options?: KeyvRedisOptions);
61
+ /**
62
+ * Get the Redis client.
63
+ */
64
+ get client(): RedisClientType;
65
+ /**
66
+ * Set the Redis client.
67
+ */
68
+ set client(value: RedisClientType);
69
+ /**
70
+ * Get the options for the adapter.
71
+ */
72
+ get opts(): KeyvRedisPropertyOptions;
73
+ /**
74
+ * Set the options for the adapter.
75
+ */
76
+ set opts(options: KeyvRedisOptions);
77
+ /**
78
+ * Get the namespace for the adapter. If undefined, it will not use a namespace including keyPrefixing.
79
+ * @default undefined
80
+ */
81
+ get namespace(): string | undefined;
82
+ /**
83
+ * Set the namespace for the adapter. If undefined, it will not use a namespace including keyPrefixing.
84
+ */
85
+ set namespace(value: string | undefined);
86
+ /**
87
+ * Get the separator between the namespace and key.
88
+ * @default '::'
89
+ */
90
+ get keyPrefixSeparator(): string;
91
+ /**
92
+ * Set the separator between the namespace and key.
93
+ */
94
+ set keyPrefixSeparator(value: string);
95
+ /**
96
+ * Get the number of keys to delete in a single batch.
97
+ * @default 1000
98
+ */
99
+ get clearBatchSize(): number;
100
+ /**
101
+ * Set the number of keys to delete in a single batch.
102
+ */
103
+ set clearBatchSize(value: number);
104
+ /**
105
+ * Get if Unlink is used instead of Del for clearing keys. This is more performant but may not be supported by all Redis versions.
106
+ * @default true
107
+ */
108
+ get useUnlink(): boolean;
109
+ /**
110
+ * Set if Unlink is used instead of Del for clearing keys. This is more performant but may not be supported by all Redis versions.
111
+ */
112
+ set useUnlink(value: boolean);
113
+ /**
114
+ * Get the Redis URL used to connect to the server. This is used to get a connected client.
115
+ */
116
+ getClient(): Promise<RedisClientType>;
117
+ /**
118
+ * Set a key value pair in the store. TTL is in milliseconds.
119
+ * @param {string} key - the key to set
120
+ * @param {string} value - the value to set
121
+ * @param {number} [ttl] - the time to live in milliseconds
122
+ */
123
+ set(key: string, value: string, ttl?: number): Promise<void>;
124
+ /**
125
+ * Will set many key value pairs in the store. TTL is in milliseconds. This will be done as a single transaction.
126
+ * @param {Array<KeyvRedisEntry<string>>} entries - the key value pairs to set with optional ttl
127
+ */
128
+ setMany(entries: Array<KeyvRedisEntry<string>>): Promise<void>;
129
+ /**
130
+ * Check if a key exists in the store.
131
+ * @param {string} key - the key to check
132
+ * @returns {Promise<boolean>} - true if the key exists, false if not
133
+ */
134
+ has(key: string): Promise<boolean>;
135
+ /**
136
+ * Check if many keys exist in the store. This will be done as a single transaction.
137
+ * @param {Array<string>} keys - the keys to check
138
+ * @returns {Promise<Array<boolean>>} - array of booleans for each key if it exists
139
+ */
140
+ hasMany(keys: string[]): Promise<boolean[]>;
141
+ /**
142
+ * Get a value from the store. If the key does not exist, it will return undefined.
143
+ * @param {string} key - the key to get
144
+ * @returns {Promise<string | undefined>} - the value or undefined if the key does not exist
145
+ */
146
+ get<T>(key: string): Promise<T | undefined>;
147
+ /**
148
+ * Get many values from the store. If a key does not exist, it will return undefined.
149
+ * @param {Array<string>} keys - the keys to get
150
+ * @returns {Promise<Array<string | undefined>>} - array of values or undefined if the key does not exist
151
+ */
152
+ getMany<T>(keys: string[]): Promise<Array<T | undefined>>;
153
+ /**
154
+ * Delete a key from the store.
155
+ * @param {string} key - the key to delete
156
+ * @returns {Promise<boolean>} - true if the key was deleted, false if not
157
+ */
25
158
  delete(key: string): Promise<boolean>;
159
+ /**
160
+ * Delete many keys from the store. This will be done as a single transaction.
161
+ * @param {Array<string>} keys - the keys to delete
162
+ * @returns {Promise<boolean>} - true if any key was deleted, false if not
163
+ */
26
164
  deleteMany(keys: string[]): Promise<boolean>;
165
+ /**
166
+ * Disconnect from the Redis server.
167
+ * @returns {Promise<void>}
168
+ */
169
+ disconnect(): Promise<void>;
170
+ /**
171
+ * Helper function to create a key with a namespace.
172
+ * @param {string} key - the key to prefix
173
+ * @param {string} namespace - the namespace to prefix the key with
174
+ * @returns {string} - the key with the namespace such as 'namespace::key'
175
+ */
176
+ createKeyPrefix(key: string, namespace?: string): string;
177
+ /**
178
+ * Helper function to get a key without the namespace.
179
+ * @param {string} key - the key to remove the namespace from
180
+ * @param {string} namespace - the namespace to remove from the key
181
+ * @returns {string} - the key without the namespace such as 'key'
182
+ */
183
+ getKeyWithoutPrefix(key: string, namespace?: string): string;
184
+ /**
185
+ * Get an async iterator for the keys and values in the store. If a namespace is provided, it will only iterate over keys with that namespace.
186
+ * @param {string} [namespace] - the namespace to iterate over
187
+ * @returns {AsyncGenerator<[string, T | undefined], void, unknown>} - async iterator with key value pairs
188
+ */
189
+ iterator<Value>(namespace?: string): AsyncGenerator<[string, Value | undefined], void, unknown>;
190
+ /**
191
+ * Clear all keys in the store.
192
+ * IMPORTANT: this can cause performance issues if there are a large number of keys in the store. Use with caution as not recommended for production.
193
+ * If a namespace is not set it will clear all keys with no prefix.
194
+ * If a namespace is set it will clear all keys with that namespace.
195
+ * @returns {Promise<void>}
196
+ */
27
197
  clear(): Promise<void>;
28
- iterator(namespace?: string): AsyncGenerator<any[], void, unknown>;
29
- has(key: string): Promise<boolean>;
30
- disconnect(): Promise<any>;
198
+ private clearNamespace;
199
+ private setOptions;
200
+ private initClient;
31
201
  }
202
+ /**
203
+ * Will create a Keyv instance with the Redis adapter. This will also set the namespace and useKeyPrefix to false.
204
+ * @param connect
205
+ * @param options
206
+ * @returns {Keyv} - Keyv instance with the Redis adapter
207
+ */
208
+ declare function createKeyv(connect?: string | RedisClientOptions | RedisClientType, options?: KeyvRedisOptions): Keyv;
32
209
 
33
- export { KeyvRedis as default };
210
+ export { type KeyvRedisEntry, type KeyvRedisOptions, type KeyvRedisPropertyOptions, createKeyv, KeyvRedis as default };
package/dist/index.d.ts CHANGED
@@ -1,33 +1,210 @@
1
1
  import EventEmitter from 'events';
2
- import { KeyvStoreAdapter, StoredData } from 'keyv';
3
- import { Redis, Cluster } from 'ioredis';
2
+ import { RedisClientOptions, RedisClientType } from 'redis';
3
+ export { RedisClientOptions, RedisClientType, createClient, createCluster } from 'redis';
4
+ import { KeyvStoreAdapter, Keyv } from 'keyv';
5
+ export { Keyv } from 'keyv';
4
6
 
5
7
  type KeyvRedisOptions = {
6
- [K in keyof Redis]?: Redis[K];
7
- } & {
8
- uri?: string;
9
- dialect?: string;
10
- useRedisSets?: boolean;
8
+ /**
9
+ * Namespace for the current instance.
10
+ */
11
+ namespace?: string;
12
+ /**
13
+ * Separator to use between namespace and key.
14
+ */
15
+ keyPrefixSeparator?: string;
16
+ /**
17
+ * Number of keys to delete in a single batch.
18
+ */
19
+ clearBatchSize?: number;
20
+ /**
21
+ * Enable Unlink instead of using Del for clearing keys. This is more performant but may not be supported by all Redis versions.
22
+ */
23
+ useUnlink?: boolean;
24
+ };
25
+ type KeyvRedisPropertyOptions = KeyvRedisOptions & {
26
+ /**
27
+ * Dialect used by the adapter. This is legacy so Keyv knows what is iteratable.
28
+ */
29
+ dialect: 'redis';
30
+ /**
31
+ * URL used to connect to the Redis server. This is legacy so Keyv knows what is iteratable.
32
+ */
33
+ url: string;
34
+ };
35
+ type KeyvRedisEntry<T> = {
36
+ /**
37
+ * Key to set.
38
+ */
39
+ key: string;
40
+ /**
41
+ * Value to set.
42
+ */
43
+ value: T;
44
+ /**
45
+ * Time to live in milliseconds.
46
+ */
47
+ ttl?: number;
11
48
  };
12
- type KeyvUriOptions = string | KeyvRedisOptions | Redis | Cluster;
13
-
14
49
  declare class KeyvRedis extends EventEmitter implements KeyvStoreAdapter {
15
- ttlSupport: boolean;
16
- namespace?: string;
17
- opts: Record<string, unknown>;
18
- redis: any;
19
- constructor(uri: KeyvRedisOptions | KeyvUriOptions, options?: KeyvRedisOptions);
20
- _getNamespace(): string;
21
- _getKeyName: (key: string) => string;
22
- get<Value>(key: string): Promise<StoredData<Value> | undefined>;
23
- getMany<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>;
24
- set(key: string, value: any, ttl?: number): Promise<undefined>;
50
+ private _client;
51
+ private _namespace;
52
+ private _keyPrefixSeparator;
53
+ private _clearBatchSize;
54
+ private _useUnlink;
55
+ /**
56
+ * KeyvRedis constructor.
57
+ * @param {string | RedisClientOptions | RedisClientType} [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.
58
+ * @param {KeyvRedisOptions} [options] Options for the adapter such as namespace, keyPrefixSeparator, and clearBatchSize.
59
+ */
60
+ constructor(connect?: string | RedisClientOptions | RedisClientType, options?: KeyvRedisOptions);
61
+ /**
62
+ * Get the Redis client.
63
+ */
64
+ get client(): RedisClientType;
65
+ /**
66
+ * Set the Redis client.
67
+ */
68
+ set client(value: RedisClientType);
69
+ /**
70
+ * Get the options for the adapter.
71
+ */
72
+ get opts(): KeyvRedisPropertyOptions;
73
+ /**
74
+ * Set the options for the adapter.
75
+ */
76
+ set opts(options: KeyvRedisOptions);
77
+ /**
78
+ * Get the namespace for the adapter. If undefined, it will not use a namespace including keyPrefixing.
79
+ * @default undefined
80
+ */
81
+ get namespace(): string | undefined;
82
+ /**
83
+ * Set the namespace for the adapter. If undefined, it will not use a namespace including keyPrefixing.
84
+ */
85
+ set namespace(value: string | undefined);
86
+ /**
87
+ * Get the separator between the namespace and key.
88
+ * @default '::'
89
+ */
90
+ get keyPrefixSeparator(): string;
91
+ /**
92
+ * Set the separator between the namespace and key.
93
+ */
94
+ set keyPrefixSeparator(value: string);
95
+ /**
96
+ * Get the number of keys to delete in a single batch.
97
+ * @default 1000
98
+ */
99
+ get clearBatchSize(): number;
100
+ /**
101
+ * Set the number of keys to delete in a single batch.
102
+ */
103
+ set clearBatchSize(value: number);
104
+ /**
105
+ * Get if Unlink is used instead of Del for clearing keys. This is more performant but may not be supported by all Redis versions.
106
+ * @default true
107
+ */
108
+ get useUnlink(): boolean;
109
+ /**
110
+ * Set if Unlink is used instead of Del for clearing keys. This is more performant but may not be supported by all Redis versions.
111
+ */
112
+ set useUnlink(value: boolean);
113
+ /**
114
+ * Get the Redis URL used to connect to the server. This is used to get a connected client.
115
+ */
116
+ getClient(): Promise<RedisClientType>;
117
+ /**
118
+ * Set a key value pair in the store. TTL is in milliseconds.
119
+ * @param {string} key - the key to set
120
+ * @param {string} value - the value to set
121
+ * @param {number} [ttl] - the time to live in milliseconds
122
+ */
123
+ set(key: string, value: string, ttl?: number): Promise<void>;
124
+ /**
125
+ * Will set many key value pairs in the store. TTL is in milliseconds. This will be done as a single transaction.
126
+ * @param {Array<KeyvRedisEntry<string>>} entries - the key value pairs to set with optional ttl
127
+ */
128
+ setMany(entries: Array<KeyvRedisEntry<string>>): Promise<void>;
129
+ /**
130
+ * Check if a key exists in the store.
131
+ * @param {string} key - the key to check
132
+ * @returns {Promise<boolean>} - true if the key exists, false if not
133
+ */
134
+ has(key: string): Promise<boolean>;
135
+ /**
136
+ * Check if many keys exist in the store. This will be done as a single transaction.
137
+ * @param {Array<string>} keys - the keys to check
138
+ * @returns {Promise<Array<boolean>>} - array of booleans for each key if it exists
139
+ */
140
+ hasMany(keys: string[]): Promise<boolean[]>;
141
+ /**
142
+ * Get a value from the store. If the key does not exist, it will return undefined.
143
+ * @param {string} key - the key to get
144
+ * @returns {Promise<string | undefined>} - the value or undefined if the key does not exist
145
+ */
146
+ get<T>(key: string): Promise<T | undefined>;
147
+ /**
148
+ * Get many values from the store. If a key does not exist, it will return undefined.
149
+ * @param {Array<string>} keys - the keys to get
150
+ * @returns {Promise<Array<string | undefined>>} - array of values or undefined if the key does not exist
151
+ */
152
+ getMany<T>(keys: string[]): Promise<Array<T | undefined>>;
153
+ /**
154
+ * Delete a key from the store.
155
+ * @param {string} key - the key to delete
156
+ * @returns {Promise<boolean>} - true if the key was deleted, false if not
157
+ */
25
158
  delete(key: string): Promise<boolean>;
159
+ /**
160
+ * Delete many keys from the store. This will be done as a single transaction.
161
+ * @param {Array<string>} keys - the keys to delete
162
+ * @returns {Promise<boolean>} - true if any key was deleted, false if not
163
+ */
26
164
  deleteMany(keys: string[]): Promise<boolean>;
165
+ /**
166
+ * Disconnect from the Redis server.
167
+ * @returns {Promise<void>}
168
+ */
169
+ disconnect(): Promise<void>;
170
+ /**
171
+ * Helper function to create a key with a namespace.
172
+ * @param {string} key - the key to prefix
173
+ * @param {string} namespace - the namespace to prefix the key with
174
+ * @returns {string} - the key with the namespace such as 'namespace::key'
175
+ */
176
+ createKeyPrefix(key: string, namespace?: string): string;
177
+ /**
178
+ * Helper function to get a key without the namespace.
179
+ * @param {string} key - the key to remove the namespace from
180
+ * @param {string} namespace - the namespace to remove from the key
181
+ * @returns {string} - the key without the namespace such as 'key'
182
+ */
183
+ getKeyWithoutPrefix(key: string, namespace?: string): string;
184
+ /**
185
+ * Get an async iterator for the keys and values in the store. If a namespace is provided, it will only iterate over keys with that namespace.
186
+ * @param {string} [namespace] - the namespace to iterate over
187
+ * @returns {AsyncGenerator<[string, T | undefined], void, unknown>} - async iterator with key value pairs
188
+ */
189
+ iterator<Value>(namespace?: string): AsyncGenerator<[string, Value | undefined], void, unknown>;
190
+ /**
191
+ * Clear all keys in the store.
192
+ * IMPORTANT: this can cause performance issues if there are a large number of keys in the store. Use with caution as not recommended for production.
193
+ * If a namespace is not set it will clear all keys with no prefix.
194
+ * If a namespace is set it will clear all keys with that namespace.
195
+ * @returns {Promise<void>}
196
+ */
27
197
  clear(): Promise<void>;
28
- iterator(namespace?: string): AsyncGenerator<any[], void, unknown>;
29
- has(key: string): Promise<boolean>;
30
- disconnect(): Promise<any>;
198
+ private clearNamespace;
199
+ private setOptions;
200
+ private initClient;
31
201
  }
202
+ /**
203
+ * Will create a Keyv instance with the Redis adapter. This will also set the namespace and useKeyPrefix to false.
204
+ * @param connect
205
+ * @param options
206
+ * @returns {Keyv} - Keyv instance with the Redis adapter
207
+ */
208
+ declare function createKeyv(connect?: string | RedisClientOptions | RedisClientType, options?: KeyvRedisOptions): Keyv;
32
209
 
33
- export { KeyvRedis as default };
210
+ export { type KeyvRedisEntry, type KeyvRedisOptions, type KeyvRedisPropertyOptions, createKeyv, KeyvRedis as default };