@nest-omni/core 3.1.1-12 → 3.1.1-14
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/cache/cache-metrics.service.d.ts +83 -0
- package/cache/cache-metrics.service.js +298 -0
- package/cache/cache-serialization.service.d.ts +29 -0
- package/cache/cache-serialization.service.js +217 -0
- package/cache/cache.health.d.ts +35 -0
- package/cache/cache.health.js +193 -0
- package/cache/cache.module.d.ts +24 -0
- package/cache/cache.module.js +105 -0
- package/cache/cache.service.d.ts +35 -0
- package/cache/cache.service.js +280 -0
- package/cache/cache.warmup.service.d.ts +34 -0
- package/cache/cache.warmup.service.js +199 -0
- package/cache/decorators/cache-evict.decorator.d.ts +2 -0
- package/cache/decorators/cache-evict.decorator.js +72 -0
- package/cache/decorators/cache-put.decorator.d.ts +2 -0
- package/cache/decorators/cache-put.decorator.js +48 -0
- package/cache/decorators/cacheable.decorator.d.ts +4 -0
- package/cache/decorators/cacheable.decorator.js +68 -0
- package/cache/decorators/index.d.ts +3 -0
- package/cache/decorators/index.js +11 -0
- package/cache/dependencies/callback.dependency.d.ts +11 -0
- package/cache/dependencies/callback.dependency.js +92 -0
- package/cache/dependencies/chain.dependency.d.ts +20 -0
- package/cache/dependencies/chain.dependency.js +113 -0
- package/cache/dependencies/db.dependency.d.ts +15 -0
- package/cache/dependencies/db.dependency.js +71 -0
- package/cache/dependencies/file.dependency.d.ts +17 -0
- package/cache/dependencies/file.dependency.js +63 -0
- package/cache/dependencies/index.d.ts +6 -0
- package/cache/dependencies/index.js +22 -0
- package/cache/dependencies/tag.dependency.d.ts +16 -0
- package/cache/dependencies/tag.dependency.js +75 -0
- package/cache/dependencies/time.dependency.d.ts +19 -0
- package/cache/dependencies/time.dependency.js +71 -0
- package/cache/examples/quick-start.d.ts +5 -0
- package/cache/examples/quick-start.js +341 -0
- package/cache/index.d.ts +14 -0
- package/cache/index.js +41 -0
- package/cache/interfaces/cache-dependency.interface.d.ts +11 -0
- package/cache/interfaces/cache-dependency.interface.js +2 -0
- package/cache/interfaces/cache-options.interface.d.ts +32 -0
- package/cache/interfaces/cache-options.interface.js +9 -0
- package/cache/interfaces/cache-provider.interface.d.ts +28 -0
- package/cache/interfaces/cache-provider.interface.js +2 -0
- package/cache/interfaces/index.d.ts +3 -0
- package/cache/interfaces/index.js +19 -0
- package/cache/providers/base-cache.provider.d.ts +16 -0
- package/cache/providers/base-cache.provider.js +32 -0
- package/cache/providers/cls-cache.provider.d.ts +16 -0
- package/cache/providers/cls-cache.provider.js +140 -0
- package/cache/providers/index.d.ts +4 -0
- package/cache/providers/index.js +23 -0
- package/cache/providers/memory-cache.provider.d.ts +26 -0
- package/cache/providers/memory-cache.provider.js +171 -0
- package/cache/providers/redis-cache.provider.d.ts +23 -0
- package/cache/providers/redis-cache.provider.js +215 -0
- package/cache/utils/dependency-manager.util.d.ts +15 -0
- package/cache/utils/dependency-manager.util.js +141 -0
- package/cache/utils/index.d.ts +2 -0
- package/cache/utils/index.js +18 -0
- package/cache/utils/key-generator.util.d.ts +13 -0
- package/cache/utils/key-generator.util.js +74 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +4 -17
- package/redis-lock/index.d.ts +4 -0
- package/redis-lock/index.js +13 -0
- package/{setup → redis-lock}/redis-lock.decorator.d.ts +3 -0
- package/{setup → redis-lock}/redis-lock.decorator.js +25 -14
- package/redis-lock/redis-lock.examples.d.ts +13 -0
- package/redis-lock/redis-lock.examples.js +159 -0
- package/redis-lock/redis-lock.module.d.ts +23 -0
- package/redis-lock/redis-lock.module.js +117 -0
- package/{setup → redis-lock}/redis-lock.service.d.ts +9 -21
- package/{setup → redis-lock}/redis-lock.service.js +29 -103
- package/setup/index.d.ts +1 -2
- package/setup/index.js +1 -2
- package/setup/schedule.decorator.js +1 -1
- package/shared/serviceRegistryModule.js +63 -4
- package/shared/services/api-config.service.d.ts +0 -5
- package/shared/services/api-config.service.js +3 -27
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var RedisLockModule_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.RedisLockModule = exports.REDIS_LOCK_SERVICE = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const redis_lock_service_1 = require("./redis-lock.service");
|
|
16
|
+
const redis_lock_decorator_1 = require("./redis-lock.decorator");
|
|
17
|
+
exports.REDIS_LOCK_SERVICE = Symbol('REDIS_LOCK_SERVICE');
|
|
18
|
+
let RedisLockModule = RedisLockModule_1 = class RedisLockModule {
|
|
19
|
+
constructor() { }
|
|
20
|
+
static forRoot(options) {
|
|
21
|
+
const { isGlobal = true, redisClient, defaultTtl, defaultKeyPrefix, defaultRetryCount, defaultRetryDelay, } = options;
|
|
22
|
+
return {
|
|
23
|
+
module: RedisLockModule_1,
|
|
24
|
+
global: isGlobal,
|
|
25
|
+
providers: [
|
|
26
|
+
{
|
|
27
|
+
provide: 'REDIS_LOCK_CLIENT',
|
|
28
|
+
useValue: redisClient,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
provide: exports.REDIS_LOCK_SERVICE,
|
|
32
|
+
useFactory: () => {
|
|
33
|
+
const service = new redis_lock_service_1.RedisLockService();
|
|
34
|
+
service.setRedisClient(redisClient);
|
|
35
|
+
if (defaultTtl) {
|
|
36
|
+
service.setDefaultTtl(defaultTtl);
|
|
37
|
+
}
|
|
38
|
+
if (defaultKeyPrefix) {
|
|
39
|
+
service.setDefaultKeyPrefix(defaultKeyPrefix);
|
|
40
|
+
}
|
|
41
|
+
if (defaultRetryCount !== undefined) {
|
|
42
|
+
service.setDefaultRetryCount(defaultRetryCount);
|
|
43
|
+
}
|
|
44
|
+
if (defaultRetryDelay !== undefined) {
|
|
45
|
+
service.setDefaultRetryDelay(defaultRetryDelay);
|
|
46
|
+
}
|
|
47
|
+
return service;
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
provide: redis_lock_service_1.RedisLockService,
|
|
52
|
+
useExisting: exports.REDIS_LOCK_SERVICE,
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
exports: [exports.REDIS_LOCK_SERVICE, redis_lock_service_1.RedisLockService],
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
static forRootAsync(options) {
|
|
59
|
+
var _a;
|
|
60
|
+
return {
|
|
61
|
+
module: RedisLockModule_1,
|
|
62
|
+
global: (_a = options.isGlobal) !== null && _a !== void 0 ? _a : true,
|
|
63
|
+
imports: options.imports || [],
|
|
64
|
+
providers: [
|
|
65
|
+
{
|
|
66
|
+
provide: 'REDIS_LOCK_MODULE_OPTIONS',
|
|
67
|
+
useFactory: options.useFactory,
|
|
68
|
+
inject: options.inject || [],
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
provide: 'REDIS_LOCK_CLIENT',
|
|
72
|
+
useFactory: (moduleOptions) => moduleOptions.redisClient,
|
|
73
|
+
inject: ['REDIS_LOCK_MODULE_OPTIONS'],
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
provide: exports.REDIS_LOCK_SERVICE,
|
|
77
|
+
useFactory: (moduleOptions) => {
|
|
78
|
+
const service = new redis_lock_service_1.RedisLockService();
|
|
79
|
+
service.setRedisClient(moduleOptions.redisClient);
|
|
80
|
+
if (moduleOptions.defaultTtl) {
|
|
81
|
+
service.setDefaultTtl(moduleOptions.defaultTtl);
|
|
82
|
+
}
|
|
83
|
+
if (moduleOptions.defaultKeyPrefix) {
|
|
84
|
+
service.setDefaultKeyPrefix(moduleOptions.defaultKeyPrefix);
|
|
85
|
+
}
|
|
86
|
+
if (moduleOptions.defaultRetryCount !== undefined) {
|
|
87
|
+
service.setDefaultRetryCount(moduleOptions.defaultRetryCount);
|
|
88
|
+
}
|
|
89
|
+
if (moduleOptions.defaultRetryDelay !== undefined) {
|
|
90
|
+
service.setDefaultRetryDelay(moduleOptions.defaultRetryDelay);
|
|
91
|
+
}
|
|
92
|
+
return service;
|
|
93
|
+
},
|
|
94
|
+
inject: ['REDIS_LOCK_MODULE_OPTIONS'],
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
provide: redis_lock_service_1.RedisLockService,
|
|
98
|
+
useExisting: exports.REDIS_LOCK_SERVICE,
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
exports: [exports.REDIS_LOCK_SERVICE, redis_lock_service_1.RedisLockService],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
onModuleInit() {
|
|
105
|
+
const globalInstance = redis_lock_service_1.RedisLockService.getGlobalInstance();
|
|
106
|
+
if (globalInstance) {
|
|
107
|
+
this.redisLockService = globalInstance;
|
|
108
|
+
(0, redis_lock_decorator_1.setLockService)(this.redisLockService);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
exports.RedisLockModule = RedisLockModule;
|
|
113
|
+
exports.RedisLockModule = RedisLockModule = RedisLockModule_1 = __decorate([
|
|
114
|
+
(0, common_1.Global)(),
|
|
115
|
+
(0, common_1.Module)({}),
|
|
116
|
+
__metadata("design:paramtypes", [])
|
|
117
|
+
], RedisLockModule);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import type Redis from 'ioredis';
|
|
2
3
|
export interface LockOptions {
|
|
3
4
|
ttl?: number;
|
|
4
5
|
retryCount?: number;
|
|
@@ -13,33 +14,22 @@ export interface LockResult {
|
|
|
13
14
|
error?: string;
|
|
14
15
|
autoExtendTimer?: NodeJS.Timeout;
|
|
15
16
|
}
|
|
16
|
-
export
|
|
17
|
-
host?: string;
|
|
18
|
-
port?: number;
|
|
19
|
-
password?: string;
|
|
20
|
-
db?: number;
|
|
21
|
-
retryDelay?: number;
|
|
22
|
-
maxRetries?: number;
|
|
23
|
-
reconnectOnError?: (err: Error) => boolean;
|
|
24
|
-
}
|
|
25
|
-
export declare class RedisLockService implements OnModuleInit, OnModuleDestroy {
|
|
17
|
+
export declare class RedisLockService implements OnModuleInit {
|
|
26
18
|
private static globalInstance;
|
|
27
|
-
private static globalConfig;
|
|
28
19
|
private readonly logger;
|
|
29
20
|
private redis;
|
|
30
|
-
private redisOptions;
|
|
31
|
-
private isConnected;
|
|
32
|
-
private isConnecting;
|
|
33
|
-
private connectionPromise;
|
|
34
21
|
private readonly defaultOptions;
|
|
35
|
-
|
|
36
|
-
constructor(redisOptions?: RedisOptions);
|
|
37
|
-
static initializeGlobalInstance(config?: RedisOptions): RedisLockService;
|
|
22
|
+
constructor();
|
|
38
23
|
static getGlobalInstance(): RedisLockService | null;
|
|
39
24
|
static getOrCreateGlobalInstance(): RedisLockService;
|
|
40
25
|
static setGlobalInstance(instance: RedisLockService): void;
|
|
41
26
|
onModuleInit(): Promise<void>;
|
|
42
27
|
onModuleDestroy(): Promise<void>;
|
|
28
|
+
setRedisClient(redis: Redis): void;
|
|
29
|
+
setDefaultTtl(ttl: number): void;
|
|
30
|
+
setDefaultKeyPrefix(prefix: string): void;
|
|
31
|
+
setDefaultRetryCount(count: number): void;
|
|
32
|
+
setDefaultRetryDelay(delay: number): void;
|
|
43
33
|
acquireLock(key: string, options?: LockOptions): Promise<LockResult>;
|
|
44
34
|
releaseLock(key: string, lockValue: string, keyPrefix?: string): Promise<boolean>;
|
|
45
35
|
extendLock(key: string, lockValue: string, ttl: number, keyPrefix?: string): Promise<boolean>;
|
|
@@ -50,9 +40,7 @@ export declare class RedisLockService implements OnModuleInit, OnModuleDestroy {
|
|
|
50
40
|
ttl: number | null;
|
|
51
41
|
}>;
|
|
52
42
|
forceRelease(key: string, keyPrefix?: string): Promise<boolean>;
|
|
53
|
-
getRedisOptions(): RedisOptions;
|
|
54
43
|
private getRedis;
|
|
55
|
-
private createRedisConnection;
|
|
56
44
|
private generateLockValue;
|
|
57
45
|
private buildLockKey;
|
|
58
46
|
private sleep;
|
|
@@ -21,14 +21,10 @@ var RedisLockService_1;
|
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
22
|
exports.RedisLockService = void 0;
|
|
23
23
|
const common_1 = require("@nestjs/common");
|
|
24
|
-
const ioredis_1 = require("ioredis");
|
|
25
24
|
let RedisLockService = RedisLockService_1 = class RedisLockService {
|
|
26
|
-
constructor(
|
|
25
|
+
constructor() {
|
|
27
26
|
this.logger = new common_1.Logger(RedisLockService_1.name);
|
|
28
27
|
this.redis = null;
|
|
29
|
-
this.isConnected = false;
|
|
30
|
-
this.isConnecting = false;
|
|
31
|
-
this.connectionPromise = null;
|
|
32
28
|
this.defaultOptions = {
|
|
33
29
|
ttl: 300000,
|
|
34
30
|
retryCount: 0,
|
|
@@ -37,46 +33,16 @@ let RedisLockService = RedisLockService_1 = class RedisLockService {
|
|
|
37
33
|
throwOnFailure: false,
|
|
38
34
|
autoExtend: 0,
|
|
39
35
|
};
|
|
40
|
-
this.defaultRedisOptions = {
|
|
41
|
-
host: process.env.REDIS_HOST || 'localhost',
|
|
42
|
-
port: parseInt(process.env.REDIS_PORT || '6379', 10),
|
|
43
|
-
password: process.env.REDIS_PASSWORD,
|
|
44
|
-
db: parseInt(process.env.REDIS_DB || '0', 10),
|
|
45
|
-
retryDelay: 2000,
|
|
46
|
-
maxRetries: 10,
|
|
47
|
-
reconnectOnError: (err) => {
|
|
48
|
-
this.logger.warn('Redis reconnection error:', err.message);
|
|
49
|
-
return true;
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
this.redisOptions = redisOptions ||
|
|
53
|
-
RedisLockService_1.globalConfig || Object.assign({}, this.defaultRedisOptions);
|
|
54
36
|
if (!RedisLockService_1.globalInstance) {
|
|
55
37
|
RedisLockService_1.globalInstance = this;
|
|
56
38
|
}
|
|
57
39
|
}
|
|
58
|
-
static initializeGlobalInstance(config) {
|
|
59
|
-
if (!RedisLockService_1.globalInstance) {
|
|
60
|
-
new RedisLockService_1(config || RedisLockService_1.globalConfig);
|
|
61
|
-
}
|
|
62
|
-
return RedisLockService_1.globalInstance;
|
|
63
|
-
}
|
|
64
40
|
static getGlobalInstance() {
|
|
65
41
|
return RedisLockService_1.globalInstance;
|
|
66
42
|
}
|
|
67
43
|
static getOrCreateGlobalInstance() {
|
|
68
44
|
if (!RedisLockService_1.globalInstance) {
|
|
69
|
-
|
|
70
|
-
RedisLockService_1.globalConfig = {
|
|
71
|
-
host: process.env.REDIS_HOST || 'localhost',
|
|
72
|
-
port: parseInt(process.env.REDIS_PORT || '6379', 10),
|
|
73
|
-
password: process.env.REDIS_PASSWORD,
|
|
74
|
-
db: parseInt(process.env.REDIS_DB || '0', 10),
|
|
75
|
-
retryDelay: 2000,
|
|
76
|
-
maxRetries: 10,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
return RedisLockService_1.initializeGlobalInstance();
|
|
45
|
+
RedisLockService_1.globalInstance = new RedisLockService_1();
|
|
80
46
|
}
|
|
81
47
|
return RedisLockService_1.globalInstance;
|
|
82
48
|
}
|
|
@@ -85,18 +51,35 @@ let RedisLockService = RedisLockService_1 = class RedisLockService {
|
|
|
85
51
|
}
|
|
86
52
|
onModuleInit() {
|
|
87
53
|
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
-
this.
|
|
54
|
+
if (!this.redis) {
|
|
55
|
+
this.logger.warn('RedisLockService initialized but no Redis client provided. Redis Lock functionality will not work.');
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
this.logger.log('RedisLockService initialized with Redis client');
|
|
59
|
+
}
|
|
89
60
|
});
|
|
90
61
|
}
|
|
91
62
|
onModuleDestroy() {
|
|
92
63
|
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
-
|
|
94
|
-
yield this.redis.quit();
|
|
95
|
-
this.isConnected = false;
|
|
96
|
-
this.logger.log('Redis connection closed');
|
|
97
|
-
}
|
|
64
|
+
this.logger.log('RedisLockService destroyed');
|
|
98
65
|
});
|
|
99
66
|
}
|
|
67
|
+
setRedisClient(redis) {
|
|
68
|
+
this.redis = redis;
|
|
69
|
+
this.logger.log('Redis client set for RedisLockService');
|
|
70
|
+
}
|
|
71
|
+
setDefaultTtl(ttl) {
|
|
72
|
+
this.defaultOptions.ttl = ttl;
|
|
73
|
+
}
|
|
74
|
+
setDefaultKeyPrefix(prefix) {
|
|
75
|
+
this.defaultOptions.keyPrefix = prefix;
|
|
76
|
+
}
|
|
77
|
+
setDefaultRetryCount(count) {
|
|
78
|
+
this.defaultOptions.retryCount = count;
|
|
79
|
+
}
|
|
80
|
+
setDefaultRetryDelay(delay) {
|
|
81
|
+
this.defaultOptions.retryDelay = delay;
|
|
82
|
+
}
|
|
100
83
|
acquireLock(key_1) {
|
|
101
84
|
return __awaiter(this, arguments, void 0, function* (key, options = {}) {
|
|
102
85
|
const redis = yield this.getRedis();
|
|
@@ -279,68 +262,12 @@ let RedisLockService = RedisLockService_1 = class RedisLockService {
|
|
|
279
262
|
}
|
|
280
263
|
});
|
|
281
264
|
}
|
|
282
|
-
getRedisOptions() {
|
|
283
|
-
return Object.assign({}, this.redisOptions);
|
|
284
|
-
}
|
|
285
265
|
getRedis() {
|
|
286
266
|
return __awaiter(this, void 0, void 0, function* () {
|
|
287
|
-
if (this.redis
|
|
288
|
-
|
|
267
|
+
if (!this.redis) {
|
|
268
|
+
throw new Error('RedisLockService requires a Redis client to be set. Please provide a Redis client through the module configuration.');
|
|
289
269
|
}
|
|
290
|
-
|
|
291
|
-
return this.connectionPromise;
|
|
292
|
-
}
|
|
293
|
-
this.isConnecting = true;
|
|
294
|
-
this.connectionPromise = this.createRedisConnection()
|
|
295
|
-
.then((redis) => {
|
|
296
|
-
this.isConnecting = false;
|
|
297
|
-
this.isConnected = true;
|
|
298
|
-
return redis;
|
|
299
|
-
})
|
|
300
|
-
.catch((error) => {
|
|
301
|
-
this.isConnecting = false;
|
|
302
|
-
this.connectionPromise = null;
|
|
303
|
-
throw error;
|
|
304
|
-
});
|
|
305
|
-
return this.connectionPromise;
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
createRedisConnection() {
|
|
309
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
310
|
-
return new Promise((resolve, reject) => {
|
|
311
|
-
const redis = new ioredis_1.default({
|
|
312
|
-
host: this.redisOptions.host,
|
|
313
|
-
port: this.redisOptions.port,
|
|
314
|
-
password: this.redisOptions.password,
|
|
315
|
-
db: this.redisOptions.db,
|
|
316
|
-
retryStrategy: (times) => {
|
|
317
|
-
this.logger.warn(`Redis connection retry attempt ${times}`);
|
|
318
|
-
if (times > (this.redisOptions.maxRetries || 10)) {
|
|
319
|
-
this.logger.error('Max retries reached, giving up on Redis connection');
|
|
320
|
-
this.isConnected = false;
|
|
321
|
-
return undefined;
|
|
322
|
-
}
|
|
323
|
-
const delay = Math.min(times * (this.redisOptions.retryDelay || 2000), 30000);
|
|
324
|
-
this.logger.log(`Retrying Redis connection in ${delay}ms`);
|
|
325
|
-
return delay;
|
|
326
|
-
},
|
|
327
|
-
reconnectOnError: this.redisOptions.reconnectOnError,
|
|
328
|
-
});
|
|
329
|
-
const onConnect = () => {
|
|
330
|
-
this.logger.log(`Connected to Redis at ${this.redisOptions.host}:${this.redisOptions.port}`);
|
|
331
|
-
this.isConnected = true;
|
|
332
|
-
resolve(redis);
|
|
333
|
-
};
|
|
334
|
-
const onError = (err) => {
|
|
335
|
-
this.logger.error('Redis connection error:', err.message);
|
|
336
|
-
if (!this.isConnected) {
|
|
337
|
-
reject(err);
|
|
338
|
-
}
|
|
339
|
-
};
|
|
340
|
-
redis.once('connect', onConnect);
|
|
341
|
-
redis.once('error', onError);
|
|
342
|
-
this.redis = redis;
|
|
343
|
-
});
|
|
270
|
+
return this.redis;
|
|
344
271
|
});
|
|
345
272
|
}
|
|
346
273
|
generateLockValue() {
|
|
@@ -355,8 +282,7 @@ let RedisLockService = RedisLockService_1 = class RedisLockService {
|
|
|
355
282
|
};
|
|
356
283
|
exports.RedisLockService = RedisLockService;
|
|
357
284
|
RedisLockService.globalInstance = null;
|
|
358
|
-
RedisLockService.globalConfig = null;
|
|
359
285
|
exports.RedisLockService = RedisLockService = RedisLockService_1 = __decorate([
|
|
360
286
|
(0, common_1.Injectable)(),
|
|
361
|
-
__metadata("design:paramtypes", [
|
|
287
|
+
__metadata("design:paramtypes", [])
|
|
362
288
|
], RedisLockService);
|
package/setup/index.d.ts
CHANGED
package/setup/index.js
CHANGED
|
@@ -18,5 +18,4 @@ __exportStar(require("./bootstrap.setup"), exports);
|
|
|
18
18
|
__exportStar(require("./mode.setup"), exports);
|
|
19
19
|
__exportStar(require("./worker.decorator"), exports);
|
|
20
20
|
__exportStar(require("./schedule.decorator"), exports);
|
|
21
|
-
__exportStar(require("
|
|
22
|
-
__exportStar(require("./redis-lock.decorator"), exports);
|
|
21
|
+
__exportStar(require("../redis-lock"), exports);
|
|
@@ -21,7 +21,7 @@ exports.WorkerCronAdvanced = WorkerCronAdvanced;
|
|
|
21
21
|
const common_1 = require("@nestjs/common");
|
|
22
22
|
const schedule_1 = require("@nestjs/schedule");
|
|
23
23
|
const mode_setup_1 = require("./mode.setup");
|
|
24
|
-
const redis_lock_service_1 = require("
|
|
24
|
+
const redis_lock_service_1 = require("../redis-lock/redis-lock.service");
|
|
25
25
|
function generateLockKey(className, methodName) {
|
|
26
26
|
return `${className}.${methodName}`;
|
|
27
27
|
}
|
|
@@ -5,6 +5,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
+
});
|
|
16
|
+
};
|
|
8
17
|
var _a;
|
|
9
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
19
|
exports.ServiceRegistryModule = void 0;
|
|
@@ -17,13 +26,15 @@ const common_1 = require("@nestjs/common");
|
|
|
17
26
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
18
27
|
const schedule_1 = require("@nestjs/schedule");
|
|
19
28
|
const setup_1 = require("@sentry/nestjs/setup");
|
|
20
|
-
const
|
|
29
|
+
const cache_1 = require("../cache");
|
|
21
30
|
const transactional_1 = require("@nestjs-cls/transactional");
|
|
22
31
|
const transactional_adapter_typeorm_1 = require("@nestjs-cls/transactional-adapter-typeorm");
|
|
23
32
|
const nestjs_i18n_1 = require("nestjs-i18n");
|
|
24
33
|
const health_checker_1 = require("../health-checker");
|
|
34
|
+
const nestjs_redis_1 = require("@songkeys/nestjs-redis");
|
|
25
35
|
const services_1 = require("./services");
|
|
26
36
|
const nestjs_cls_1 = require("nestjs-cls");
|
|
37
|
+
const redis_lock_1 = require("../redis-lock");
|
|
27
38
|
const typeorm_2 = require("typeorm");
|
|
28
39
|
const providers = [
|
|
29
40
|
services_1.ApiConfigService,
|
|
@@ -73,6 +84,14 @@ const modules = [
|
|
|
73
84
|
resolvers: [new nestjs_i18n_1.QueryResolver(['lang']), nestjs_i18n_1.AcceptLanguageResolver],
|
|
74
85
|
}),
|
|
75
86
|
health_checker_1.HealthCheckerModule,
|
|
87
|
+
nestjs_redis_1.RedisModule.forRoot({
|
|
88
|
+
config: {
|
|
89
|
+
host: process.env.REDIS_HOST || 'localhost',
|
|
90
|
+
port: parseInt(process.env.REDIS_PORT) || 6379,
|
|
91
|
+
password: process.env.REDIS_PASSWORD,
|
|
92
|
+
db: parseInt(process.env.REDIS_DB) || 0,
|
|
93
|
+
},
|
|
94
|
+
}),
|
|
76
95
|
];
|
|
77
96
|
if (services_1.ApiConfigService.toBoolean(process.env.DB_ENABLED, true)) {
|
|
78
97
|
modules.push(typeorm_1.TypeOrmModule.forRootAsync({
|
|
@@ -87,10 +106,50 @@ if (services_1.ApiConfigService.toBoolean(process.env.BULL_ENABLED)) {
|
|
|
87
106
|
}));
|
|
88
107
|
}
|
|
89
108
|
if (services_1.ApiConfigService.toBoolean(process.env.CACHE_ENABLED)) {
|
|
90
|
-
modules.push(
|
|
91
|
-
|
|
109
|
+
modules.push(cache_1.CacheModule.forRootAsync({
|
|
110
|
+
imports: [nestjs_redis_1.RedisModule],
|
|
111
|
+
inject: [services_1.ApiConfigService, nestjs_redis_1.RedisService, typeorm_2.DataSource],
|
|
112
|
+
isGlobal: true,
|
|
113
|
+
useFactory: (config, redisService, dataSource) => __awaiter(void 0, void 0, void 0, function* () {
|
|
114
|
+
let redisClient = null;
|
|
115
|
+
try {
|
|
116
|
+
redisClient = redisService.getClient();
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
console.error('Redis client not available for cache, using memory cache only:', error);
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
isGlobal: true,
|
|
123
|
+
redisClient,
|
|
124
|
+
dataSource,
|
|
125
|
+
memoryTtl: config.getNumber('CACHE_TTL'),
|
|
126
|
+
memoryNamespace: config.getString('CACHE_NAMESPACE', 'default'),
|
|
127
|
+
};
|
|
128
|
+
}),
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
if (services_1.ApiConfigService.toBoolean(process.env.SCHEDULE_ENABLED) &&
|
|
132
|
+
services_1.ApiConfigService.toBoolean(process.env.REDIS_LOCK_ENABLED, true)) {
|
|
133
|
+
modules.push(redis_lock_1.RedisLockModule.forRootAsync({
|
|
134
|
+
imports: [nestjs_redis_1.RedisModule],
|
|
135
|
+
inject: [services_1.ApiConfigService, nestjs_redis_1.RedisService],
|
|
92
136
|
isGlobal: true,
|
|
93
|
-
useFactory: (config) =>
|
|
137
|
+
useFactory: (config, redisService) => __awaiter(void 0, void 0, void 0, function* () {
|
|
138
|
+
let redisClient = null;
|
|
139
|
+
try {
|
|
140
|
+
redisClient = redisService.getClient();
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
console.error('Redis client not available for RedisLock, Redis Lock functionality will not work:', error);
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
redisClient,
|
|
147
|
+
defaultTtl: config.getNumber('REDIS_LOCK_TTL', 300000),
|
|
148
|
+
defaultKeyPrefix: config.getString('REDIS_LOCK_KEY_PREFIX', 'lock'),
|
|
149
|
+
defaultRetryCount: config.getNumber('REDIS_LOCK_RETRY_COUNT', 3),
|
|
150
|
+
defaultRetryDelay: config.getNumber('REDIS_LOCK_RETRY_DELAY', 200),
|
|
151
|
+
};
|
|
152
|
+
}),
|
|
94
153
|
}));
|
|
95
154
|
}
|
|
96
155
|
if (services_1.ApiConfigService.toBoolean(process.env.SCHEDULE_ENABLED)) {
|
|
@@ -5,7 +5,6 @@ import { Params } from 'nestjs-pino';
|
|
|
5
5
|
import { BullRootModuleOptions } from '@nestjs/bull/dist/interfaces/bull-module-options.interface';
|
|
6
6
|
import { AxiosProxyConfig } from 'axios';
|
|
7
7
|
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
|
|
8
|
-
import Keyv from 'keyv';
|
|
9
8
|
export declare class ApiConfigService {
|
|
10
9
|
private configService;
|
|
11
10
|
static rootPath: string;
|
|
@@ -51,9 +50,5 @@ export declare class ApiConfigService {
|
|
|
51
50
|
getNumber(key: string, defaultValue?: number): number;
|
|
52
51
|
getBoolean(key: string, defaultValue?: boolean): boolean;
|
|
53
52
|
getString(key: string, defaultValue?: string): string;
|
|
54
|
-
cacheConfig(): Promise<{
|
|
55
|
-
isGlobal: boolean;
|
|
56
|
-
store: Keyv<any>;
|
|
57
|
-
}>;
|
|
58
53
|
private get;
|
|
59
54
|
}
|
|
@@ -8,15 +8,6 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
12
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
13
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
14
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
15
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
16
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
17
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
18
|
-
});
|
|
19
|
-
};
|
|
20
11
|
var ApiConfigService_1;
|
|
21
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
13
|
exports.ApiConfigService = void 0;
|
|
@@ -26,8 +17,6 @@ const lodash_1 = require("lodash");
|
|
|
26
17
|
const connect_redis_1 = require("connect-redis");
|
|
27
18
|
const ioredis_1 = require("ioredis");
|
|
28
19
|
const common_2 = require("../../common");
|
|
29
|
-
const keyv_1 = require("keyv");
|
|
30
|
-
const redis_1 = require("@keyv/redis");
|
|
31
20
|
let ApiConfigService = ApiConfigService_1 = class ApiConfigService {
|
|
32
21
|
constructor(configService) {
|
|
33
22
|
this.configService = configService;
|
|
@@ -70,6 +59,7 @@ let ApiConfigService = ApiConfigService_1 = class ApiConfigService {
|
|
|
70
59
|
synchronize: this.isDev,
|
|
71
60
|
migrationsRun: true,
|
|
72
61
|
logging: this.getBoolean('DB_LOG_ENABLED'),
|
|
62
|
+
logger: this.isDev ? 'formatted-console' : 'simple-console',
|
|
73
63
|
maxQueryExecutionTime: 3000,
|
|
74
64
|
namingStrategy: new common_2.SnakeNamingStrategy(),
|
|
75
65
|
timezone: this.getString('TZ'),
|
|
@@ -95,7 +85,7 @@ let ApiConfigService = ApiConfigService_1 = class ApiConfigService {
|
|
|
95
85
|
}
|
|
96
86
|
get corsEnabled() {
|
|
97
87
|
try {
|
|
98
|
-
return this.getBoolean('CORS_ENABLED');
|
|
88
|
+
return this.getBoolean('CORS_ENABLED', false);
|
|
99
89
|
}
|
|
100
90
|
catch (error) {
|
|
101
91
|
console.log(error);
|
|
@@ -279,24 +269,10 @@ let ApiConfigService = ApiConfigService_1 = class ApiConfigService {
|
|
|
279
269
|
const value = this.get(key, defaultValue);
|
|
280
270
|
return value.replace(/\\n/g, '\n');
|
|
281
271
|
}
|
|
282
|
-
cacheConfig() {
|
|
283
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
284
|
-
const uri = `redis://${this.getString('REDIS_USERNAME') ? this.getString('REDIS_USERNAME') + ':' : ''}${this.getString('REDIS_PASSWORD') ? this.getString('REDIS_PASSWORD') + '@' : ''}${this.getString('REDIS_HOST')}:${this.getNumber('REDIS_PORT')}/${this.getNumber('REDIS_DB')}`;
|
|
285
|
-
const keyvRedis = new redis_1.default(uri);
|
|
286
|
-
const store = new keyv_1.default({
|
|
287
|
-
store: keyvRedis,
|
|
288
|
-
ttl: this.getNumber('CACHE_TTL'),
|
|
289
|
-
});
|
|
290
|
-
return {
|
|
291
|
-
isGlobal: true,
|
|
292
|
-
store,
|
|
293
|
-
};
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
272
|
get(key, defaultValue) {
|
|
297
273
|
const value = this.configService.get(key);
|
|
298
274
|
if ((0, lodash_1.isNil)(value)) {
|
|
299
|
-
if (defaultValue) {
|
|
275
|
+
if (defaultValue !== undefined) {
|
|
300
276
|
return defaultValue;
|
|
301
277
|
}
|
|
302
278
|
throw new Error(`key:'${key}' - environment does not set`);
|