@edirect/redis 11.0.7 → 11.0.9

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.
Files changed (50) hide show
  1. package/dist/cjs/README.md +13 -0
  2. package/dist/cjs/package.json +3 -0
  3. package/dist/cjs/src/constants.js +5 -0
  4. package/dist/cjs/src/index.js +16 -0
  5. package/dist/cjs/src/redis.client.module.js +25 -0
  6. package/dist/cjs/src/redis.interface.js +2 -0
  7. package/dist/cjs/src/redis.module.js +17 -0
  8. package/dist/cjs/src/redis.providers.js +62 -0
  9. package/dist/cjs/src/redis.service.js +130 -0
  10. package/dist/cjs/types/src/constants.d.ts +3 -0
  11. package/dist/cjs/types/src/constants.d.ts.map +1 -0
  12. package/dist/cjs/types/src/index.d.ts +8 -0
  13. package/dist/cjs/types/src/index.d.ts.map +1 -0
  14. package/dist/cjs/types/src/redis.client.module.d.ts +6 -0
  15. package/dist/cjs/types/src/redis.client.module.d.ts.map +1 -0
  16. package/dist/cjs/types/src/redis.interface.d.ts +14 -0
  17. package/dist/cjs/types/src/redis.interface.d.ts.map +1 -0
  18. package/dist/cjs/types/src/redis.module.d.ts +3 -0
  19. package/dist/cjs/types/src/redis.module.d.ts.map +1 -0
  20. package/dist/cjs/types/src/redis.providers.d.ts +6 -0
  21. package/dist/cjs/types/src/redis.providers.d.ts.map +1 -0
  22. package/dist/cjs/types/src/redis.service.d.ts +22 -0
  23. package/dist/cjs/types/src/redis.service.d.ts.map +1 -0
  24. package/dist/esm/README.md +13 -0
  25. package/dist/esm/package.json +3 -0
  26. package/dist/esm/src/constants.js +2 -0
  27. package/dist/esm/src/index.js +5 -0
  28. package/dist/esm/src/redis.client.module.js +22 -0
  29. package/dist/esm/src/redis.interface.js +1 -0
  30. package/dist/esm/src/redis.module.js +14 -0
  31. package/dist/esm/src/redis.providers.js +57 -0
  32. package/dist/esm/src/redis.service.js +127 -0
  33. package/dist/esm/types/src/constants.d.ts +3 -0
  34. package/dist/esm/types/src/constants.d.ts.map +1 -0
  35. package/dist/esm/types/src/index.d.ts +8 -0
  36. package/dist/esm/types/src/index.d.ts.map +1 -0
  37. package/dist/esm/types/src/redis.client.module.d.ts +6 -0
  38. package/dist/esm/types/src/redis.client.module.d.ts.map +1 -0
  39. package/dist/esm/types/src/redis.interface.d.ts +14 -0
  40. package/dist/esm/types/src/redis.interface.d.ts.map +1 -0
  41. package/dist/esm/types/src/redis.module.d.ts +3 -0
  42. package/dist/esm/types/src/redis.module.d.ts.map +1 -0
  43. package/dist/esm/types/src/redis.providers.d.ts +6 -0
  44. package/dist/esm/types/src/redis.providers.d.ts.map +1 -0
  45. package/dist/esm/types/src/redis.service.d.ts +22 -0
  46. package/dist/esm/types/src/redis.service.d.ts.map +1 -0
  47. package/dist/package.json +7 -7
  48. package/dist/src/redis.providers.js +14 -21
  49. package/dist/src/redis.service.js +113 -98
  50. package/package.json +53 -15
