@tsed/ioredis 8.9.0-beta.1 → 8.9.0-beta.3
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/lib/esm/index.js +1 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/utils/createConnection.js +55 -0
- package/lib/esm/utils/createConnection.js.map +1 -0
- package/lib/esm/utils/registerConnectionProvider.js +12 -71
- package/lib/esm/utils/registerConnectionProvider.js.map +1 -1
- package/lib/types/index.d.ts +1 -0
- package/lib/types/utils/createConnection.d.ts +3 -0
- package/lib/types/utils/registerConnectionProvider.d.ts +5 -1
- package/package.json +7 -7
- package/src/index.ts +1 -0
- package/src/utils/createConnection.ts +68 -0
- package/src/utils/registerConnectionProvider.ts +12 -81
package/lib/esm/index.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
export * from "./domain/IORedisConfiguration.js";
|
|
5
5
|
export * from "./domain/IORedisStore.js";
|
|
6
6
|
export * from "./domain/IORedisTest.js";
|
|
7
|
+
export * from "./utils/createConnection.js";
|
|
7
8
|
export * from "./utils/mockConnections.js";
|
|
8
9
|
export * from "./utils/registerConnectionProvider.js";
|
|
9
10
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,kCAAkC,CAAC;AACjD,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,uCAAuC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,cAAc,kCAAkC,CAAC;AACjD,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,uCAAuC,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { setValue } from "@tsed/core";
|
|
2
|
+
import { configuration, logger } from "@tsed/di";
|
|
3
|
+
import ioredis from "ioredis";
|
|
4
|
+
import { ioRedisStore } from "../domain/IORedisStore.js";
|
|
5
|
+
export async function createConnection(config) {
|
|
6
|
+
const { name, cache, ...redisOptions } = config;
|
|
7
|
+
const retryStrategy = (times) => {
|
|
8
|
+
logger().fatal({ event: "REDIS_ERROR", message: `Redis is not responding - Retry count: ${times}` });
|
|
9
|
+
return 2000;
|
|
10
|
+
};
|
|
11
|
+
let connection;
|
|
12
|
+
const reconnectOnError = (err) => {
|
|
13
|
+
logger().fatal({
|
|
14
|
+
event: "REDIS_ERROR",
|
|
15
|
+
message: `Redis - Reconnect on error: ${(err && err.message) || err}`,
|
|
16
|
+
stack: err?.stack
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
if ("nodes" in redisOptions) {
|
|
20
|
+
const { nodes, ...clusterOptions } = redisOptions;
|
|
21
|
+
setValue(clusterOptions, "clusterRetryStrategy", retryStrategy);
|
|
22
|
+
setValue(clusterOptions, "redisOptions.reconnectOnError", reconnectOnError);
|
|
23
|
+
connection = new ioredis.Redis.Cluster(nodes, {
|
|
24
|
+
...clusterOptions,
|
|
25
|
+
lazyConnect: true
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
else if ("sentinels" in redisOptions) {
|
|
29
|
+
const { sentinels, sentinelName = name, ...sentinelsOptions } = redisOptions;
|
|
30
|
+
setValue(sentinelsOptions, "sentinelRetryStrategy", retryStrategy);
|
|
31
|
+
setValue(sentinelsOptions, "redisOptions.reconnectOnError", reconnectOnError);
|
|
32
|
+
connection = new ioredis.Redis({
|
|
33
|
+
name: String(sentinelName),
|
|
34
|
+
sentinels,
|
|
35
|
+
...sentinelsOptions,
|
|
36
|
+
lazyConnect: true
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
connection = new ioredis.Redis({
|
|
41
|
+
reconnectOnError,
|
|
42
|
+
...redisOptions,
|
|
43
|
+
lazyConnect: true
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
await connection.connect();
|
|
47
|
+
logger().info("Connected to redis database...");
|
|
48
|
+
if (configuration().get("cache") && cache) {
|
|
49
|
+
configuration().set("cache.redisInstance", connection);
|
|
50
|
+
configuration().set("cache.store", ioRedisStore);
|
|
51
|
+
}
|
|
52
|
+
connection.name = name;
|
|
53
|
+
return connection;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=createConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createConnection.js","sourceRoot":"","sources":["../../../src/utils/createConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,YAAY,CAAC;AACpC,OAAO,EAAC,aAAa,EAAE,MAAM,EAAC,MAAM,UAAU,CAAC;AAC/C,OAAO,OAAsD,MAAM,SAAS,CAAC;AAG7E,OAAO,EAAC,YAAY,EAAC,MAAM,2BAA2B,CAAC;AAEvD,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,MAA4B;IACjE,MAAM,EAAC,IAAI,EAAE,KAAK,EAAE,GAAG,YAAY,EAAC,GAAG,MAAM,CAAC;IAE9C,MAAM,aAAa,GAAG,CAAC,KAAa,EAAE,EAAE;QACtC,MAAM,EAAE,CAAC,KAAK,CAAC,EAAC,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,0CAA0C,KAAK,EAAE,EAAC,CAAC,CAAC;QACnG,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,IAAI,UAA2B,CAAC;IAEhC,MAAM,gBAAgB,GAAG,CAAC,GAAQ,EAAE,EAAE;QACpC,MAAM,EAAE,CAAC,KAAK,CAAC;YACb,KAAK,EAAE,aAAa;YACpB,OAAO,EAAE,+BAA+B,CAAC,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,IAAI,GAAG,EAAE;YACrE,KAAK,EAAE,GAAG,EAAE,KAAK;SAClB,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,IAAI,OAAO,IAAI,YAAY,EAAE,CAAC;QAC5B,MAAM,EAAC,KAAK,EAAE,GAAG,cAAc,EAAC,GAAG,YAAY,CAAC;QAEhD,QAAQ,CAAC,cAAc,EAAE,sBAAsB,EAAE,aAAa,CAAC,CAAC;QAChE,QAAQ,CAAC,cAAc,EAAE,+BAA+B,EAAE,gBAAgB,CAAC,CAAC;QAE5E,UAAU,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE;YAC5C,GAAG,cAAc;YACjB,WAAW,EAAE,IAAI;SAClB,CAAC,CAAC;IACL,CAAC;SAAM,IAAI,WAAW,IAAI,YAAY,EAAE,CAAC;QACvC,MAAM,EAAC,SAAS,EAAE,YAAY,GAAG,IAAI,EAAE,GAAG,gBAAgB,EAAC,GAAG,YAAY,CAAC;QAE3E,QAAQ,CAAC,gBAAgB,EAAE,uBAAuB,EAAE,aAAa,CAAC,CAAC;QACnE,QAAQ,CAAC,gBAAgB,EAAE,+BAA+B,EAAE,gBAAgB,CAAC,CAAC;QAE9E,UAAU,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC;YAC7B,IAAI,EAAE,MAAM,CAAC,YAAY,CAAC;YAC1B,SAAS;YACT,GAAG,gBAAgB;YACnB,WAAW,EAAE,IAAI;SAClB,CAAC,CAAC;IACL,CAAC;SAAM,CAAC;QACN,UAAU,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC;YAC7B,gBAAgB;YAChB,GAAG,YAAY;YACf,WAAW,EAAE,IAAI;SACF,CAAC,CAAC;IACrB,CAAC;IAED,MAAM,UAAU,CAAC,OAAO,EAAE,CAAC;IAE3B,MAAM,EAAE,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAC;IAEhD,IAAI,aAAa,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,KAAK,EAAE,CAAC;QAC1C,aAAa,EAAE,CAAC,GAAG,CAAC,qBAAqB,EAAE,UAAU,CAAC,CAAC;QACvD,aAAa,EAAE,CAAC,GAAG,CAAC,aAAa,EAAE,YAAY,CAAC,CAAC;IACnD,CAAC;IAEA,UAAkB,CAAC,IAAI,GAAG,IAAI,CAAC;IAEhC,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
|
@@ -1,75 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Logger } from "@tsed/logger";
|
|
4
|
-
import ioredis from "ioredis";
|
|
5
|
-
import { ioRedisStore } from "../domain/IORedisStore.js";
|
|
1
|
+
import { constant, injectable } from "@tsed/di";
|
|
2
|
+
import { createConnection } from "./createConnection.js";
|
|
6
3
|
export const IOREDIS_CONNECTIONS = Symbol.for("ioredis:connections");
|
|
7
4
|
export function registerConnectionProvider({ token, provide, name = "default" }) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return 2000;
|
|
19
|
-
};
|
|
20
|
-
if (item) {
|
|
21
|
-
const { name, cache, ...redisOptions } = item;
|
|
22
|
-
let connection;
|
|
23
|
-
const reconnectOnError = (err) => {
|
|
24
|
-
logger.fatal({
|
|
25
|
-
event: "REDIS_ERROR",
|
|
26
|
-
message: `Redis - Reconnect on error: ${(err && err.message) || err}`,
|
|
27
|
-
stack: err?.stack
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
if ("nodes" in redisOptions) {
|
|
31
|
-
const { nodes, ...clusterOptions } = redisOptions;
|
|
32
|
-
setValue(clusterOptions, "clusterRetryStrategy", retryStrategy);
|
|
33
|
-
setValue(clusterOptions, "redisOptions.reconnectOnError", reconnectOnError);
|
|
34
|
-
connection = new ioredis.Redis.Cluster(nodes, {
|
|
35
|
-
...clusterOptions,
|
|
36
|
-
lazyConnect: true
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
else if ("sentinels" in redisOptions) {
|
|
40
|
-
const { sentinels, sentinelName = name, ...sentinelsOptions } = redisOptions;
|
|
41
|
-
setValue(sentinelsOptions, "sentinelRetryStrategy", retryStrategy);
|
|
42
|
-
setValue(sentinelsOptions, "redisOptions.reconnectOnError", reconnectOnError);
|
|
43
|
-
connection = new ioredis.Redis({
|
|
44
|
-
name: String(sentinelName),
|
|
45
|
-
sentinels,
|
|
46
|
-
...sentinelsOptions,
|
|
47
|
-
lazyConnect: true
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
connection = new ioredis.Redis({
|
|
52
|
-
reconnectOnError,
|
|
53
|
-
...redisOptions,
|
|
54
|
-
lazyConnect: true
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
await connection.connect();
|
|
58
|
-
logger.info("Connected to redis database...");
|
|
59
|
-
if (configuration.get("cache") && cache) {
|
|
60
|
-
configuration.set("cache.redisInstance", connection);
|
|
61
|
-
configuration.set("cache.store", ioRedisStore);
|
|
62
|
-
}
|
|
63
|
-
connection.name = name;
|
|
64
|
-
return connection;
|
|
65
|
-
}
|
|
66
|
-
return { name };
|
|
67
|
-
},
|
|
68
|
-
hooks: {
|
|
69
|
-
$onDestroy(connection) {
|
|
70
|
-
return connection.disconnect && connection.disconnect();
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
});
|
|
5
|
+
return injectable(provide || token, {
|
|
6
|
+
connectionName: name
|
|
7
|
+
})
|
|
8
|
+
.type(IOREDIS_CONNECTIONS)
|
|
9
|
+
.asyncFactory(() => {
|
|
10
|
+
const items = constant("ioredis", []);
|
|
11
|
+
const item = items.find((item) => item.name === name);
|
|
12
|
+
return item ? createConnection({ ...item, name }) : { name };
|
|
13
|
+
})
|
|
14
|
+
.token();
|
|
74
15
|
}
|
|
75
16
|
//# sourceMappingURL=registerConnectionProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registerConnectionProvider.js","sourceRoot":"","sources":["../../../src/utils/registerConnectionProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,
|
|
1
|
+
{"version":3,"file":"registerConnectionProvider.js","sourceRoot":"","sources":["../../../src/utils/registerConnectionProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,UAAU,EAAgB,MAAM,UAAU,CAAC;AAI7D,OAAO,EAAC,gBAAgB,EAAC,MAAM,uBAAuB,CAAC;AAWvD,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;AAGrE,MAAM,UAAU,0BAA0B,CAAC,EAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAG,SAAS,EAAgC;IAC1G,OAAO,UAAU,CAAC,OAAO,IAAI,KAAK,EAAE;QAClC,cAAc,EAAE,IAAI;KACrB,CAAC;SACC,IAAI,CAAC,mBAAmB,CAAC;SACzB,YAAY,CAAC,GAAG,EAAE;QACjB,MAAM,KAAK,GAAG,QAAQ,CAAyB,SAAS,EAAE,EAAE,CAAC,CAAC;QAC9D,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;QAEtD,OAAO,IAAI,CAAC,CAAC,CAAC,gBAAgB,CAAC,EAAC,GAAG,IAAI,EAAE,IAAI,EAAC,CAAC,CAAC,CAAC,CAAE,EAAC,IAAI,EAAS,CAAC;IACpE,CAAC,CAAC;SACD,KAAK,EAAE,CAAC;AACb,CAAC"}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -4,5 +4,6 @@
|
|
|
4
4
|
export * from "./domain/IORedisConfiguration.js";
|
|
5
5
|
export * from "./domain/IORedisStore.js";
|
|
6
6
|
export * from "./domain/IORedisTest.js";
|
|
7
|
+
export * from "./utils/createConnection.js";
|
|
7
8
|
export * from "./utils/mockConnections.js";
|
|
8
9
|
export * from "./utils/registerConnectionProvider.js";
|
|
@@ -12,4 +12,8 @@ export declare const IOREDIS_CONNECTIONS: unique symbol;
|
|
|
12
12
|
export type IORedis = Redis & {
|
|
13
13
|
name: string;
|
|
14
14
|
};
|
|
15
|
-
export declare function registerConnectionProvider({ token, provide, name }: CreateConnectionProviderProps):
|
|
15
|
+
export declare function registerConnectionProvider({ token, provide, name }: CreateConnectionProviderProps): {
|
|
16
|
+
readonly __type: "token_factory"; /**
|
|
17
|
+
* @deprecated use `token` instead of `provide`
|
|
18
|
+
*/
|
|
19
|
+
};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/ioredis",
|
|
3
3
|
"description": "IO Redis connection for Ts.ED",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.9.0-beta.
|
|
5
|
+
"version": "8.9.0-beta.3",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"tslib": "2.7.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@tsed/barrels": "8.9.0-beta.
|
|
30
|
-
"@tsed/core": "8.9.0-beta.
|
|
31
|
-
"@tsed/di": "8.9.0-beta.
|
|
32
|
-
"@tsed/typescript": "8.9.0-beta.
|
|
29
|
+
"@tsed/barrels": "8.9.0-beta.3",
|
|
30
|
+
"@tsed/core": "8.9.0-beta.3",
|
|
31
|
+
"@tsed/di": "8.9.0-beta.3",
|
|
32
|
+
"@tsed/typescript": "8.9.0-beta.3",
|
|
33
33
|
"@types/ioredis-mock": "^8.2.5",
|
|
34
34
|
"eslint": "9.12.0",
|
|
35
35
|
"ioredis": "5.4.1",
|
|
@@ -38,8 +38,8 @@
|
|
|
38
38
|
"vitest": "3.1.1"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@tsed/core": ">=8.9.0-beta.
|
|
42
|
-
"@tsed/di": ">=8.9.0-beta.
|
|
41
|
+
"@tsed/core": ">=8.9.0-beta.3",
|
|
42
|
+
"@tsed/di": ">=8.9.0-beta.3",
|
|
43
43
|
"ioredis": ">=5.2.3",
|
|
44
44
|
"ioredis-mock": ">=8.2.2"
|
|
45
45
|
},
|
package/src/index.ts
CHANGED
|
@@ -4,5 +4,6 @@
|
|
|
4
4
|
export * from "./domain/IORedisConfiguration.js";
|
|
5
5
|
export * from "./domain/IORedisStore.js";
|
|
6
6
|
export * from "./domain/IORedisTest.js";
|
|
7
|
+
export * from "./utils/createConnection.js";
|
|
7
8
|
export * from "./utils/mockConnections.js";
|
|
8
9
|
export * from "./utils/registerConnectionProvider.js";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {setValue} from "@tsed/core";
|
|
2
|
+
import {configuration, logger} from "@tsed/di";
|
|
3
|
+
import ioredis, {type Cluster, type Redis, type RedisOptions} from "ioredis";
|
|
4
|
+
|
|
5
|
+
import {IORedisConfiguration} from "../domain/IORedisConfiguration.js";
|
|
6
|
+
import {ioRedisStore} from "../domain/IORedisStore.js";
|
|
7
|
+
|
|
8
|
+
export async function createConnection(config: IORedisConfiguration) {
|
|
9
|
+
const {name, cache, ...redisOptions} = config;
|
|
10
|
+
|
|
11
|
+
const retryStrategy = (times: number) => {
|
|
12
|
+
logger().fatal({event: "REDIS_ERROR", message: `Redis is not responding - Retry count: ${times}`});
|
|
13
|
+
return 2000;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
let connection: Cluster | Redis;
|
|
17
|
+
|
|
18
|
+
const reconnectOnError = (err: any) => {
|
|
19
|
+
logger().fatal({
|
|
20
|
+
event: "REDIS_ERROR",
|
|
21
|
+
message: `Redis - Reconnect on error: ${(err && err.message) || err}`,
|
|
22
|
+
stack: err?.stack
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
if ("nodes" in redisOptions) {
|
|
27
|
+
const {nodes, ...clusterOptions} = redisOptions;
|
|
28
|
+
|
|
29
|
+
setValue(clusterOptions, "clusterRetryStrategy", retryStrategy);
|
|
30
|
+
setValue(clusterOptions, "redisOptions.reconnectOnError", reconnectOnError);
|
|
31
|
+
|
|
32
|
+
connection = new ioredis.Redis.Cluster(nodes, {
|
|
33
|
+
...clusterOptions,
|
|
34
|
+
lazyConnect: true
|
|
35
|
+
});
|
|
36
|
+
} else if ("sentinels" in redisOptions) {
|
|
37
|
+
const {sentinels, sentinelName = name, ...sentinelsOptions} = redisOptions;
|
|
38
|
+
|
|
39
|
+
setValue(sentinelsOptions, "sentinelRetryStrategy", retryStrategy);
|
|
40
|
+
setValue(sentinelsOptions, "redisOptions.reconnectOnError", reconnectOnError);
|
|
41
|
+
|
|
42
|
+
connection = new ioredis.Redis({
|
|
43
|
+
name: String(sentinelName),
|
|
44
|
+
sentinels,
|
|
45
|
+
...sentinelsOptions,
|
|
46
|
+
lazyConnect: true
|
|
47
|
+
});
|
|
48
|
+
} else {
|
|
49
|
+
connection = new ioredis.Redis({
|
|
50
|
+
reconnectOnError,
|
|
51
|
+
...redisOptions,
|
|
52
|
+
lazyConnect: true
|
|
53
|
+
} as RedisOptions);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
await connection.connect();
|
|
57
|
+
|
|
58
|
+
logger().info("Connected to redis database...");
|
|
59
|
+
|
|
60
|
+
if (configuration().get("cache") && cache) {
|
|
61
|
+
configuration().set("cache.redisInstance", connection);
|
|
62
|
+
configuration().set("cache.store", ioRedisStore);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
(connection as any).name = name;
|
|
66
|
+
|
|
67
|
+
return connection;
|
|
68
|
+
}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {Logger} from "@tsed/logger";
|
|
4
|
-
import type {Cluster, Redis, RedisOptions} from "ioredis";
|
|
5
|
-
import ioredis from "ioredis";
|
|
1
|
+
import {constant, injectable, TokenProvider} from "@tsed/di";
|
|
2
|
+
import type {Redis} from "ioredis";
|
|
6
3
|
|
|
7
4
|
import {IORedisConfiguration} from "../domain/IORedisConfiguration.js";
|
|
8
|
-
import {
|
|
5
|
+
import {createConnection} from "./createConnection.js";
|
|
9
6
|
|
|
10
7
|
export interface CreateConnectionProviderProps {
|
|
11
8
|
token: TokenProvider;
|
|
@@ -20,81 +17,15 @@ export const IOREDIS_CONNECTIONS = Symbol.for("ioredis:connections");
|
|
|
20
17
|
export type IORedis = Redis & {name: string};
|
|
21
18
|
|
|
22
19
|
export function registerConnectionProvider({token, provide, name = "default"}: CreateConnectionProviderProps) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const items = configuration.get<IORedisConfiguration[]>("ioredis", []);
|
|
20
|
+
return injectable(provide || token, {
|
|
21
|
+
connectionName: name
|
|
22
|
+
})
|
|
23
|
+
.type(IOREDIS_CONNECTIONS)
|
|
24
|
+
.asyncFactory(() => {
|
|
25
|
+
const items = constant<IORedisConfiguration[]>("ioredis", []);
|
|
30
26
|
const item = items.find((item) => item.name === name);
|
|
31
|
-
const retryStrategy = (times: number) => {
|
|
32
|
-
logger.fatal({event: "REDIS_ERROR", message: `Redis is not responding - Retry count: ${times}`});
|
|
33
|
-
return 2000;
|
|
34
|
-
};
|
|
35
27
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const reconnectOnError = (err: any) => {
|
|
41
|
-
logger.fatal({
|
|
42
|
-
event: "REDIS_ERROR",
|
|
43
|
-
message: `Redis - Reconnect on error: ${(err && err.message) || err}`,
|
|
44
|
-
stack: err?.stack
|
|
45
|
-
});
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
if ("nodes" in redisOptions) {
|
|
49
|
-
const {nodes, ...clusterOptions} = redisOptions;
|
|
50
|
-
|
|
51
|
-
setValue(clusterOptions, "clusterRetryStrategy", retryStrategy);
|
|
52
|
-
setValue(clusterOptions, "redisOptions.reconnectOnError", reconnectOnError);
|
|
53
|
-
|
|
54
|
-
connection = new ioredis.Redis.Cluster(nodes, {
|
|
55
|
-
...clusterOptions,
|
|
56
|
-
lazyConnect: true
|
|
57
|
-
});
|
|
58
|
-
} else if ("sentinels" in redisOptions) {
|
|
59
|
-
const {sentinels, sentinelName = name, ...sentinelsOptions} = redisOptions;
|
|
60
|
-
|
|
61
|
-
setValue(sentinelsOptions, "sentinelRetryStrategy", retryStrategy);
|
|
62
|
-
setValue(sentinelsOptions, "redisOptions.reconnectOnError", reconnectOnError);
|
|
63
|
-
|
|
64
|
-
connection = new ioredis.Redis({
|
|
65
|
-
name: String(sentinelName),
|
|
66
|
-
sentinels,
|
|
67
|
-
...sentinelsOptions,
|
|
68
|
-
lazyConnect: true
|
|
69
|
-
});
|
|
70
|
-
} else {
|
|
71
|
-
connection = new ioredis.Redis({
|
|
72
|
-
reconnectOnError,
|
|
73
|
-
...redisOptions,
|
|
74
|
-
lazyConnect: true
|
|
75
|
-
} as RedisOptions);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
await connection.connect();
|
|
79
|
-
|
|
80
|
-
logger.info("Connected to redis database...");
|
|
81
|
-
|
|
82
|
-
if (configuration.get("cache") && cache) {
|
|
83
|
-
configuration.set("cache.redisInstance", connection);
|
|
84
|
-
configuration.set("cache.store", ioRedisStore);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
(connection as any).name = name;
|
|
88
|
-
|
|
89
|
-
return connection;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return {name};
|
|
93
|
-
},
|
|
94
|
-
hooks: {
|
|
95
|
-
$onDestroy(connection: Redis) {
|
|
96
|
-
return connection.disconnect && connection.disconnect();
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
});
|
|
28
|
+
return item ? createConnection({...item, name}) : ({name} as any);
|
|
29
|
+
})
|
|
30
|
+
.token();
|
|
100
31
|
}
|