@db-bridge/redis 1.0.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/LICENSE +21 -0
- package/README.md +468 -0
- package/dist/index.d.ts +330 -0
- package/dist/index.js +1676 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { EventEmitter } from 'eventemitter3';
|
|
2
|
+
import Redis, { RedisOptions } from 'ioredis';
|
|
3
|
+
import { CacheAdapter, Logger, CacheOptions, QueryResult, DatabaseAdapter } from '@db-bridge/core';
|
|
4
|
+
export { CacheAdapter, ConnectionConfig, DatabaseAdapter } from '@db-bridge/core';
|
|
5
|
+
|
|
6
|
+
declare class RedisCommands {
|
|
7
|
+
private adapter;
|
|
8
|
+
constructor(adapter: RedisAdapter);
|
|
9
|
+
private get client();
|
|
10
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
11
|
+
hset(key: string, field: string, value: string): Promise<number>;
|
|
12
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
13
|
+
hmset(key: string, data: Record<string, string>): Promise<string>;
|
|
14
|
+
hdel(key: string, ...fields: string[]): Promise<number>;
|
|
15
|
+
lpush(key: string, ...values: string[]): Promise<number>;
|
|
16
|
+
rpush(key: string, ...values: string[]): Promise<number>;
|
|
17
|
+
lpop(key: string): Promise<string | null>;
|
|
18
|
+
rpop(key: string): Promise<string | null>;
|
|
19
|
+
lrange(key: string, start: number, stop: number): Promise<string[]>;
|
|
20
|
+
llen(key: string): Promise<number>;
|
|
21
|
+
sadd(key: string, ...members: string[]): Promise<number>;
|
|
22
|
+
srem(key: string, ...members: string[]): Promise<number>;
|
|
23
|
+
smembers(key: string): Promise<string[]>;
|
|
24
|
+
sismember(key: string, member: string): Promise<number>;
|
|
25
|
+
scard(key: string): Promise<number>;
|
|
26
|
+
zadd(key: string, ...args: (string | number)[]): Promise<number>;
|
|
27
|
+
zrem(key: string, ...members: string[]): Promise<number>;
|
|
28
|
+
zrange(key: string, start: number, stop: number, withScores?: boolean): Promise<string[]>;
|
|
29
|
+
zrevrange(key: string, start: number, stop: number, withScores?: boolean): Promise<string[]>;
|
|
30
|
+
zcard(key: string): Promise<number>;
|
|
31
|
+
publish(channel: string, message: string): Promise<number>;
|
|
32
|
+
subscribe(channels: string[], callback: (channel: string, message: string) => void): Promise<void>;
|
|
33
|
+
unsubscribe(channels?: string[]): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface RedisAdapterOptions$1 {
|
|
37
|
+
redis?: RedisOptions;
|
|
38
|
+
keyPrefix?: string;
|
|
39
|
+
ttl?: number;
|
|
40
|
+
logger?: Logger;
|
|
41
|
+
enableCompression?: boolean;
|
|
42
|
+
connectionTimeout?: number;
|
|
43
|
+
commandTimeout?: number;
|
|
44
|
+
retryOptions?: {
|
|
45
|
+
maxRetries?: number;
|
|
46
|
+
retryDelay?: number;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
declare class RedisAdapter extends EventEmitter implements CacheAdapter {
|
|
50
|
+
private client?;
|
|
51
|
+
private isConnected;
|
|
52
|
+
private _commands?;
|
|
53
|
+
private readonly options;
|
|
54
|
+
constructor(options?: RedisAdapterOptions$1);
|
|
55
|
+
connect(config?: RedisOptions): Promise<void>;
|
|
56
|
+
disconnect(): Promise<void>;
|
|
57
|
+
get<T = unknown>(key: string): Promise<T | null>;
|
|
58
|
+
set<T = unknown>(key: string, value: T, ttl?: number): Promise<void>;
|
|
59
|
+
delete(key: string): Promise<boolean>;
|
|
60
|
+
exists(key: string): Promise<boolean>;
|
|
61
|
+
clear(): Promise<void>;
|
|
62
|
+
mget<T = unknown>(keys: string[]): Promise<(T | null)[]>;
|
|
63
|
+
mset<T = unknown>(items: Array<{
|
|
64
|
+
key: string;
|
|
65
|
+
value: T;
|
|
66
|
+
ttl?: number;
|
|
67
|
+
}>): Promise<void>;
|
|
68
|
+
keys(pattern?: string): Promise<string[]>;
|
|
69
|
+
ttl(key: string): Promise<number>;
|
|
70
|
+
expire(key: string, ttl: number): Promise<boolean>;
|
|
71
|
+
increment(key: string, value?: number): Promise<number>;
|
|
72
|
+
del(key: string): Promise<boolean>;
|
|
73
|
+
decrement(key: string, value?: number): Promise<number>;
|
|
74
|
+
ping(): Promise<string>;
|
|
75
|
+
flushdb(): Promise<void>;
|
|
76
|
+
mdel(keys: string[]): Promise<number>;
|
|
77
|
+
persist(key: string): Promise<boolean>;
|
|
78
|
+
setNX(key: string, value: unknown, ttl?: number): Promise<boolean>;
|
|
79
|
+
hset(key: string, field: string, value: string): Promise<number>;
|
|
80
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
81
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
82
|
+
hexists(key: string, field: string): Promise<boolean>;
|
|
83
|
+
hdel(key: string, ...fields: string[]): Promise<number>;
|
|
84
|
+
lpush(key: string, ...values: string[]): Promise<number>;
|
|
85
|
+
rpush(key: string, ...values: string[]): Promise<number>;
|
|
86
|
+
lpop(key: string): Promise<string | null>;
|
|
87
|
+
rpop(key: string): Promise<string | null>;
|
|
88
|
+
llen(key: string): Promise<number>;
|
|
89
|
+
lrange(key: string, start: number, stop: number): Promise<string[]>;
|
|
90
|
+
sadd(key: string, ...members: string[]): Promise<number>;
|
|
91
|
+
srem(key: string, ...members: string[]): Promise<number>;
|
|
92
|
+
smembers(key: string): Promise<string[]>;
|
|
93
|
+
sismember(key: string, member: string): Promise<boolean>;
|
|
94
|
+
scard(key: string): Promise<number>;
|
|
95
|
+
zadd(key: string, score: number, member: string): Promise<number>;
|
|
96
|
+
zrem(key: string, ...members: string[]): Promise<number>;
|
|
97
|
+
zrange(key: string, start: number, stop: number): Promise<string[]>;
|
|
98
|
+
zrangeWithScores(key: string, start: number, stop: number): Promise<[string, number][]>;
|
|
99
|
+
zscore(key: string, member: string): Promise<number | null>;
|
|
100
|
+
zcard(key: string): Promise<number>;
|
|
101
|
+
scan(pattern: string): AsyncGenerator<string>;
|
|
102
|
+
private ensureConnected;
|
|
103
|
+
get commands(): RedisCommands;
|
|
104
|
+
getClient(): Redis | undefined;
|
|
105
|
+
getFullKey(key: string): string;
|
|
106
|
+
private serialize;
|
|
107
|
+
private deserialize;
|
|
108
|
+
private compress;
|
|
109
|
+
private decompress;
|
|
110
|
+
private isCompressed;
|
|
111
|
+
private scanKeys;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface RedisCacheAdapterOptions {
|
|
115
|
+
redis: RedisAdapter;
|
|
116
|
+
defaultTTL?: number;
|
|
117
|
+
keyPrefix?: string;
|
|
118
|
+
invalidationPatterns?: Map<string, string[]>;
|
|
119
|
+
}
|
|
120
|
+
declare class RedisCacheAdapter {
|
|
121
|
+
private redis;
|
|
122
|
+
private defaultTTL;
|
|
123
|
+
private keyPrefix;
|
|
124
|
+
private invalidationPatterns;
|
|
125
|
+
constructor(options: RedisCacheAdapterOptions);
|
|
126
|
+
getCachedQuery<T = unknown>(key: string, options?: CacheOptions): Promise<QueryResult<T> | null>;
|
|
127
|
+
setCachedQuery<T = unknown>(key: string, result: QueryResult<T>, options?: CacheOptions): Promise<void>;
|
|
128
|
+
invalidateQueries(patterns: string[]): Promise<number>;
|
|
129
|
+
invalidateAll(): Promise<void>;
|
|
130
|
+
getStatistics(): Promise<{
|
|
131
|
+
totalKeys: number;
|
|
132
|
+
totalSize: number;
|
|
133
|
+
patterns: string[];
|
|
134
|
+
}>;
|
|
135
|
+
private getQueryKey;
|
|
136
|
+
private registerInvalidation;
|
|
137
|
+
}
|
|
138
|
+
declare function createDatabaseAdapterWithCache(adapter: DatabaseAdapter, cacheAdapter: CacheAdapter, options?: {
|
|
139
|
+
defaultTTL?: number;
|
|
140
|
+
cacheableCommands?: string[];
|
|
141
|
+
}): DatabaseAdapter;
|
|
142
|
+
|
|
143
|
+
interface ConnectionConfig {
|
|
144
|
+
redis?: RedisOptions;
|
|
145
|
+
keyPrefix?: string;
|
|
146
|
+
connectionTimeout?: number;
|
|
147
|
+
retryOptions?: {
|
|
148
|
+
maxRetries?: number;
|
|
149
|
+
retryDelay?: number;
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
declare class RedisConnectionManager extends EventEmitter {
|
|
153
|
+
protected client?: Redis;
|
|
154
|
+
protected isConnected: boolean;
|
|
155
|
+
protected config: ConnectionConfig;
|
|
156
|
+
constructor(config?: ConnectionConfig);
|
|
157
|
+
connect(overrideConfig?: RedisOptions): Promise<void>;
|
|
158
|
+
disconnect(): Promise<void>;
|
|
159
|
+
getClient(): Redis | undefined;
|
|
160
|
+
ensureConnected(): void;
|
|
161
|
+
getFullKey(key: string): string;
|
|
162
|
+
private setupEventHandlers;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
declare class BasicOperationsTrait extends RedisConnectionManager {
|
|
166
|
+
protected commandTimeout: number;
|
|
167
|
+
setCommandTimeout(timeout: number): void;
|
|
168
|
+
get<T = unknown>(key: string): Promise<T | null>;
|
|
169
|
+
set<T = unknown>(key: string, value: T, ttl?: number): Promise<void>;
|
|
170
|
+
delete(key: string): Promise<boolean>;
|
|
171
|
+
del(key: string): Promise<boolean>;
|
|
172
|
+
exists(key: string): Promise<boolean>;
|
|
173
|
+
ttl(key: string): Promise<number>;
|
|
174
|
+
expire(key: string, ttl: number): Promise<boolean>;
|
|
175
|
+
protected serialize<T>(value: T): string;
|
|
176
|
+
protected deserialize<T>(value: string): T;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
declare class BatchOperationsTrait extends BasicOperationsTrait {
|
|
180
|
+
mget<T = unknown>(keys: string[]): Promise<(T | null)[]>;
|
|
181
|
+
mset<T = unknown>(items: Array<{
|
|
182
|
+
key: string;
|
|
183
|
+
value: T;
|
|
184
|
+
ttl?: number;
|
|
185
|
+
}>): Promise<void>;
|
|
186
|
+
clear(): Promise<void>;
|
|
187
|
+
keys(pattern?: string): Promise<string[]>;
|
|
188
|
+
private scanKeys;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
declare class CounterOperationsTrait extends BatchOperationsTrait {
|
|
192
|
+
increment(key: string, value?: number): Promise<number>;
|
|
193
|
+
decrement(key: string, value?: number): Promise<number>;
|
|
194
|
+
incr(key: string): Promise<number>;
|
|
195
|
+
decr(key: string): Promise<number>;
|
|
196
|
+
incrby(key: string, value: number): Promise<number>;
|
|
197
|
+
decrby(key: string, value: number): Promise<number>;
|
|
198
|
+
incrbyfloat(key: string, value: number): Promise<string>;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
interface RedisAdapterOptions {
|
|
202
|
+
redis?: RedisOptions;
|
|
203
|
+
keyPrefix?: string;
|
|
204
|
+
ttl?: number;
|
|
205
|
+
logger?: Logger;
|
|
206
|
+
enableCompression?: boolean;
|
|
207
|
+
connectionTimeout?: number;
|
|
208
|
+
commandTimeout?: number;
|
|
209
|
+
retryOptions?: {
|
|
210
|
+
maxRetries?: number;
|
|
211
|
+
retryDelay?: number;
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
declare class ModularRedisAdapter extends CounterOperationsTrait implements CacheAdapter {
|
|
215
|
+
private _commands?;
|
|
216
|
+
private defaultTtl;
|
|
217
|
+
private logger?;
|
|
218
|
+
private enableCompression;
|
|
219
|
+
constructor(options?: RedisAdapterOptions);
|
|
220
|
+
connect(config?: RedisOptions): Promise<void>;
|
|
221
|
+
disconnect(): Promise<void>;
|
|
222
|
+
set<T = unknown>(key: string, value: T, ttl?: number): Promise<void>;
|
|
223
|
+
get commands(): RedisCommands;
|
|
224
|
+
protected serialize<T>(value: T): string;
|
|
225
|
+
protected deserialize<T>(value: string): T;
|
|
226
|
+
private compress;
|
|
227
|
+
private decompress;
|
|
228
|
+
private isCompressed;
|
|
229
|
+
private setupLogging;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
interface StreamEntry {
|
|
233
|
+
id: string;
|
|
234
|
+
fields: Record<string, string>;
|
|
235
|
+
}
|
|
236
|
+
interface StreamInfo {
|
|
237
|
+
length: number;
|
|
238
|
+
radixTreeKeys: number;
|
|
239
|
+
radixTreeNodes: number;
|
|
240
|
+
groups: number;
|
|
241
|
+
lastGeneratedId: string;
|
|
242
|
+
firstEntry?: StreamEntry;
|
|
243
|
+
lastEntry?: StreamEntry;
|
|
244
|
+
}
|
|
245
|
+
declare class StreamBaseTrait extends EventEmitter {
|
|
246
|
+
protected client: Redis;
|
|
247
|
+
constructor(client: Redis);
|
|
248
|
+
protected parseEntries(raw: any[]): StreamEntry[];
|
|
249
|
+
protected parseFields(raw: string[]): Record<string, string>;
|
|
250
|
+
protected parseStreamResults(raw: any[]): Array<[string, StreamEntry[]]>;
|
|
251
|
+
protected camelCase(str: string): string;
|
|
252
|
+
protected throwError(message: string, key?: string, error?: Error): never;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
declare class StreamCrudTrait extends StreamBaseTrait {
|
|
256
|
+
xadd(key: string, _id: string | "*" | undefined, fields: Record<string, string | number | boolean>, options?: {
|
|
257
|
+
maxlen?: number;
|
|
258
|
+
approximate?: boolean;
|
|
259
|
+
}): Promise<string>;
|
|
260
|
+
xdel(key: string, ...ids: string[]): Promise<number>;
|
|
261
|
+
xtrim(key: string, maxlen: number, approximate?: boolean): Promise<number>;
|
|
262
|
+
xlen(key: string): Promise<number>;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
declare class StreamReadTrait extends StreamCrudTrait {
|
|
266
|
+
xread(streams: Record<string, string>, options?: {
|
|
267
|
+
count?: number;
|
|
268
|
+
block?: number;
|
|
269
|
+
}): Promise<Array<[string, StreamEntry[]]>>;
|
|
270
|
+
xrange(key: string, start?: string, end?: string, count?: number): Promise<StreamEntry[]>;
|
|
271
|
+
xrevrange(key: string, end?: string, start?: string, count?: number): Promise<StreamEntry[]>;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
interface ConsumerGroupInfo {
|
|
275
|
+
name: string;
|
|
276
|
+
consumers: number;
|
|
277
|
+
pending: number;
|
|
278
|
+
lastDeliveredId: string;
|
|
279
|
+
}
|
|
280
|
+
declare class StreamConsumerTrait extends StreamReadTrait {
|
|
281
|
+
xgroupCreate(key: string, groupName: string, id?: string, mkstream?: boolean): Promise<void>;
|
|
282
|
+
xgroupDestroy(key: string, groupName: string): Promise<boolean>;
|
|
283
|
+
xgroupSetId(key: string, groupName: string, id: string): Promise<void>;
|
|
284
|
+
xreadgroup(groupName: string, consumerName: string, streams: Record<string, string>, options?: {
|
|
285
|
+
count?: number;
|
|
286
|
+
block?: number;
|
|
287
|
+
noack?: boolean;
|
|
288
|
+
}): Promise<Array<[string, StreamEntry[]]>>;
|
|
289
|
+
xack(key: string, groupName: string, ...ids: string[]): Promise<number>;
|
|
290
|
+
xpending(key: string, groupName: string, options?: {
|
|
291
|
+
start?: string;
|
|
292
|
+
end?: string;
|
|
293
|
+
count?: number;
|
|
294
|
+
consumer?: string;
|
|
295
|
+
}): Promise<any>;
|
|
296
|
+
xclaim(key: string, groupName: string, consumerName: string, minIdleTime: number, ids: string[], options?: {
|
|
297
|
+
idle?: number;
|
|
298
|
+
time?: number;
|
|
299
|
+
retrycount?: number;
|
|
300
|
+
force?: boolean;
|
|
301
|
+
justid?: boolean;
|
|
302
|
+
}): Promise<StreamEntry[] | string[]>;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
declare class StreamInfoTrait extends StreamConsumerTrait {
|
|
306
|
+
xinfo(subcommand: 'STREAM' | 'GROUPS' | 'CONSUMERS', key: string, groupName?: string): Promise<StreamInfo | ConsumerGroupInfo[] | any[]>;
|
|
307
|
+
private parseStreamInfo;
|
|
308
|
+
private parseGroupsInfo;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
declare class ModularRedisStreamManager extends StreamInfoTrait {
|
|
312
|
+
constructor(client: Redis);
|
|
313
|
+
createStreamWithGroup(key: string, groupName: string, options?: {
|
|
314
|
+
maxlen?: number;
|
|
315
|
+
approximate?: boolean;
|
|
316
|
+
}): Promise<void>;
|
|
317
|
+
getStreamStats(key: string): Promise<{
|
|
318
|
+
length: number;
|
|
319
|
+
info: any;
|
|
320
|
+
groups?: any[];
|
|
321
|
+
}>;
|
|
322
|
+
consumeStream(groupName: string, consumerName: string, keys: string[], options?: {
|
|
323
|
+
count?: number;
|
|
324
|
+
block?: number;
|
|
325
|
+
processMessage: (key: string, message: any) => Promise<void>;
|
|
326
|
+
autoAck?: boolean;
|
|
327
|
+
}): Promise<number>;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export { type ConsumerGroupInfo, ModularRedisAdapter, ModularRedisStreamManager, RedisAdapter, type RedisAdapterOptions$1 as RedisAdapterOptions, RedisCacheAdapter, type RedisCacheAdapterOptions, RedisCommands, type StreamEntry, type StreamInfo, createDatabaseAdapterWithCache };
|