@@ -0,0 +1,13 @@
1
+ # @edirect/redis
2
+
3
+ The EDirectInsure Redis Module.
4
+
5
+ ## Installation
6
+
7
+ ```shell
8
+ $ npm i --save @edirect/redis
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Import RedisModule on AppModule (app.module.ts)
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.REDIS_SERVICE_KEY = exports.REDIS_CLIENT_KEY = void 0;
4
+ exports.REDIS_CLIENT_KEY = 'REDIS_CLIENT';
5
+ exports.REDIS_SERVICE_KEY = 'REDIS_SERVICE';
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RedisService = exports.RedisProviders = exports.RedisClientProvider = exports.createRedisClientProvider = exports.RedisModule = exports.RedisClientModule = exports.REDIS_SERVICE_KEY = exports.REDIS_CLIENT_KEY = void 0;
4
+ var constants_1 = require("./constants");
5
+ Object.defineProperty(exports, "REDIS_CLIENT_KEY", { enumerable: true, get: function () { return constants_1.REDIS_CLIENT_KEY; } });
6
+ Object.defineProperty(exports, "REDIS_SERVICE_KEY", { enumerable: true, get: function () { return constants_1.REDIS_SERVICE_KEY; } });
7
+ var redis_client_module_1 = require("./redis.client.module");
8
+ Object.defineProperty(exports, "RedisClientModule", { enumerable: true, get: function () { return redis_client_module_1.RedisClientModule; } });
9
+ var redis_module_1 = require("./redis.module");
10
+ Object.defineProperty(exports, "RedisModule", { enumerable: true, get: function () { return redis_module_1.RedisModule; } });
11
+ var redis_providers_1 = require("./redis.providers");
12
+ Object.defineProperty(exports, "createRedisClientProvider", { enumerable: true, get: function () { return redis_providers_1.createRedisClientProvider; } });
13
+ Object.defineProperty(exports, "RedisClientProvider", { enumerable: true, get: function () { return redis_providers_1.RedisClientProvider; } });
14
+ Object.defineProperty(exports, "RedisProviders", { enumerable: true, get: function () { return redis_providers_1.RedisProviders; } });
15
+ var redis_service_1 = require("./redis.service");
16
+ Object.defineProperty(exports, "RedisService", { enumerable: true, get: function () { return redis_service_1.RedisService; } });
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var RedisClientModule_1;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.RedisClientModule = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const common_1 = require("@nestjs/common");
7
+ const redis_providers_1 = require("./redis.providers");
8
+ let RedisClientModule = RedisClientModule_1 = class RedisClientModule {
9
+ static forRoot(options, provide) {
10
+ const redisProviders = (0, redis_providers_1.RedisProviders)(provide);
11
+ return {
12
+ module: RedisClientModule_1,
13
+ providers: [
14
+ (0, redis_providers_1.createRedisClientProvider)(options),
15
+ ...redisProviders
16
+ ],
17
+ exports: [...redisProviders],
18
+ };
19
+ }
20
+ };
21
+ exports.RedisClientModule = RedisClientModule;
22
+ exports.RedisClientModule = RedisClientModule = RedisClientModule_1 = tslib_1.__decorate([
23
+ (0, common_1.Global)(),
24
+ (0, common_1.Module)({})
25
+ ], RedisClientModule);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RedisModule = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@nestjs/common");
6
+ const redis_providers_1 = require("./redis.providers");
7
+ const providers = (0, redis_providers_1.RedisProviders)();
8
+ let RedisModule = class RedisModule {
9
+ };
10
+ exports.RedisModule = RedisModule;
11
+ exports.RedisModule = RedisModule = tslib_1.__decorate([
12
+ (0, common_1.Global)(),
13
+ (0, common_1.Module)({
14
+ providers: [redis_providers_1.RedisClientProvider, ...providers],
15
+ exports: [...providers],
16
+ })
17
+ ], RedisModule);
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RedisProviders = exports.RedisClientProvider = exports.createRedisClientProvider = void 0;
4
+ const config_1 = require("@edirect/config");
5
+ const redis_1 = require("redis");
6
+ const constants_1 = require("./constants");
7
+ const redis_service_1 = require("./redis.service");
8
+ const logger_1 = require("@edirect/logger");
9
+ const defaultOptions = (configService, loggerService) => ({
10
+ url: `redis://${configService.get('REDIS_HOST')}:${configService.get('REDIS_PORT')}`,
11
+ ...((configService.get('REDIS_PASS') ||
12
+ configService.get('REDIS_PASSWORD')) && {
13
+ password: configService.get('REDIS_PASS') ||
14
+ configService.get('REDIS_PASSWORD'),
15
+ }),
16
+ pingInterval: 1000,
17
+ socket: {
18
+ connectTimeout: 10000,
19
+ keepAlive: true,
20
+ noDelay: true,
21
+ reconnectStrategy(retries, cause) {
22
+ if (cause) {
23
+ loggerService.error('Error connecting to redis', JSON.stringify(cause));
24
+ }
25
+ return Math.min(retries * 50, 500);
26
+ },
27
+ },
28
+ });
29
+ const createRedisClientProvider = (options) => ({
30
+ provide: constants_1.REDIS_CLIENT_KEY,
31
+ useFactory: async (configService, loggerService) => {
32
+ const redisClient = (0, redis_1.createClient)({
33
+ ...defaultOptions(configService, loggerService),
34
+ ...options,
35
+ });
36
+ await redisClient.connect();
37
+ return redisClient;
38
+ },
39
+ inject: [config_1.ConfigService, logger_1.LoggerService],
40
+ });
41
+ exports.createRedisClientProvider = createRedisClientProvider;
42
+ exports.RedisClientProvider = {
43
+ provide: constants_1.REDIS_CLIENT_KEY,
44
+ useFactory: async (configService, loggerService) => {
45
+ const redisClient = (0, redis_1.createClient)(defaultOptions(configService, loggerService));
46
+ await redisClient.connect();
47
+ return redisClient;
48
+ },
49
+ inject: [config_1.ConfigService, logger_1.LoggerService],
50
+ };
51
+ const RedisProviders = (provide) => [
52
+ {
53
+ provide: provide || constants_1.REDIS_SERVICE_KEY,
54
+ useFactory: (redisClient, configService, loggerService) => new redis_service_1.RedisService(redisClient, configService, loggerService),
55
+ inject: [constants_1.REDIS_CLIENT_KEY, config_1.ConfigService, logger_1.LoggerService],
56
+ },
57
+ {
58
+ provide: redis_service_1.RedisService,
59
+ useExisting: provide || constants_1.REDIS_SERVICE_KEY,
60
+ },
61
+ ];
62
+ exports.RedisProviders = RedisProviders;
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RedisService = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@nestjs/common");
6
+ const config_1 = require("@edirect/config");
7
+ const logger_1 = require("@edirect/logger");
8
+ const constants_1 = require("./constants");
9
+ let RedisService = class RedisService {
10
+ redisClient;
11
+ configService;
12
+ loggerService;
13
+ constructor(redisClient, configService, loggerService) {
14
+ this.redisClient = redisClient;
15
+ this.configService = configService;
16
+ this.loggerService = loggerService;
17
+ }
18
+ async get(key) {
19
+ try {
20
+ const ret = await this.redisClient.get(key);
21
+ return ret ? JSON.parse(ret) : null;
22
+ }
23
+ catch (error) {
24
+ this.loggerService.error('Error when executing GET command in redis', JSON.stringify(error));
25
+ throw error;
26
+ }
27
+ }
28
+ async mget(keys) {
29
+ try {
30
+ const ret = await this.redisClient.mGet(keys);
31
+ return ret ? ret.map((item) => (item ? JSON.parse(item) : null)) : [];
32
+ }
33
+ catch (error) {
34
+ this.loggerService.error('Error when executing MGET command in redis', JSON.stringify(error));
35
+ throw error;
36
+ }
37
+ }
38
+ async hget(hash, field) {
39
+ try {
40
+ const ret = await this.redisClient.hGet(hash, field);
41
+ return ret ? JSON.parse(ret) : null;
42
+ }
43
+ catch (error) {
44
+ this.loggerService.error('Error when executing HGET command in redis', JSON.stringify(error));
45
+ throw error;
46
+ }
47
+ }
48
+ async hgetall(hash) {
49
+ try {
50
+ const ret = await this.redisClient.hGetAll(hash);
51
+ const result = {};
52
+ for (const [key, value] of Object.entries(ret)) {
53
+ result[key] = value ? JSON.parse(value) : null;
54
+ }
55
+ return result;
56
+ }
57
+ catch (error) {
58
+ this.loggerService.error('Error when executing HGETALL command in redis', JSON.stringify(error));
59
+ throw error;
60
+ }
61
+ }
62
+ async set(key, data, ttl, ttlType = 'EX') {
63
+ try {
64
+ if (!ttl)
65
+ ttl = parseInt(this.configService.get('REDIS_TTL') || '0', 10);
66
+ if (data instanceof Object)
67
+ data = JSON.stringify(data);
68
+ if (ttl > 0) {
69
+ await this.redisClient.set(key, data, {
70
+ expiration: {
71
+ type: ttlType,
72
+ value: ttl,
73
+ },
74
+ });
75
+ }
76
+ else {
77
+ await this.redisClient.set(key, data);
78
+ }
79
+ }
80
+ catch (error) {
81
+ this.loggerService.error(`Error when executing SET${ttlType === 'EX' ? '' : ttlType} command in redis`, JSON.stringify(error));
82
+ throw error;
83
+ }
84
+ }
85
+ async del(key) {
86
+ try {
87
+ await this.redisClient.del(key);
88
+ }
89
+ catch (error) {
90
+ this.loggerService.error('Error when executing DEL command in redis', JSON.stringify(error));
91
+ throw error;
92
+ }
93
+ }
94
+ async setnx(key, data) {
95
+ try {
96
+ if (data instanceof Object)
97
+ data = JSON.stringify(data);
98
+ return (await this.redisClient.setNX(key, data)) === 1;
99
+ }
100
+ catch (error) {
101
+ this.loggerService.error('Error when executing SETNX command in redis', JSON.stringify(error));
102
+ throw error;
103
+ }
104
+ }
105
+ async asyncPing() {
106
+ return (await this.redisClient.ping()) === 'PONG';
107
+ }
108
+ async keys(key) {
109
+ try {
110
+ const ret = await this.redisClient.keys(key);
111
+ if (Array.isArray(ret)) {
112
+ return ret.map((item) => (item ? JSON.parse(item) : null));
113
+ }
114
+ return ret ? JSON.parse(ret) : [];
115
+ }
116
+ catch (error) {
117
+ this.loggerService.error('Error when executing KEYS command in redis', JSON.stringify(error));
118
+ throw error;
119
+ }
120
+ }
121
+ };
122
+ exports.RedisService = RedisService;
123
+ exports.RedisService = RedisService = tslib_1.__decorate([
124
+ (0, common_1.Injectable)(),
125
+ tslib_1.__param(0, (0, common_1.Inject)(constants_1.REDIS_CLIENT_KEY)),
126
+ tslib_1.__param(1, (0, common_1.Inject)(config_1.ConfigService)),
127
+ tslib_1.__param(2, (0, common_1.Inject)(logger_1.LoggerService)),
128
+ tslib_1.__metadata("design:paramtypes", [Object, config_1.ConfigService,
129
+ logger_1.LoggerService])
130
+ ], RedisService);
@@ -0,0 +1,3 @@
1
+ export declare const REDIS_CLIENT_KEY = "REDIS_CLIENT";
2
+ export declare const REDIS_SERVICE_KEY = "REDIS_SERVICE";
3
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,iBAAiB,CAAC;AAC/C,eAAO,MAAM,iBAAiB,kBAAkB,CAAC"}
@@ -0,0 +1,8 @@
1
+ export { REDIS_CLIENT_KEY, REDIS_SERVICE_KEY } from './constants';
2
+ export { RedisClientModule } from './redis.client.module';
3
+ export type { RedisServiceInterface } from './redis.interface';
4
+ export { RedisModule } from './redis.module';
5
+ export { createRedisClientProvider, RedisClientProvider, RedisProviders, } from './redis.providers';
6
+ export { RedisService } from './redis.service';
7
+ export type { createClient, RedisClientType as RedisClient, RedisClientType, RedisClientOptions as ClientOpts, RedisClientOptions, } from 'redis';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,YAAY,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EACL,yBAAyB,EACzB,mBAAmB,EACnB,cAAc,GACf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,YAAY,EACV,YAAY,EACZ,eAAe,IAAI,WAAW,EAC9B,eAAe,EACf,kBAAkB,IAAI,UAAU,EAChC,kBAAkB,GACnB,MAAM,OAAO,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { RedisClientOptions } from 'redis';
3
+ export declare class RedisClientModule {
4
+ static forRoot(options?: RedisClientOptions, provide?: string): DynamicModule;
5
+ }
6
+ //# sourceMappingURL=redis.client.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.client.module.d.ts","sourceRoot":"","sources":["../../../../src/redis.client.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAA4B,MAAM,gBAAgB,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAO3C,qBAEa,iBAAiB;IAC5B,MAAM,CAAC,OAAO,CACZ,OAAO,CAAC,EAAE,kBAAkB,EAC5B,OAAO,CAAC,EAAE,MAAM,GACf,aAAa;CAYjB"}
@@ -0,0 +1,14 @@
1
+ export interface RedisServiceInterface {
2
+ get(key: string): Promise<string | object | null>;
3
+ mget(key: string[]): Promise<string[] | object[]>;
4
+ hget(hash: string, field: string): Promise<string | null>;
5
+ hgetall(hash: string): Promise<{
6
+ [key: string]: string | null;
7
+ }>;
8
+ set(key: string, data: object | string, ttl?: number): Promise<void>;
9
+ del(key: string): Promise<void>;
10
+ setnx(key: string, data: object | string): Promise<boolean>;
11
+ asyncPing(): Promise<boolean>;
12
+ keys(key: string): Promise<string | string[]>;
13
+ }
14
+ //# sourceMappingURL=redis.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.interface.d.ts","sourceRoot":"","sources":["../../../../src/redis.interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC,CAAC;IAElD,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;IAElD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAE1D,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC,CAAC;IAEjE,GAAG,CACD,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEhC,KAAK,CACH,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,MAAM,GACpB,OAAO,CAAC,OAAO,CAAC,CAAC;IAEpB,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9B,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;CAC/C"}
@@ -0,0 +1,3 @@
1
+ export declare class RedisModule {
2
+ }
3
+ //# sourceMappingURL=redis.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.module.d.ts","sourceRoot":"","sources":["../../../../src/redis.module.ts"],"names":[],"mappings":"AAMA,qBAKa,WAAW;CAAI"}
@@ -0,0 +1,6 @@
1
+ import { Provider } from '@nestjs/common';
2
+ import { RedisClientOptions } from 'redis';
3
+ export declare const createRedisClientProvider: (options?: RedisClientOptions) => Provider;
4
+ export declare const RedisClientProvider: Provider;
5
+ export declare const RedisProviders: (provide?: string) => Provider[];
6
+ //# sourceMappingURL=redis.providers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.providers.d.ts","sourceRoot":"","sources":["../../../../src/redis.providers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAAgB,kBAAkB,EAAmB,MAAM,OAAO,CAAC;AAiC1E,eAAO,MAAM,yBAAyB,EAAE,CACtC,OAAO,CAAC,EAAE,kBAAkB,KACzB,QAWH,CAAC;AAEH,eAAO,MAAM,mBAAmB,EAAE,QAQjC,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,CAAC,OAAO,CAAC,EAAE,MAAM,KAAK,QAAQ,EAa1D,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { ConfigService } from '@edirect/config';
2
+ import { LoggerService } from '@edirect/logger';
3
+ import { RedisServiceInterface } from './redis.interface';
4
+ import type { RedisClientType } from 'redis';
5
+ export declare class RedisService implements RedisServiceInterface {
6
+ private readonly redisClient;
7
+ private readonly configService;
8
+ private readonly loggerService;
9
+ constructor(redisClient: RedisClientType, configService: ConfigService, loggerService: LoggerService);
10
+ get(key: string): Promise<string | object | null>;
11
+ mget(keys: string[]): Promise<string[] | object[]>;
12
+ hget(hash: string, field: string): Promise<string | null>;
13
+ hgetall(hash: string): Promise<{
14
+ [key: string]: string;
15
+ }>;
16
+ set(key: string, data: object | string, ttl?: number, ttlType?: 'EX' | 'PX' | 'EXAT' | 'PXAT'): Promise<void>;
17
+ del(key: string): Promise<void>;
18
+ setnx(key: string, data: object | string): Promise<boolean>;
19
+ asyncPing(): Promise<boolean>;
20
+ keys(key: string): Promise<string | string[]>;
21
+ }
22
+ //# sourceMappingURL=redis.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.service.d.ts","sourceRoot":"","sources":["../../../../src/redis.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAE7C,qBACa,YAAa,YAAW,qBAAqB;IAGtD,OAAO,CAAC,QAAQ,CAAC,WAAW;IAE5B,OAAO,CAAC,QAAQ,CAAC,aAAa;IAE9B,OAAO,CAAC,QAAQ,CAAC,aAAa;gBAJb,WAAW,EAAE,eAAe,EAE5B,aAAa,EAAE,aAAa,EAE5B,aAAa,EAAE,aAAa;IAGlC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAajD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;IAalD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAazD,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IAiBzD,GAAG,CACd,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,GAAG,CAAC,EAAE,MAAM,EACZ,OAAO,GAAE,IAAI,GAAG,IAAI,GAAG,MAAM,GAAG,MAAa,GAC5C,OAAO,CAAC,IAAI,CAAC;IA2BH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAY/B,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAa3D,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAI7B,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC;CAe3D"}
@@ -0,0 +1,13 @@
1
+ # @edirect/redis
2
+
3
+ The EDirectInsure Redis Module.
4
+
5
+ ## Installation
6
+
7
+ ```shell
8
+ $ npm i --save @edirect/redis
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Import RedisModule on AppModule (app.module.ts)
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,2 @@
1
+ export const REDIS_CLIENT_KEY = 'REDIS_CLIENT';
2
+ export const REDIS_SERVICE_KEY = 'REDIS_SERVICE';
@@ -0,0 +1,5 @@
1
+ export { REDIS_CLIENT_KEY, REDIS_SERVICE_KEY } from './constants';
2
+ export { RedisClientModule } from './redis.client.module';
3
+ export { RedisModule } from './redis.module';
4
+ export { createRedisClientProvider, RedisClientProvider, RedisProviders, } from './redis.providers';
5
+ export { RedisService } from './redis.service';
@@ -0,0 +1,22 @@
1
+ var RedisClientModule_1;
2
+ import { __decorate } from "tslib";
3
+ import { Global, Module } from '@nestjs/common';
4
+ import { createRedisClientProvider, RedisProviders } from './redis.providers';
5
+ let RedisClientModule = RedisClientModule_1 = class RedisClientModule {
6
+ static forRoot(options, provide) {
7
+ const redisProviders = RedisProviders(provide);
8
+ return {
9
+ module: RedisClientModule_1,
10
+ providers: [
11
+ createRedisClientProvider(options),
12
+ ...redisProviders
13
+ ],
14
+ exports: [...redisProviders],
15
+ };
16
+ }
17
+ };
18
+ RedisClientModule = RedisClientModule_1 = __decorate([
19
+ Global(),
20
+ Module({})
21
+ ], RedisClientModule);
22
+ export { RedisClientModule };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,14 @@
1
+ import { __decorate } from "tslib";
2
+ import { Global, Module } from '@nestjs/common';
3
+ import { RedisClientProvider, RedisProviders } from './redis.providers';
4
+ const providers = RedisProviders();
5
+ let RedisModule = class RedisModule {
6
+ };
7
+ RedisModule = __decorate([
8
+ Global(),
9
+ Module({
10
+ providers: [RedisClientProvider, ...providers],
11
+ exports: [...providers],
12
+ })
13
+ ], RedisModule);
14
+ export { RedisModule };
@@ -0,0 +1,57 @@
1
+ import { ConfigService } from '@edirect/config';
2
+ import { createClient } from 'redis';
3
+ import { REDIS_CLIENT_KEY, REDIS_SERVICE_KEY } from './constants';
4
+ import { RedisService } from './redis.service';
5
+ import { LoggerService } from '@edirect/logger';
6
+ const defaultOptions = (configService, loggerService) => ({
7
+ url: `redis://${configService.get('REDIS_HOST')}:${configService.get('REDIS_PORT')}`,
8
+ ...((configService.get('REDIS_PASS') ||
9
+ configService.get('REDIS_PASSWORD')) && {
10
+ password: configService.get('REDIS_PASS') ||
11
+ configService.get('REDIS_PASSWORD'),
12
+ }),
13
+ pingInterval: 1000,
14
+ socket: {
15
+ connectTimeout: 10000,
16
+ keepAlive: true,
17
+ noDelay: true,
18
+ reconnectStrategy(retries, cause) {
19
+ if (cause) {
20
+ loggerService.error('Error connecting to redis', JSON.stringify(cause));
21
+ }
22
+ return Math.min(retries * 50, 500);
23
+ },
24
+ },
25
+ });
26
+ export const createRedisClientProvider = (options) => ({
27
+ provide: REDIS_CLIENT_KEY,
28
+ useFactory: async (configService, loggerService) => {
29
+ const redisClient = createClient({
30
+ ...defaultOptions(configService, loggerService),
31
+ ...options,
32
+ });
33
+ await redisClient.connect();
34
+ return redisClient;
35
+ },
36
+ inject: [ConfigService, LoggerService],
37
+ });
38
+ export const RedisClientProvider = {
39
+ provide: REDIS_CLIENT_KEY,
40
+ useFactory: async (configService, loggerService) => {
41
+ const redisClient = createClient(defaultOptions(configService, loggerService));
42
+ await redisClient.connect();
43
+ return redisClient;
44
+ },
45
+ inject: [ConfigService, LoggerService],
46
+ };
47
+ export const RedisProviders = (provide) => [
48
+ {
49
+ provide: provide || REDIS_SERVICE_KEY,
50
+ useFactory: (redisClient, configService, loggerService) => new RedisService(redisClient, configService, loggerService),
51
+ inject: [REDIS_CLIENT_KEY, ConfigService, LoggerService],
52
+ },
53
+ {
54
+ provide: RedisService,
55
+ useExisting: provide || REDIS_SERVICE_KEY,
56
+ },
57
+ ];
@@ -0,0 +1,127 @@
1
+ import { __decorate, __metadata, __param } from "tslib";
2
+ import { Injectable, Inject } from '@nestjs/common';
3
+ import { ConfigService } from '@edirect/config';
4
+ import { LoggerService } from '@edirect/logger';
5
+ import { REDIS_CLIENT_KEY } from './constants';
6
+ let RedisService = class RedisService {
7
+ redisClient;
8
+ configService;
9
+ loggerService;
10
+ constructor(redisClient, configService, loggerService) {
11
+ this.redisClient = redisClient;
12
+ this.configService = configService;
13
+ this.loggerService = loggerService;
14
+ }
15
+ async get(key) {
16
+ try {
17
+ const ret = await this.redisClient.get(key);
18
+ return ret ? JSON.parse(ret) : null;
19
+ }
20
+ catch (error) {
21
+ this.loggerService.error('Error when executing GET command in redis', JSON.stringify(error));
22
+ throw error;
23
+ }
24
+ }
25
+ async mget(keys) {
26
+ try {
27
+ const ret = await this.redisClient.mGet(keys);
28
+ return ret ? ret.map((item) => (item ? JSON.parse(item) : null)) : [];
29
+ }
30
+ catch (error) {
31
+ this.loggerService.error('Error when executing MGET command in redis', JSON.stringify(error));
32
+ throw error;
33
+ }
34
+ }
35
+ async hget(hash, field) {
36
+ try {
37
+ const ret = await this.redisClient.hGet(hash, field);
38
+ return ret ? JSON.parse(ret) : null;
39
+ }
40
+ catch (error) {
41
+ this.loggerService.error('Error when executing HGET command in redis', JSON.stringify(error));
42
+ throw error;
43
+ }
44
+ }
45
+ async hgetall(hash) {
46
+ try {
47
+ const ret = await this.redisClient.hGetAll(hash);
48
+ const result = {};
49
+ for (const [key, value] of Object.entries(ret)) {
50
+ result[key] = value ? JSON.parse(value) : null;
51
+ }
52
+ return result;
53
+ }
54
+ catch (error) {
55
+ this.loggerService.error('Error when executing HGETALL command in redis', JSON.stringify(error));
56
+ throw error;
57
+ }
58
+ }
59
+ async set(key, data, ttl, ttlType = 'EX') {
60
+ try {
61
+ if (!ttl)
62
+ ttl = parseInt(this.configService.get('REDIS_TTL') || '0', 10);
63
+ if (data instanceof Object)
64
+ data = JSON.stringify(data);
65
+ if (ttl > 0) {
66
+ await this.redisClient.set(key, data, {
67
+ expiration: {
68
+ type: ttlType,
69
+ value: ttl,
70
+ },
71
+ });
72
+ }
73
+ else {
74
+ await this.redisClient.set(key, data);
75
+ }
76
+ }
77
+ catch (error) {
78
+ this.loggerService.error(`Error when executing SET${ttlType === 'EX' ? '' : ttlType} command in redis`, JSON.stringify(error));
79
+ throw error;
80
+ }
81
+ }
82
+ async del(key) {
83
+ try {
84
+ await this.redisClient.del(key);
85
+ }
86
+ catch (error) {
87
+ this.loggerService.error('Error when executing DEL command in redis', JSON.stringify(error));
88
+ throw error;
89
+ }
90
+ }
91
+ async setnx(key, data) {
92
+ try {
93
+ if (data instanceof Object)
94
+ data = JSON.stringify(data);
95
+ return (await this.redisClient.setNX(key, data)) === 1;
96
+ }
97
+ catch (error) {
98
+ this.loggerService.error('Error when executing SETNX command in redis', JSON.stringify(error));
99
+ throw error;
100
+ }
101
+ }
102
+ async asyncPing() {
103
+ return (await this.redisClient.ping()) === 'PONG';
104
+ }
105
+ async keys(key) {
106
+ try {
107
+ const ret = await this.redisClient.keys(key);
108
+ if (Array.isArray(ret)) {
109
+ return ret.map((item) => (item ? JSON.parse(item) : null));
110
+ }
111
+ return ret ? JSON.parse(ret) : [];
112
+ }
113
+ catch (error) {
114
+ this.loggerService.error('Error when executing KEYS command in redis', JSON.stringify(error));
115
+ throw error;
116
+ }
117
+ }
118
+ };
119
+ RedisService = __decorate([
120
+ Injectable(),
121
+ __param(0, Inject(REDIS_CLIENT_KEY)),
122
+ __param(1, Inject(ConfigService)),
123
+ __param(2, Inject(LoggerService)),
124
+ __metadata("design:paramtypes", [Object, ConfigService,
125
+ LoggerService])
126
+ ], RedisService);
127
+ export { RedisService };
@@ -0,0 +1,3 @@
1
+ export declare const REDIS_CLIENT_KEY = "REDIS_CLIENT";
2
+ export declare const REDIS_SERVICE_KEY = "REDIS_SERVICE";
3
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,iBAAiB,CAAC;AAC/C,eAAO,MAAM,iBAAiB,kBAAkB,CAAC"}
@@ -0,0 +1,8 @@
1
+ export { REDIS_CLIENT_KEY, REDIS_SERVICE_KEY } from './constants';
2
+ export { RedisClientModule } from './redis.client.module';
3
+ export type { RedisServiceInterface } from './redis.interface';
4
+ export { RedisModule } from './redis.module';
5
+ export { createRedisClientProvider, RedisClientProvider, RedisProviders, } from './redis.providers';
6
+ export { RedisService } from './redis.service';
7
+ export type { createClient, RedisClientType as RedisClient, RedisClientType, RedisClientOptions as ClientOpts, RedisClientOptions, } from 'redis';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,YAAY,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EACL,yBAAyB,EACzB,mBAAmB,EACnB,cAAc,GACf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,YAAY,EACV,YAAY,EACZ,eAAe,IAAI,WAAW,EAC9B,eAAe,EACf,kBAAkB,IAAI,UAAU,EAChC,kBAAkB,GACnB,MAAM,OAAO,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { RedisClientOptions } from 'redis';
3
+ export declare class RedisClientModule {
4
+ static forRoot(options?: RedisClientOptions, provide?: string): DynamicModule;
5
+ }
6
+ //# sourceMappingURL=redis.client.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.client.module.d.ts","sourceRoot":"","sources":["../../../../src/redis.client.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAA4B,MAAM,gBAAgB,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAO3C,qBAEa,iBAAiB;IAC5B,MAAM,CAAC,OAAO,CACZ,OAAO,CAAC,EAAE,kBAAkB,EAC5B,OAAO,CAAC,EAAE,MAAM,GACf,aAAa;CAYjB"}
@@ -0,0 +1,14 @@
1
+ export interface RedisServiceInterface {
2
+ get(key: string): Promise<string | object | null>;
3
+ mget(key: string[]): Promise<string[] | object[]>;
4
+ hget(hash: string, field: string): Promise<string | null>;
5
+ hgetall(hash: string): Promise<{
6
+ [key: string]: string | null;
7
+ }>;
8
+ set(key: string, data: object | string, ttl?: number): Promise<void>;
9
+ del(key: string): Promise<void>;
10
+ setnx(key: string, data: object | string): Promise<boolean>;
11
+ asyncPing(): Promise<boolean>;
12
+ keys(key: string): Promise<string | string[]>;
13
+ }
14
+ //# sourceMappingURL=redis.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.interface.d.ts","sourceRoot":"","sources":["../../../../src/redis.interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,qBAAqB;IACpC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC,CAAC;IAElD,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;IAElD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAE1D,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC,CAAC;IAEjE,GAAG,CACD,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEhC,KAAK,CACH,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,MAAM,GACpB,OAAO,CAAC,OAAO,CAAC,CAAC;IAEpB,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAE9B,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;CAC/C"}
@@ -0,0 +1,3 @@
1
+ export declare class RedisModule {
2
+ }
3
+ //# sourceMappingURL=redis.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.module.d.ts","sourceRoot":"","sources":["../../../../src/redis.module.ts"],"names":[],"mappings":"AAMA,qBAKa,WAAW;CAAI"}
@@ -0,0 +1,6 @@
1
+ import { Provider } from '@nestjs/common';
2
+ import { RedisClientOptions } from 'redis';
3
+ export declare const createRedisClientProvider: (options?: RedisClientOptions) => Provider;
4
+ export declare const RedisClientProvider: Provider;
5
+ export declare const RedisProviders: (provide?: string) => Provider[];
6
+ //# sourceMappingURL=redis.providers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.providers.d.ts","sourceRoot":"","sources":["../../../../src/redis.providers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAAgB,kBAAkB,EAAmB,MAAM,OAAO,CAAC;AAiC1E,eAAO,MAAM,yBAAyB,EAAE,CACtC,OAAO,CAAC,EAAE,kBAAkB,KACzB,QAWH,CAAC;AAEH,eAAO,MAAM,mBAAmB,EAAE,QAQjC,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,CAAC,OAAO,CAAC,EAAE,MAAM,KAAK,QAAQ,EAa1D,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { ConfigService } from '@edirect/config';
2
+ import { LoggerService } from '@edirect/logger';
3
+ import { RedisServiceInterface } from './redis.interface';
4
+ import type { RedisClientType } from 'redis';
5
+ export declare class RedisService implements RedisServiceInterface {
6
+ private readonly redisClient;
7
+ private readonly configService;
8
+ private readonly loggerService;
9
+ constructor(redisClient: RedisClientType, configService: ConfigService, loggerService: LoggerService);
10
+ get(key: string): Promise<string | object | null>;
11
+ mget(keys: string[]): Promise<string[] | object[]>;
12
+ hget(hash: string, field: string): Promise<string | null>;
13
+ hgetall(hash: string): Promise<{
14
+ [key: string]: string;
15
+ }>;
16
+ set(key: string, data: object | string, ttl?: number, ttlType?: 'EX' | 'PX' | 'EXAT' | 'PXAT'): Promise<void>;
17
+ del(key: string): Promise<void>;
18
+ setnx(key: string, data: object | string): Promise<boolean>;
19
+ asyncPing(): Promise<boolean>;
20
+ keys(key: string): Promise<string | string[]>;
21
+ }
22
+ //# sourceMappingURL=redis.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redis.service.d.ts","sourceRoot":"","sources":["../../../../src/redis.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAE7C,qBACa,YAAa,YAAW,qBAAqB;IAGtD,OAAO,CAAC,QAAQ,CAAC,WAAW;IAE5B,OAAO,CAAC,QAAQ,CAAC,aAAa;IAE9B,OAAO,CAAC,QAAQ,CAAC,aAAa;gBAJb,WAAW,EAAE,eAAe,EAE5B,aAAa,EAAE,aAAa,EAE5B,aAAa,EAAE,aAAa;IAGlC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAajD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;IAalD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAazD,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IAiBzD,GAAG,CACd,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,GAAG,CAAC,EAAE,MAAM,EACZ,OAAO,GAAE,IAAI,GAAG,IAAI,GAAG,MAAM,GAAG,MAAa,GAC5C,OAAO,CAAC,IAAI,CAAC;IA2BH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAY/B,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAa3D,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAI7B,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,MAAM,EAAE,CAAC;CAe3D"}
package/dist/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@edirect/redis",
3
- "version": "11.0.6",
3
+ "version": "11.0.8",
4
4
  "packageScope": "@edirect",
