@tsed/ioredis 7.2.3 → 7.3.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.
@@ -3,90 +3,92 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ioRedisStore = exports.IORedisStore = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const ioredis_1 = tslib_1.__importDefault(require("ioredis"));
6
- async function handle(next, parse, cb) {
7
- try {
8
- let result = await next();
9
- if (parse) {
10
- result = result && JSON.parse(result);
11
- }
12
- if (!cb) {
13
- return result;
14
- }
15
- cb(null, result);
16
- }
17
- catch (er) {
18
- if (!cb) {
19
- throw er;
20
- }
21
- cb(er, null);
22
- }
23
- }
6
+ const getVal = (value) => JSON.stringify(value) || '"undefined"';
24
7
  class IORedisStore {
25
8
  name = "redis";
9
+ /**
10
+ * @deprecated
11
+ */
26
12
  isCacheableValue;
13
+ isCacheable;
27
14
  redisCache;
28
15
  storeArgs;
29
- constructor(...args) {
30
- if (args.length > 0 && args[0].redisInstance) {
31
- this.redisCache = args[0].redisInstance;
32
- }
33
- else if (args.length > 0 && args[0].clusterConfig) {
34
- const { nodes, options } = args[0].clusterConfig;
35
- this.redisCache = new ioredis_1.default.Cluster(nodes, options || {});
36
- }
37
- else {
38
- this.redisCache = new ioredis_1.default(...args);
39
- }
16
+ constructor(options) {
17
+ options ||= {};
18
+ this.redisCache =
19
+ "clusterConfig" in options ? new ioredis_1.default.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new ioredis_1.default(options);
40
20
  this.storeArgs = this.redisCache.options;
41
- this.isCacheableValue = this.storeArgs.isCacheableValue || ((value) => value !== undefined && value !== null);
21
+ this.isCacheable = this.isCacheableValue =
22
+ this.storeArgs.isCacheable || this.storeArgs.isCacheableValue || ((value) => value !== undefined && value !== null);
23
+ }
24
+ get client() {
25
+ return this.redisCache;
42
26
  }
43
27
  getClient() {
44
28
  return this.redisCache;
45
29
  }
46
- set(key, value, options, cb) {
47
- if (typeof options === "function") {
48
- cb = options;
49
- options = {};
30
+ async set(key, value, ttl) {
31
+ this.assertCacheable(value);
32
+ ttl = this.getTtl(ttl);
33
+ const val = getVal(value);
34
+ if (ttl) {
35
+ await this.redisCache.setex(key, ttl, val);
36
+ return;
50
37
  }
51
- options = options || {};
52
- return handle(async () => {
53
- if (!this.isCacheableValue(value)) {
54
- throw new Error(`"${value}" is not a cacheable value`);
55
- }
56
- const ttl = options.ttl || options.ttl === 0 ? options.ttl : this.storeArgs.ttl;
57
- const val = JSON.stringify(value) || '"undefined"';
58
- if (ttl) {
59
- return this.redisCache.setex(key, ttl, val);
60
- }
61
- return this.redisCache.set(key, val);
62
- }, false, cb);
38
+ await this.redisCache.set(key, val);
63
39
  }
64
- get(key, options, cb) {
65
- if (typeof options === "function") {
66
- cb = options;
40
+ async get(key, options) {
41
+ const val = await this.redisCache.get(key);
42
+ if (val === undefined || val === null) {
43
+ return undefined;
67
44
  }
68
- return handle(() => this.redisCache.get(key), true, cb);
45
+ return JSON.parse(val);
46
+ }
47
+ async del(key) {
48
+ await this.redisCache.del(key);
69
49
  }
70
- del(key, options, cb) {
71
- if (typeof options === "function") {
72
- cb = options;
50
+ async mset(args, ttl) {
51
+ ttl = this.getTtl(ttl);
52
+ if (ttl) {
53
+ const multi = this.redisCache.multi();
54
+ for (const [key, value] of args) {
55
+ this.assertCacheable(value);
56
+ multi.setex(key, ttl / 1000, getVal(value));
57
+ }
58
+ await multi.exec();
73
59
  }
74
- return handle(() => this.redisCache.del(key), false, cb);
60
+ else
61
+ await this.redisCache.mset(args.flatMap(([key, value]) => {
62
+ this.assertCacheable(value);
63
+ return [key, getVal(value)];
64
+ }));
75
65
  }
76
- reset(cb) {
77
- return handle(() => this.redisCache.flushdb(), false, cb);
66
+ async mget(...args) {
67
+ const x = await this.redisCache.mget(args);
68
+ return x.map((x) => (x === null || x === undefined ? undefined : JSON.parse(x)));
78
69
  }
79
- keys(pattern, cb) {
80
- if (typeof pattern === "function") {
81
- cb = pattern;
82
- pattern = "*";
83
- }
84
- return handle(() => this.redisCache.keys(pattern), false, cb);
70
+ async mdel(...args) {
71
+ await this.redisCache.del(args);
85
72
  }
86
- ttl(key, cb) {
87
- return handle(() => this.redisCache.ttl(key), false, cb);
73
+ async reset() {
74
+ await this.redisCache.flushall();
75
+ }
76
+ keys(pattern = "*") {
77
+ return this.redisCache.keys(pattern);
78
+ }
79
+ ttl(key) {
80
+ return this.redisCache.ttl(key);
81
+ }
82
+ getTtl(ttl) {
83
+ return ttl === undefined ? this.storeArgs.ttl : ttl;
84
+ }
85
+ assertCacheable(value) {
86
+ if (!this.isCacheable(value)) {
87
+ throw new Error(`"${getVal(value)}" is not a cacheable value`);
88
+ }
88
89
  }
89
90
  }
90
91
  exports.IORedisStore = IORedisStore;
91
- exports.ioRedisStore = { create: (...args) => new IORedisStore(...args) };
92
+ const ioRedisStore = (config) => new IORedisStore(config);
93
+ exports.ioRedisStore = ioRedisStore;
92
94
  //# sourceMappingURL=IORedisStore.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"IORedisStore.js","sourceRoot":"","sources":["../../../src/domain/IORedisStore.ts"],"names":[],"mappings":";;;;AACA,8DAAuC;AAGvC,KAAK,UAAU,MAAM,CAAiB,IAA6B,EAAE,KAAc,EAAE,EAA8B;IACjH,IAAI;QACF,IAAI,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QAE1B,IAAI,KAAK,EAAE;YACT,MAAM,GAAG,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,MAAa,CAAC,CAAC;SAC9C;QAED,IAAI,CAAC,EAAE,EAAE;YACP,OAAO,MAAM,CAAC;SACf;QAED,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;KAClB;IAAC,OAAO,EAAE,EAAE;QACX,IAAI,CAAC,EAAE,EAAE;YACP,MAAM,EAAE,CAAC;SACV;QAED,EAAE,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;KACd;AACH,CAAC;AAED,M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AC,OAAO,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED,IAAI,CAAC,OAAe,EAAE,EAAsB;QAC1C,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;YACjC,EAAE,GAAG,OAAO,CAAC;YACb,OAAO,GAAG,GAAG,CAAC;SACf;QAED,OAAO,MAAM,CAAW,GAAG,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;IAC1E,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,EAAqB;QACpC,OAAO,MAAM,CAAS,GAAG,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;CACF;AArFD,oCAqFC;AAEY,QAAA,YAAY,GAAG,EAAC,MAAM,EAAE,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,GAAG,IAAI,CAAC,EAAC,CAAC","sourcesContent":["import {Store} from \"cache-manager\";\nimport Redis, {Cluster} from \"ioredis\";\nimport {Callback} from \"ioredis/built/types\";\n\nasync function handle<Response = any>(next: () => Promise<Response>, parse: boolean, cb?: Callback<Response | null>) {\n try {\n let result = await next();\n\n if (parse) {\n result = result && JSON.parse(result as any);\n }\n\n if (!cb) {\n return result;\n }\n\n cb(null, result);\n } catch (er) {\n if (!cb) {\n throw er;\n }\n\n cb(er, null);\n }\n}\n\nexport class IORedisStore implements Store {\n public name = \"redis\";\n public isCacheableValue;\n private redisCache: Redis | Cluster;\n private storeArgs: any;\n\n constructor(...args: any[]) {\n if (args.length > 0 && args[0].redisInstance) {\n this.redisCache = args[0].redisInstance;\n } else if (args.length > 0 && args[0].clusterConfig) {\n const {nodes, options} = args[0].clusterConfig;\n\n this.redisCache = new Redis.Cluster(nodes, options || {});\n } else {\n this.redisCache = new (Redis as any)(...args);\n }\n\n this.storeArgs = this.redisCache.options;\n this.isCacheableValue = this.storeArgs.isCacheableValue || ((value: any) => value !== undefined && value !== null);\n }\n\n getClient() {\n return this.redisCache;\n }\n\n set(key: string, value: any, options: any, cb?: Callback<\"OK\">) {\n if (typeof options === \"function\") {\n cb = options;\n options = {};\n }\n\n options = options || {};\n\n return handle<\"OK\">(\n async () => {\n if (!this.isCacheableValue(value)) {\n throw new Error(`\"${value}\" is not a cacheable value`);\n }\n\n const ttl = options.ttl || options.ttl === 0 ? options.ttl : this.storeArgs.ttl;\n const val = JSON.stringify(value) || '\"undefined\"';\n\n if (ttl) {\n return this.redisCache.setex(key, ttl, val);\n }\n\n return this.redisCache.set(key, val);\n },\n false,\n cb\n );\n }\n\n get(key: string, options?: any, cb?: Callback<string | null>) {\n if (typeof options === \"function\") {\n cb = options;\n }\n\n return handle(() => this.redisCache.get(key), true, cb);\n }\n\n del(key: string, options: any, cb?: Callback<number>) {\n if (typeof options === \"function\") {\n cb = options;\n }\n\n return handle(() => this.redisCache.del(key), false, cb);\n }\n\n reset(cb?: Callback<\"OK\">) {\n return handle(() => this.redisCache.flushdb(), false, cb);\n }\n\n keys(pattern: string, cb: Callback<string[]>) {\n if (typeof pattern === \"function\") {\n cb = pattern;\n pattern = \"*\";\n }\n\n return handle<string[]>(() => this.redisCache.keys(pattern), false, cb);\n }\n\n ttl(key: string, cb?: Callback<number>) {\n return handle<number>(() => this.redisCache.ttl(key), false, cb);\n }\n}\n\nexport const ioRedisStore = {create: (...args: any[]) => new IORedisStore(...args)};\n"]}
1
+ {"version":3,"file":"IORedisStore.js","sourceRoot":"","sources":["../../../src/domain/IORedisStore.ts"],"names":[],"mappings":";;;;AACA,8DAAkF;AAOlF,MAAM,MAAM,GAAG,CAAC,KAAc,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,aAAa,CAAC;AAE1E,MAAa,YAAY;IAChB,IAAI,GAAG,OAAO,CAAC;IACtB;;OAEG;IACI,gBA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IAAI,CAAC,IAAI,CAAC,CAAC;QAE3C,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAa,CAAC,CAAC,CAAC;IAChG,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,GAAG,IAAc;QAC1B,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC;IAED,KAAK,CAAC,KAAK;QACT,MAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC;IACnC,CAAC;IAED,IAAI,CAAC,UAAkB,GAAG;QACxB,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACvC,CAAC;IAED,GAAG,CAAC,GAAW;QACb,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAClC,CAAC;IAEO,MAAM,CAAC,GAAY;QACzB,OAAO,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;IACtD,CAAC;IAEO,eAAe,CAAC,KAAU;QAChC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE;YAC5B,MAAM,IAAI,KAAK,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC;SAChE;IACH,CAAC;CACF;AAhHD,oCAgHC;AAEM,MAAM,YAAY,GAAG,CAAC,MAA2B,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC;AAAzE,QAAA,YAAY,gBAA6D","sourcesContent":["import type {Config, FactoryConfig, Store} from \"cache-manager\";\nimport Redis, {Cluster, ClusterNode, ClusterOptions, RedisOptions} from \"ioredis\";\n\nexport interface RedisClusterConfig {\n nodes: ClusterNode[];\n options?: ClusterOptions;\n}\n\nconst getVal = (value: unknown) => JSON.stringify(value) || '\"undefined\"';\n\nexport class IORedisStore implements Store {\n public name = \"redis\";\n /**\n * @deprecated\n */\n public isCacheableValue;\n public isCacheable;\n private redisCache: Redis | Cluster;\n private storeArgs: any;\n\n constructor(options?: (RedisOptions | {clusterConfig: RedisClusterConfig}) & Config) {\n options ||= {};\n\n this.redisCache =\n \"clusterConfig\" in options ? new Redis.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new Redis(options);\n\n this.storeArgs = this.redisCache.options;\n this.isCacheable = this.isCacheableValue =\n this.storeArgs.isCacheable || this.storeArgs.isCacheableValue || ((value: any) => value !== undefined && value !== null);\n }\n\n get client() {\n return this.redisCache;\n }\n\n getClient() {\n return this.redisCache;\n }\n\n async set(key: string, value: any, ttl?: number) {\n this.assertCacheable(value);\n\n ttl = this.getTtl(ttl);\n\n const val = getVal(value);\n\n if (ttl) {\n await this.redisCache.setex(key, ttl, val);\n return;\n }\n\n await this.redisCache.set(key, val);\n }\n\n async get<T = any>(key: string, options?: any) {\n const val = await this.redisCache.get(key);\n\n if (val === undefined || val === null) {\n return undefined;\n }\n\n return JSON.parse(val) as T;\n }\n\n async del(key: string) {\n await this.redisCache.del(key);\n }\n\n async mset(args: [string, unknown][], ttl?: number) {\n ttl = this.getTtl(ttl);\n\n if (ttl) {\n const multi = this.redisCache.multi();\n\n for (const [key, value] of args) {\n this.assertCacheable(value);\n\n multi.setex(key, ttl / 1000, getVal(value));\n }\n\n await multi.exec();\n } else\n await this.redisCache.mset(\n args.flatMap(([key, value]) => {\n this.assertCacheable(value);\n\n return [key, getVal(value)] as [string, string];\n })\n );\n }\n\n async mget(...args: string[]) {\n const x = await this.redisCache.mget(args);\n\n return x.map((x) => (x === null || x === undefined ? undefined : (JSON.parse(x) as unknown)));\n }\n\n async mdel(...args: string[]) {\n await this.redisCache.del(args);\n }\n\n async reset() {\n await this.redisCache.flushall();\n }\n\n keys(pattern: string = \"*\") {\n return this.redisCache.keys(pattern);\n }\n\n ttl(key: string) {\n return this.redisCache.ttl(key);\n }\n\n private getTtl(ttl?: number) {\n return ttl === undefined ? this.storeArgs.ttl : ttl;\n }\n\n private assertCacheable(value: any) {\n if (!this.isCacheable(value)) {\n throw new Error(`\"${getVal(value)}\" is not a cacheable value`);\n }\n }\n}\n\nexport const ioRedisStore = (config?: FactoryConfig<any>) => new IORedisStore(config);\n"]}
@@ -1,87 +1,88 @@
1
1
  import Redis from "ioredis";
2
- async function handle(next, parse, cb) {
3
- try {
4
- let result = await next();
5
- if (parse) {
6
- result = result && JSON.parse(result);
7
- }
8
- if (!cb) {
9
- return result;
10
- }
11
- cb(null, result);
12
- }
13
- catch (er) {
14
- if (!cb) {
15
- throw er;
16
- }
17
- cb(er, null);
18
- }
19
- }
2
+ const getVal = (value) => JSON.stringify(value) || '"undefined"';
20
3
  export class IORedisStore {
21
4
  name = "redis";
5
+ /**
6
+ * @deprecated
7
+ */
22
8
  isCacheableValue;
9
+ isCacheable;
23
10
  redisCache;
24
11
  storeArgs;
25
- constructor(...args) {
26
- if (args.length > 0 && args[0].redisInstance) {
27
- this.redisCache = args[0].redisInstance;
28
- }
29
- else if (args.length > 0 && args[0].clusterConfig) {
30
- const { nodes, options } = args[0].clusterConfig;
31
- this.redisCache = new Redis.Cluster(nodes, options || {});
32
- }
33
- else {
34
- this.redisCache = new Redis(...args);
35
- }
12
+ constructor(options) {
13
+ options ||= {};
14
+ this.redisCache =
15
+ "clusterConfig" in options ? new Redis.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new Redis(options);
36
16
  this.storeArgs = this.redisCache.options;
37
- this.isCacheableValue = this.storeArgs.isCacheableValue || ((value) => value !== undefined && value !== null);
17
+ this.isCacheable = this.isCacheableValue =
18
+ this.storeArgs.isCacheable || this.storeArgs.isCacheableValue || ((value) => value !== undefined && value !== null);
19
+ }
20
+ get client() {
21
+ return this.redisCache;
38
22
  }
39
23
  getClient() {
40
24
  return this.redisCache;
41
25
  }
42
- set(key, value, options, cb) {
43
- if (typeof options === "function") {
44
- cb = options;
45
- options = {};
26
+ async set(key, value, ttl) {
27
+ this.assertCacheable(value);
28
+ ttl = this.getTtl(ttl);
29
+ const val = getVal(value);
30
+ if (ttl) {
31
+ await this.redisCache.setex(key, ttl, val);
32
+ return;
46
33
  }
47
- options = options || {};
48
- return handle(async () => {
49
- if (!this.isCacheableValue(value)) {
50
- throw new Error(`"${value}" is not a cacheable value`);
51
- }
52
- const ttl = options.ttl || options.ttl === 0 ? options.ttl : this.storeArgs.ttl;
53
- const val = JSON.stringify(value) || '"undefined"';
54
- if (ttl) {
55
- return this.redisCache.setex(key, ttl, val);
56
- }
57
- return this.redisCache.set(key, val);
58
- }, false, cb);
34
+ await this.redisCache.set(key, val);
59
35
  }
60
- get(key, options, cb) {
61
- if (typeof options === "function") {
62
- cb = options;
36
+ async get(key, options) {
37
+ const val = await this.redisCache.get(key);
38
+ if (val === undefined || val === null) {
39
+ return undefined;
63
40
  }
64
- return handle(() => this.redisCache.get(key), true, cb);
41
+ return JSON.parse(val);
42
+ }
43
+ async del(key) {
44
+ await this.redisCache.del(key);
65
45
  }
66
- del(key, options, cb) {
67
- if (typeof options === "function") {
68
- cb = options;
46
+ async mset(args, ttl) {
47
+ ttl = this.getTtl(ttl);
48
+ if (ttl) {
49
+ const multi = this.redisCache.multi();
50
+ for (const [key, value] of args) {
51
+ this.assertCacheable(value);
52
+ multi.setex(key, ttl / 1000, getVal(value));
53
+ }
54
+ await multi.exec();
69
55
  }
70
- return handle(() => this.redisCache.del(key), false, cb);
56
+ else
57
+ await this.redisCache.mset(args.flatMap(([key, value]) => {
58
+ this.assertCacheable(value);
59
+ return [key, getVal(value)];
60
+ }));
71
61
  }
72
- reset(cb) {
73
- return handle(() => this.redisCache.flushdb(), false, cb);
62
+ async mget(...args) {
63
+ const x = await this.redisCache.mget(args);
64
+ return x.map((x) => (x === null || x === undefined ? undefined : JSON.parse(x)));
74
65
  }
75
- keys(pattern, cb) {
76
- if (typeof pattern === "function") {
77
- cb = pattern;
78
- pattern = "*";
79
- }
80
- return handle(() => this.redisCache.keys(pattern), false, cb);
66
+ async mdel(...args) {
67
+ await this.redisCache.del(args);
81
68
  }
82
- ttl(key, cb) {
83
- return handle(() => this.redisCache.ttl(key), false, cb);
69
+ async reset() {
70
+ await this.redisCache.flushall();
71
+ }
72
+ keys(pattern = "*") {
73
+ return this.redisCache.keys(pattern);
74
+ }
75
+ ttl(key) {
76
+ return this.redisCache.ttl(key);
77
+ }
78
+ getTtl(ttl) {
79
+ return ttl === undefined ? this.storeArgs.ttl : ttl;
80
+ }
81
+ assertCacheable(value) {
82
+ if (!this.isCacheable(value)) {
83
+ throw new Error(`"${getVal(value)}" is not a cacheable value`);
84
+ }
84
85
  }
85
86
  }
86
- export const ioRedisStore = { create: (...args) => new IORedisStore(...args) };
87
+ export const ioRedisStore = (config) => new IORedisStore(config);
87
88
  //# sourceMappingURL=IORedisStore.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"IORedisStore.js","sourceRoot":"","sources":["../../../src/domain/IORedisStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAgB,MAAM,SAAS,CAAC;AAGvC,KAAK,UAAU,MAAM,CAAiB,IAA6B,EAAE,KAAc,EAAE,EAA8B;IACjH,IAAI;QACF,IAAI,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;QAE1B,IAAI,KAAK,EAAE;YACT,MAAM,GAAG,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,MAAa,CAAC,CAAC;SAC9C;QAED,IAAI,CAAC,EAAE,EAAE;YACP,OAAO,MAAM,CAAC;SACf;QAED,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;KAClB;IAAC,OAAO,EAAE,EAAE;QACX,IAAI,CAAC,EAAE,EAAE;YACP,MAAM,EAAE,CAAC;SACV;QAED,EAAE,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC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CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED,IAAI,CAAC,OAAe,EAAE,EAAsB;QAC1C,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;YACjC,EAAE,GAAG,OAAO,CAAC;YACb,OAAO,GAAG,GAAG,CAAC;SACf;QAED,OAAO,MAAM,CAAW,GAAG,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;IAC1E,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,EAAqB;QACpC,OAAO,MAAM,CAAS,GAAG,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;CACF;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,EAAC,MAAM,EAAE,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,IAAI,YAAY,CAAC,GAAG,IAAI,CAAC,EAAC,CAAC","sourcesContent":["import {Store} from \"cache-manager\";\nimport Redis, {Cluster} from \"ioredis\";\nimport {Callback} from \"ioredis/built/types\";\n\nasync function handle<Response = any>(next: () => Promise<Response>, parse: boolean, cb?: Callback<Response | null>) {\n try {\n let result = await next();\n\n if (parse) {\n result = result && JSON.parse(result as any);\n }\n\n if (!cb) {\n return result;\n }\n\n cb(null, result);\n } catch (er) {\n if (!cb) {\n throw er;\n }\n\n cb(er, null);\n }\n}\n\nexport class IORedisStore implements Store {\n public name = \"redis\";\n public isCacheableValue;\n private redisCache: Redis | Cluster;\n private storeArgs: any;\n\n constructor(...args: any[]) {\n if (args.length > 0 && args[0].redisInstance) {\n this.redisCache = args[0].redisInstance;\n } else if (args.length > 0 && args[0].clusterConfig) {\n const {nodes, options} = args[0].clusterConfig;\n\n this.redisCache = new Redis.Cluster(nodes, options || {});\n } else {\n this.redisCache = new (Redis as any)(...args);\n }\n\n this.storeArgs = this.redisCache.options;\n this.isCacheableValue = this.storeArgs.isCacheableValue || ((value: any) => value !== undefined && value !== null);\n }\n\n getClient() {\n return this.redisCache;\n }\n\n set(key: string, value: any, options: any, cb?: Callback<\"OK\">) {\n if (typeof options === \"function\") {\n cb = options;\n options = {};\n }\n\n options = options || {};\n\n return handle<\"OK\">(\n async () => {\n if (!this.isCacheableValue(value)) {\n throw new Error(`\"${value}\" is not a cacheable value`);\n }\n\n const ttl = options.ttl || options.ttl === 0 ? options.ttl : this.storeArgs.ttl;\n const val = JSON.stringify(value) || '\"undefined\"';\n\n if (ttl) {\n return this.redisCache.setex(key, ttl, val);\n }\n\n return this.redisCache.set(key, val);\n },\n false,\n cb\n );\n }\n\n get(key: string, options?: any, cb?: Callback<string | null>) {\n if (typeof options === \"function\") {\n cb = options;\n }\n\n return handle(() => this.redisCache.get(key), true, cb);\n }\n\n del(key: string, options: any, cb?: Callback<number>) {\n if (typeof options === \"function\") {\n cb = options;\n }\n\n return handle(() => this.redisCache.del(key), false, cb);\n }\n\n reset(cb?: Callback<\"OK\">) {\n return handle(() => this.redisCache.flushdb(), false, cb);\n }\n\n keys(pattern: string, cb: Callback<string[]>) {\n if (typeof pattern === \"function\") {\n cb = pattern;\n pattern = \"*\";\n }\n\n return handle<string[]>(() => this.redisCache.keys(pattern), false, cb);\n }\n\n ttl(key: string, cb?: Callback<number>) {\n return handle<number>(() => this.redisCache.ttl(key), false, cb);\n }\n}\n\nexport const ioRedisStore = {create: (...args: any[]) => new IORedisStore(...args)};\n"]}
1
+ {"version":3,"file":"IORedisStore.js","sourceRoot":"","sources":["../../../src/domain/IORedisStore.ts"],"names":[],"mappings":"AACA,OAAO,KAA2D,MAAM,SAAS,CAAC;AAOlF,MAAM,MAAM,GAAG,CAAC,KAAc,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,aAAa,CAAC;AAE1E,MAAM,OAAO,YAAY;IACh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Options} from \"ioredis\";\n\nexport interface RedisClusterConfig {\n nodes: ClusterNode[];\n options?: ClusterOptions;\n}\n\nconst getVal = (value: unknown) => JSON.stringify(value) || '\"undefined\"';\n\nexport class IORedisStore implements Store {\n public name = \"redis\";\n /**\n * @deprecated\n */\n public isCacheableValue;\n public isCacheable;\n private redisCache: Redis | Cluster;\n private storeArgs: any;\n\n constructor(options?: (RedisOptions | {clusterConfig: RedisClusterConfig}) & Config) {\n options ||= {};\n\n this.redisCache =\n \"clusterConfig\" in options ? new Redis.Cluster(options.clusterConfig.nodes, options.clusterConfig.options) : new Redis(options);\n\n this.storeArgs = this.redisCache.options;\n this.isCacheable = this.isCacheableValue =\n this.storeArgs.isCacheable || this.storeArgs.isCacheableValue || ((value: any) => value !== undefined && value !== null);\n }\n\n get client() {\n return this.redisCache;\n }\n\n getClient() {\n return this.redisCache;\n }\n\n async set(key: string, value: any, ttl?: number) {\n this.assertCacheable(value);\n\n ttl = this.getTtl(ttl);\n\n const val = getVal(value);\n\n if (ttl) {\n await this.redisCache.setex(key, ttl, val);\n return;\n }\n\n await this.redisCache.set(key, val);\n }\n\n async get<T = any>(key: string, options?: any) {\n const val = await this.redisCache.get(key);\n\n if (val === undefined || val === null) {\n return undefined;\n }\n\n return JSON.parse(val) as T;\n }\n\n async del(key: string) {\n await this.redisCache.del(key);\n }\n\n async mset(args: [string, unknown][], ttl?: number) {\n ttl = this.getTtl(ttl);\n\n if (ttl) {\n const multi = this.redisCache.multi();\n\n for (const [key, value] of args) {\n this.assertCacheable(value);\n\n multi.setex(key, ttl / 1000, getVal(value));\n }\n\n await multi.exec();\n } else\n await this.redisCache.mset(\n args.flatMap(([key, value]) => {\n this.assertCacheable(value);\n\n return [key, getVal(value)] as [string, string];\n })\n );\n }\n\n async mget(...args: string[]) {\n const x = await this.redisCache.mget(args);\n\n return x.map((x) => (x === null || x === undefined ? undefined : (JSON.parse(x) as unknown)));\n }\n\n async mdel(...args: string[]) {\n await this.redisCache.del(args);\n }\n\n async reset() {\n await this.redisCache.flushall();\n }\n\n keys(pattern: string = \"*\") {\n return this.redisCache.keys(pattern);\n }\n\n ttl(key: string) {\n return this.redisCache.ttl(key);\n }\n\n private getTtl(ttl?: number) {\n return ttl === undefined ? this.storeArgs.ttl : ttl;\n }\n\n private assertCacheable(value: any) {\n if (!this.isCacheable(value)) {\n throw new Error(`\"${getVal(value)}\" is not a cacheable value`);\n }\n }\n}\n\nexport const ioRedisStore = (config?: FactoryConfig<any>) => new IORedisStore(config);\n"]}
@@ -1,20 +1,33 @@
1
- import { Store } from "cache-manager";
2
- import Redis, { Cluster } from "ioredis";
3
- import { Callback } from "ioredis/built/types";
1
+ import type { Config, FactoryConfig, Store } from "cache-manager";
2
+ import Redis, { Cluster, ClusterNode, ClusterOptions, RedisOptions } from "ioredis";
3
+ export interface RedisClusterConfig {
4
+ nodes: ClusterNode[];
5
+ options?: ClusterOptions;
6
+ }
4
7
  export declare class IORedisStore implements Store {
5
8
  name: string;
9
+ /**
10
+ * @deprecated
11
+ */
6
12
  isCacheableValue: any;
13
+ isCacheable: any;
7
14
  private redisCache;
8
15
  private storeArgs;
9
- constructor(...args: any[]);
16
+ constructor(options?: (RedisOptions | {
17
+ clusterConfig: RedisClusterConfig;
18
+ }) & Config);
19
+ get client(): Redis | Cluster;
10
20
  getClient(): Redis | Cluster;
11
- set(key: string, value: any, options: any, cb?: Callback<"OK">): Promise<"OK" | undefined>;
12
- get(key: string, options?: any, cb?: Callback<string | null>): Promise<string | null | undefined>;
13
- del(key: string, options: any, cb?: Callback<number>): Promise<number | undefined>;
14
- reset(cb?: Callback<"OK">): Promise<"OK" | undefined>;
15
- keys(pattern: string, cb: Callback<string[]>): Promise<string[] | undefined>;
16
- ttl(key: string, cb?: Callback<number>): Promise<number | undefined>;
21
+ set(key: string, value: any, ttl?: number): Promise<void>;
22
+ get<T = any>(key: string, options?: any): Promise<T | undefined>;
23
+ del(key: string): Promise<void>;
24
+ mset(args: [string, unknown][], ttl?: number): Promise<void>;
25
+ mget(...args: string[]): Promise<unknown[]>;
26
+ mdel(...args: string[]): Promise<void>;
27
+ reset(): Promise<void>;
28
+ keys(pattern?: string): Promise<string[]>;
29
+ ttl(key: string): Promise<number>;
30
+ private getTtl;
31
+ private assertCacheable;
17
32
  }
18
- export declare const ioRedisStore: {
19
- create: (...args: any[]) => IORedisStore;
20
- };
33
+ export declare const ioRedisStore: (config?: FactoryConfig<any>) => IORedisStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsed/ioredis",
3
- "version": "7.2.3",
3
+ "version": "7.3.0",
4
4
  "description": "IO Redis connection for Ts.ED",
5
5
  "private": false,
6
6
  "source": "./src/index.ts",
@@ -24,15 +24,15 @@
24
24
  "tslib": "2.4.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@tsed/core": "7.2.3",
28
- "@tsed/di": "7.2.3",
27
+ "@tsed/core": "7.3.0",
28
+ "@tsed/di": "7.3.0",
29
29
  "@types/ioredis-mock": "^5.6.0",
30
30
  "ioredis": "5.2.3",
31
31
  "ioredis-mock": "8.2.2"
32
32
  },
33
33
  "peerDependencies": {
34
- "@tsed/core": "^7.2.3",
35
- "@tsed/di": "^7.2.3",
34
+ "@tsed/core": "^7.3.0",
35
+ "@tsed/di": "^7.3.0",
36
36
  "ioredis": ">=5.2.3",
37
37
  "ioredis-mock": ">=8.2.2"
38
38
  },