5
5
  "type": "module",
6
- "main": "./dist/index.esm.js",
7
- "module": "./dist/index.esm.js",
8
- "types": "./dist/index.esm.d.ts",
6
+ "main": "./dist/src/index.js",
7
+ "module": "./dist/src/index.js",
8
+ "types": "./dist/src/index.d.ts",
9
9
  "exports": {
10
10
  "./package.json": "./package.json",
11
11
  ".": {
12
12
  "development": "./src/index.ts",
13
- "types": "./dist/index.esm.d.ts",
14
- "import": "./dist/index.esm.js",
15
- "default": "./dist/index.esm.js"
13
+ "types": "./dist/src/index.d.ts",
14
+ "import": "./dist/src/index.js",
15
+ "default": "./dist/src/index.js"
16
16
  }
17
17
  },
18
18
  "files": [
@@ -1,17 +1,14 @@
1
+ import { __awaiter } from "tslib";
1
2
  import { ConfigService } from '@edirect/config';
2
3
  import { createClient } from 'redis';
3
4
  import { REDIS_CLIENT_KEY, REDIS_SERVICE_KEY } from './constants';
4
5
  import { RedisService } from './redis.service';
5
6
  import { LoggerService } from '@edirect/logger';
6
- const defaultOptions = (configService, loggerService) => ({
7
- url: `redis://${configService.get('REDIS_HOST')}:${configService.get('REDIS_PORT')}`,
8
- ...((configService.get('REDIS_PASS') ||
9
- configService.get('REDIS_PASSWORD')) && {
10
- password: configService.get('REDIS_PASS') ||
11
- configService.get('REDIS_PASSWORD'),
12
- }),
13
- pingInterval: 1000,
14
- socket: {
7
+ const defaultOptions = (configService, loggerService) => (Object.assign(Object.assign({ url: `redis://${configService.get('REDIS_HOST')}:${configService.get('REDIS_PORT')}` }, ((configService.get('REDIS_PASS') ||
8
+ configService.get('REDIS_PASSWORD')) && {
9
+ password: configService.get('REDIS_PASS') ||
10
+ configService.get('REDIS_PASSWORD'),
11
+ })), { pingInterval: 1000, socket: {
15
12
  connectTimeout: 10000,
16
13
  keepAlive: true,
17
14
  noDelay: true,
@@ -21,27 +18,23 @@ const defaultOptions = (configService, loggerService) => ({
21
18
  }
22
19
  return Math.min(retries * 50, 500);
23
20
  },
24
- },
25
- });
21
+ } }));
26
22
  export const createRedisClientProvider = (options) => ({
27
23
  provide: REDIS_CLIENT_KEY,
28
- useFactory: async (configService, loggerService) => {
29
- const redisClient = createClient({
30
- ...defaultOptions(configService, loggerService),
31
- ...options,
32
- });
33
- await redisClient.connect();
24
+ useFactory: (configService, loggerService) => __awaiter(void 0, void 0, void 0, function* () {
25
+ const redisClient = createClient(Object.assign(Object.assign({}, defaultOptions(configService, loggerService)), options));
26
+ yield redisClient.connect();
34
27
  return redisClient;
35
- },
28
+ }),
36
29
  inject: [ConfigService, LoggerService],
37
30
  });
38
31
  export const RedisClientProvider = {
39
32
  provide: REDIS_CLIENT_KEY,
40
- useFactory: async (configService, loggerService) => {
33
+ useFactory: (configService, loggerService) => __awaiter(void 0, void 0, void 0, function* () {
41
34
  const redisClient = createClient(defaultOptions(configService, loggerService));
42
- await redisClient.connect();
35
+ yield redisClient.connect();
43
36
  return redisClient;
44
- },
37
+ }),
45
38
  inject: [ConfigService, LoggerService],
46
39
  };
47
40
  export const RedisProviders = (provide) => [
@@ -1,119 +1,134 @@
1
- import { __decorate, __metadata, __param } from "tslib";
1
+ import { __awaiter, __decorate, __metadata, __param } from "tslib";
2
2
  import { Injectable, Inject } from '@nestjs/common';
3
3
  import { ConfigService } from '@edirect/config';
4
4
  import { LoggerService } from '@edirect/logger';
5
5
  import { REDIS_CLIENT_KEY } from './constants';
6
6
  let RedisService = class RedisService {
7
- redisClient;
8
- configService;
9
- loggerService;
10
7
  constructor(redisClient, configService, loggerService) {
11
8
  this.redisClient = redisClient;
12
9
  this.configService = configService;
13
10
  this.loggerService = loggerService;
14
11
  }
15
- async get(key) {
16
- try {
17
- const ret = await this.redisClient.get(key);
18
- return ret ? JSON.parse(ret) : null;
19
- }
20
- catch (error) {
21
- this.loggerService.error('Error when executing GET command in redis', JSON.stringify(error));
22
- throw error;
23
- }
12
+ get(key) {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ try {
15
+ const ret = yield this.redisClient.get(key);
16
+ return ret ? JSON.parse(ret) : null;
17
+ }
18
+ catch (error) {
19
+ this.loggerService.error('Error when executing GET command in redis', JSON.stringify(error));
20
+ throw error;
21
+ }
22
+ });
24
23
  }
25
- async mget(keys) {
26
- try {
27
- const ret = await this.redisClient.mGet(keys);
28
- return ret ? ret.map((item) => (item ? JSON.parse(item) : null)) : [];
29
- }
30
- catch (error) {
31
- this.loggerService.error('Error when executing MGET command in redis', JSON.stringify(error));
32
- throw error;
33
- }
24
+ mget(keys) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ try {
27
+ const ret = yield this.redisClient.mGet(keys);
28
+ return ret ? ret.map((item) => (item ? JSON.parse(item) : null)) : [];
29
+ }
30
+ catch (error) {
31
+ this.loggerService.error('Error when executing MGET command in redis', JSON.stringify(error));
32
+ throw error;
33
+ }
34
+ });
34
35
  }
35
- async hget(hash, field) {
36
- try {
37
- const ret = await this.redisClient.hGet(hash, field);
38
- return ret ? JSON.parse(ret) : null;
39
- }
40
- catch (error) {
41
- this.loggerService.error('Error when executing HGET command in redis', JSON.stringify(error));
42
- throw error;
43
- }
36
+ hget(hash, field) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ try {
39
+ const ret = yield this.redisClient.hGet(hash, field);
40
+ return ret ? JSON.parse(ret) : null;
41
+ }
42
+ catch (error) {
43
+ this.loggerService.error('Error when executing HGET command in redis', JSON.stringify(error));
44
+ throw error;
45
+ }
46
+ });
44
47
  }
45
- async hgetall(hash) {
46
- try {
47
- const ret = await this.redisClient.hGetAll(hash);
48
- const result = {};
49
- for (const [key, value] of Object.entries(ret)) {
50
- result[key] = value ? JSON.parse(value) : null;
51
- }
52
- return result;
53
- }
54
- catch (error) {
55
- this.loggerService.error('Error when executing HGETALL command in redis', JSON.stringify(error));
56
- throw error;
57
- }
48
+ hgetall(hash) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ try {
51
+ const ret = yield this.redisClient.hGetAll(hash);
52
+ const result = {};
53
+ for (const [key, value] of Object.entries(ret)) {
54
+ result[key] = value ? JSON.parse(value) : null;
55
+ }
56
+ return result;
57
+ }
58
+ catch (error) {
59
+ this.loggerService.error('Error when executing HGETALL command in redis', JSON.stringify(error));
60
+ throw error;
61
+ }
62
+ });
58
63
  }
59
- async set(key, data, ttl, ttlType = 'EX') {
60
- try {
61
- if (!ttl)
62
- ttl = parseInt(this.configService.get('REDIS_TTL') || '0', 10);
63
- if (data instanceof Object)
64
- data = JSON.stringify(data);
65
- if (ttl > 0) {
66
- await this.redisClient.set(key, data, {
67
- expiration: {
68
- type: ttlType,
69
- value: ttl,
70
- },
71
- });
72
- }
73
- else {
74
- await this.redisClient.set(key, data);
75
- }
76
- }
77
- catch (error) {
78
- this.loggerService.error(`Error when executing SET${ttlType === 'EX' ? '' : ttlType} command in redis`, JSON.stringify(error));
79
- throw error;
80
- }
64
+ set(key_1, data_1, ttl_1) {
65
+ return __awaiter(this, arguments, void 0, function* (key, data, ttl, ttlType = 'EX') {
66
+ try {
67
+ if (!ttl)
68
+ ttl = parseInt(this.configService.get('REDIS_TTL') || '0', 10);
69
+ if (data instanceof Object)
70
+ data = JSON.stringify(data);
71
+ if (ttl > 0) {
72
+ yield this.redisClient.set(key, data, {
73
+ expiration: {
74
+ type: ttlType,
75
+ value: ttl,
76
+ },
77
+ });
78
+ }
79
+ else {
80
+ yield this.redisClient.set(key, data);
81
+ }
82
+ }
83
+ catch (error) {
84
+ this.loggerService.error(`Error when executing SET${ttlType === 'EX' ? '' : ttlType} command in redis`, JSON.stringify(error));
85
+ throw error;
86
+ }
87
+ });
81
88
  }
82
- async del(key) {
83
- try {
84
- await this.redisClient.del(key);
85
- }
86
- catch (error) {
87
- this.loggerService.error('Error when executing DEL command in redis', JSON.stringify(error));
88
- throw error;
89
- }
89
+ del(key) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ try {
92
+ yield this.redisClient.del(key);
93
+ }
94
+ catch (error) {
95
+ this.loggerService.error('Error when executing DEL command in redis', JSON.stringify(error));
96
+ throw error;
97
+ }
98
+ });
90
99
  }
91
- async setnx(key, data) {
92
- try {
93
- if (data instanceof Object)
94
- data = JSON.stringify(data);
95
- return (await this.redisClient.setNX(key, data)) === 1;
96
- }
97
- catch (error) {
98
- this.loggerService.error('Error when executing SETNX command in redis', JSON.stringify(error));
99
- throw error;
100
- }
100
+ setnx(key, data) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ try {
103
+ if (data instanceof Object)
104
+ data = JSON.stringify(data);
105
+ return (yield this.redisClient.setNX(key, data)) === 1;
106
+ }
107
+ catch (error) {
108
+ this.loggerService.error('Error when executing SETNX command in redis', JSON.stringify(error));
109
+ throw error;
110
+ }
111
+ });
101
112
  }
102
- async asyncPing() {
103
- return (await this.redisClient.ping()) === 'PONG';
113
+ asyncPing() {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ return (yield this.redisClient.ping()) === 'PONG';
116
+ });
104
117
  }
105
- async keys(key) {
106
- try {
107
- const ret = await this.redisClient.keys(key);
108
- if (Array.isArray(ret)) {
109
- return ret.map((item) => (item ? JSON.parse(item) : null));
110
- }
111
- return ret ? JSON.parse(ret) : [];
112
- }
113
- catch (error) {
114
- this.loggerService.error('Error when executing KEYS command in redis', JSON.stringify(error));
115
- throw error;
116
- }
118
+ keys(key) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ try {
121
+ const ret = yield this.redisClient.keys(key);
122
+ if (Array.isArray(ret)) {
123
+ return ret.map((item) => (item ? JSON.parse(item) : null));
124
+ }
125
+ return ret ? JSON.parse(ret) : [];
126
+ }
127
+ catch (error) {
128
+ this.loggerService.error('Error when executing KEYS command in redis', JSON.stringify(error));
129
+ throw error;
130
+ }
131
+ });
117
132
  }
118
133
  };
119
134
  RedisService = __decorate([
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@edirect/redis",
3
- "version": "11.0.7",
3
+ "version": "11.0.9",
4
4
  "packageScope": "@edirect",
5
5
  "type": "module",
6
- "main": "./dist/index.esm.js",
7
- "module": "./dist/index.esm.js",
8
- "types": "./dist/index.esm.d.ts",
6
+ "main": "./dist/src/index.js",
7
+ "module": "./dist/src/index.js",
8
+ "types": "./dist/src/index.d.ts",
9
9
  "exports": {
10
10
  "./package.json": "./package.json",
11
11
  ".": {
12
12
  "development": "./src/index.ts",
13
- "types": "./dist/index.esm.d.ts",
14
- "import": "./dist/index.esm.js",
15
- "default": "./dist/index.esm.js"
13
+ "types": "./dist/src/index.d.ts",
14
+ "import": "./dist/src/index.js",
15
+ "default": "./dist/src/index.js"
16
16
  }
17
17
  },
18
18
  "files": [
@@ -22,23 +22,61 @@
22
22
  "dependencies": {
23
23
  "@nestjs/common": "^11.1.6",
24
24
  "redis": "^5.8.2",
25
- "@edirect/config": "11.0.7",
26
- "@edirect/logger": "11.0.7"
25
+ "@edirect/config": "11.0.9",
26
+ "@edirect/logger": "11.0.9"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@nx/rollup": "21.3.11"
30
30
  },
31
31
  "nx": {
32
+ "name": "@edirect/redis",
32
33
  "targets": {
33
- "build": {
34
+ "build:esm": {
35
+ "executor": "@nx/js:tsc",
36
+ "options": {
37
+ "main": "{projectRoot}/src/index.ts",
38
+ "tsConfig": "{projectRoot}/tsconfig.esm.json",
39
+ "outputPath": "{projectRoot}/dist/esm",
40
+ "assets": [
41
+ "{projectRoot}/package.json",
42
+ "{projectRoot}/README.md"
43
+ ]
44
+ }
45
+ },
46
+ "build:cjs": {
34
47
  "executor": "@nx/js:tsc",
48
+ "options": {
49
+ "main": "{projectRoot}/src/index.ts",
50
+ "tsConfig": "{projectRoot}/tsconfig.cjs.json",
51
+ "outputPath": "{projectRoot}/dist/cjs",
52
+ "assets": [
53
+ "{projectRoot}/package.json",
54
+ "{projectRoot}/README.md"
55
+ ]
56
+ }
57
+ },
58
+ "gen-pkg-json": {
59
+ "executor": "nx:run-commands",
60
+ "options": {
61
+ "cwd": "{projectRoot}",
62
+ "commands": [
63
+ "node -e \"const fs=require('fs');fs.mkdirSync('dist/esm',{recursive:true});fs.mkdirSync('dist/cjs',{recursive:true});fs.writeFileSync('dist/esm/package.json',JSON.stringify({type:'module'},null,2));fs.writeFileSync('dist/cjs/package.json',JSON.stringify({type:'commonjs'},null,2));\""
64
+ ]
65
+ },
35
66
  "outputs": [
36
- "{workspaceRoot}/packages/edirect-redis/dist"
37
- ],
67
+ "{projectRoot}/dist/esm/package.json",
68
+ "{projectRoot}/dist/cjs/package.json"
69
+ ]
70
+ },
71
+ "build": {
72
+ "executor": "nx:run-commands",
38
73
  "options": {
39
- "main": "{workspaceRoot}/packages/edirect-redis/src/index.ts",
40
- "tsConfig": "{workspaceRoot}/packages/edirect-redis/tsconfig.lib.json",
41
- "outputPath": "{workspaceRoot}/packages/edirect-redis/dist"
74
+ "commands": [
75
+ "nx build:esm @edirect/redis",
76
+ "nx build:cjs @edirect/redis",
77
+ "nx gen-pkg-json @edirect/redis"
78
+ ],
79
+ "parallel": false
42
80
  }
43
81
  }
44
82
  }