@jsnw/nestjs-ioredis 1.3.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,26 +1,15 @@
1
1
  # @jsnw/nestjs-ioredis
2
2
 
3
- NestJS module for integrating Redis using [ioredis](https://www.npmjs.com/package/ioredis) with support for multiple connections and dependency injection.
4
-
5
- ## Features
6
-
7
- - 🚀 Easy integration with NestJS applications
8
- - 🔌 Support for multiple named Redis connections
9
- - 💉 Dependency injection with `@InjectRedis()` decorator
10
- - 🔄 Automatic connection lifecycle management
11
- - 🏷️ Key prefix support with automatic formatting
12
- - 🛡️ TypeScript support with full type definitions
3
+ NestJS module for integrating Redis using [ioredis](https://www.npmjs.com/package/ioredis)
13
4
 
14
5
  ## Installation
15
6
 
16
7
  ```bash
17
- npm i -s @jsnw/nestjs-ioredis ioredis
8
+ npm i -s @jsnw/nestjs-ioredis ioredis @nestjs/common@11
18
9
  ```
19
10
 
20
11
  ## Quick Start
21
12
 
22
- ### Basic Usage
23
-
24
13
  Import `RedisModule` in your app module and configure it with `forRoot()`:
25
14
 
26
15
  ```typescript
@@ -33,171 +22,13 @@ import { RedisModule } from '@jsnw/nestjs-ioredis';
33
22
  hostname: 'localhost',
34
23
  port: 6379,
35
24
  username: 'default',
36
- password: 'your-password',
37
- isDefault: true,
25
+ password: 'your-password'
38
26
  }),
39
27
  ],
40
28
  })
41
29
  export class AppModule {}
42
30
  ```
43
31
 
44
- ### Inject Redis in Services
45
-
46
- Use the `@InjectRedis()` decorator to inject Redis instances into your services:
47
-
48
- ```typescript
49
- import { Injectable } from '@nestjs/common';
50
- import { InjectRedis } from '@jsnw/nestjs-ioredis';
51
- import type { Redis } from 'ioredis';
52
-
53
- @Injectable()
54
- export class UserService {
55
- constructor(
56
- @InjectRedis() private readonly redis: Redis,
57
- ) {}
58
-
59
- async cacheUser(userId: string, userData: any): Promise<void> {
60
- await this.redis.set(`user:${userId}`, JSON.stringify(userData));
61
- }
62
-
63
- async getUser(userId: string): Promise<any> {
64
- const data = await this.redis.get(`user:${userId}`);
65
- return data ? JSON.parse(data) : null;
66
- }
67
- }
68
- ```
69
-
70
- ## Configuration
71
-
72
- ### RedisForRootParams
73
-
74
- | Parameter | Type | Required | Default | Description |
75
- |-----------|------|----------|---------|-------------|
76
- | `hostname` | `string` | Yes | - | Redis server hostname |
77
- | `port` | `number` | Yes | - | Redis server port |
78
- | `username` | `string` | Yes | - | Redis username |
79
- | `password` | `string` | Yes | - | Redis password |
80
- | `connectionName` | `string` | No | Auto-generated | Unique name for the connection |
81
- | `db` | `number` | No | `0` | Redis database index |
82
- | `keyPrefix` | `string` | No | `''` | Prefix for all keys (automatically adds ':' separator) |
83
- | `connectionTimeout` | `number` | No | `10000` | Connection timeout in milliseconds |
84
- | `lazy` | `boolean` | No | `false` | Enable lazy connection (connect on first command) |
85
- | `isDefault` | `boolean` | No | `false` | Mark this connection as the default |
86
-
87
- ## Advanced Usage
88
-
89
- ### Multiple Named Connections
90
-
91
- You can configure multiple Redis connections with different names:
92
-
93
- ```typescript
94
- import { Module } from '@nestjs/common';
95
- import { RedisModule } from '@jsnw/nestjs-ioredis';
96
-
97
- @Module({
98
- imports: [
99
- RedisModule.forRoot({
100
- connectionName: 'cache',
101
- hostname: 'cache.redis.local',
102
- port: 6379,
103
- username: 'default',
104
- password: 'cache-password',
105
- isDefault: true, // This is the default connection
106
- }),
107
- RedisModule.forRoot({
108
- connectionName: 'sessions',
109
- hostname: 'sessions.redis.local',
110
- port: 6379,
111
- username: 'default',
112
- password: 'sessions-password',
113
- db: 1,
114
- }),
115
- ],
116
- })
117
- export class AppModule {}
118
- ```
119
-
120
- ### Inject Named Connections
121
-
122
- ```typescript
123
- import { Injectable } from '@nestjs/common';
124
- import { InjectRedis } from '@jsnw/nestjs-ioredis';
125
- import type { Redis } from 'ioredis';
126
-
127
- @Injectable()
128
- export class AppService {
129
- constructor(
130
- @InjectRedis('cache') private readonly cacheRedis: Redis,
131
- @InjectRedis('sessions') private readonly sessionRedis: Redis,
132
- @InjectRedis() private readonly defaultRedis: Redis, // Uses default connection
133
- ) {}
134
-
135
- async cacheData(key: string, value: string): Promise<void> {
136
- await this.cacheRedis.set(key, value);
137
- }
138
-
139
- async saveSession(sessionId: string, data: any): Promise<void> {
140
- await this.sessionRedis.set(sessionId, JSON.stringify(data), 'EX', 3600);
141
- }
142
- }
143
- ```
144
-
145
- ### Key Prefix
146
-
147
- The `keyPrefix` option automatically adds a prefix to all keys with proper colon formatting:
148
-
149
- ```typescript
150
- RedisModule.forRoot({
151
- hostname: 'localhost',
152
- port: 6379,
153
- username: 'default',
154
- password: 'password',
155
- keyPrefix: 'myapp', // Will become 'myapp:'
156
- isDefault: true,
157
- })
158
- ```
159
-
160
- Now when you use Redis commands:
161
-
162
- ```typescript
163
- await redis.set('user:123', 'data'); // Actually stored as 'myapp:user:123'
164
- ```
165
-
166
- ### Lazy Connection
167
-
168
- Enable lazy connection to defer the actual Redis connection until the first command:
169
-
170
- ```typescript
171
- RedisModule.forRoot({
172
- hostname: 'localhost',
173
- port: 6379,
174
- username: 'default',
175
- password: 'password',
176
- lazy: true,
177
- isDefault: true,
178
- })
179
- ```
180
-
181
- ## Automatic Cleanup
182
-
183
- The module automatically closes all Redis connections when the NestJS application shuts down. No manual cleanup is required.
184
-
185
- ## API Reference
186
-
187
- ### `RedisModule`
188
-
189
- #### `forRoot(params: RedisForRootParams): DynamicModule`
190
-
191
- Configures the Redis module with the provided connection parameters.
192
-
193
- ### `@InjectRedis(connectionName?: string)`
194
-
195
- Decorator for injecting Redis instances into your services. If no connection name is provided, the default connection will be injected.
196
-
197
- ### `RedisForRootParams`
198
-
199
- TypeScript interface for configuring Redis connections. See [Configuration](#configuration) section for details.
200
-
201
32
  ## License
202
33
 
203
34
  MIT
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RedisLock = exports.InjectRedis = exports.RedisModule = void 0;
3
+ exports.RedisLock = exports.RedisLockFactory = exports.RedisModule = void 0;
4
4
  var redis_module_1 = require("./redis.module");
5
5
  Object.defineProperty(exports, "RedisModule", { enumerable: true, get: function () { return redis_module_1.RedisModule; } });
6
- var redis_decorators_1 = require("./redis.decorators");
7
- Object.defineProperty(exports, "InjectRedis", { enumerable: true, get: function () { return redis_decorators_1.InjectRedis; } });
6
+ var redis_lock_factory_1 = require("./redis-lock.factory");
7
+ Object.defineProperty(exports, "RedisLockFactory", { enumerable: true, get: function () { return redis_lock_factory_1.RedisLockFactory; } });
8
8
  var redis_lock_1 = require("./redis-lock");
9
9
  Object.defineProperty(exports, "RedisLock", { enumerable: true, get: function () { return redis_lock_1.RedisLock; } });
@@ -0,0 +1,70 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ var RedisCoreModule_1;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.RedisCoreModule = void 0;
14
+ const common_1 = require("@nestjs/common");
15
+ const ioredis_1 = __importDefault(require("ioredis"));
16
+ const redis_consts_1 = require("./redis.consts");
17
+ const redis_helpers_1 = require("./redis.helpers");
18
+ const redis_lock_factory_1 = require("./redis-lock.factory");
19
+ let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
20
+ /**
21
+ * @param {RedisRegisterOptions} options
22
+ * @return {DynamicModule}
23
+ */
24
+ static register(options) {
25
+ const optionsProvider = this.createOptionsProvider(options), redisProvider = this.createRedisProvider();
26
+ return {
27
+ module: RedisCoreModule_1,
28
+ providers: [
29
+ optionsProvider,
30
+ redisProvider,
31
+ redis_lock_factory_1.RedisLockFactory
32
+ ],
33
+ exports: [
34
+ redisProvider,
35
+ redis_lock_factory_1.RedisLockFactory
36
+ ]
37
+ };
38
+ }
39
+ /**
40
+ * @param {RedisRegisterOptions} options
41
+ * @return {ValueProvider}
42
+ * @private
43
+ */
44
+ static createOptionsProvider(options) {
45
+ return {
46
+ provide: redis_consts_1.REDIS_OPTIONS_TOKEN,
47
+ useValue: {
48
+ ...options,
49
+ keyPrefix: (0, redis_helpers_1.resolveKeyPrefix)(options.keyPrefix),
50
+ locksHashKey: options.locksHashKey ?? redis_consts_1.REDIS_DEFAULT_LOCKS_HASH_KEY
51
+ }
52
+ };
53
+ }
54
+ /**
55
+ * @return {FactoryProvider}
56
+ * @private
57
+ */
58
+ static createRedisProvider() {
59
+ return {
60
+ provide: ioredis_1.default,
61
+ useFactory: (options) => new ioredis_1.default(options),
62
+ inject: [redis_consts_1.REDIS_OPTIONS_TOKEN]
63
+ };
64
+ }
65
+ };
66
+ exports.RedisCoreModule = RedisCoreModule;
67
+ exports.RedisCoreModule = RedisCoreModule = RedisCoreModule_1 = __decorate([
68
+ (0, common_1.Global)(),
69
+ (0, common_1.Module)({})
70
+ ], RedisCoreModule);
@@ -0,0 +1,47 @@
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 __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var __importDefault = (this && this.__importDefault) || function (mod) {
15
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.RedisLockFactory = void 0;
19
+ const common_1 = require("@nestjs/common");
20
+ const ioredis_1 = __importDefault(require("ioredis"));
21
+ const redis_consts_1 = require("./redis.consts");
22
+ const redis_lock_1 = require("./redis-lock");
23
+ let RedisLockFactory = class RedisLockFactory {
24
+ options;
25
+ redis;
26
+ /**
27
+ * @param {RedisRegisterOptions} options
28
+ * @param {Redis} redis
29
+ */
30
+ constructor(options, redis) {
31
+ this.options = options;
32
+ this.redis = redis;
33
+ }
34
+ /**
35
+ * @param {string} name
36
+ * @return {RedisLock}
37
+ */
38
+ create(name) {
39
+ return new redis_lock_1.RedisLock(this.redis, name, this.options.locksHashKey);
40
+ }
41
+ };
42
+ exports.RedisLockFactory = RedisLockFactory;
43
+ exports.RedisLockFactory = RedisLockFactory = __decorate([
44
+ (0, common_1.Injectable)(),
45
+ __param(0, (0, common_1.Inject)(redis_consts_1.REDIS_OPTIONS_TOKEN)),
46
+ __metadata("design:paramtypes", [Object, ioredis_1.default])
47
+ ], RedisLockFactory);
@@ -7,7 +7,6 @@ exports.RedisLock = void 0;
7
7
  const node_crypto_1 = require("node:crypto");
8
8
  const promises_1 = require("node:timers/promises");
9
9
  const dedent_1 = __importDefault(require("dedent"));
10
- const redis_consts_1 = require("./redis.consts");
11
10
  const redis_helpers_1 = require("./redis.helpers");
12
11
  class RedisLock {
13
12
  redis;
@@ -18,11 +17,13 @@ class RedisLock {
18
17
  /**
19
18
  * @param {Redis} redis
20
19
  * @param {string} name
20
+ * @param {string} locksHashKey
21
+ * @protected
21
22
  */
22
- constructor(redis, name) {
23
+ constructor(redis, name, locksHashKey) {
23
24
  this.redis = redis;
24
25
  this.name = name;
25
- this.locksHashKey = this.redis?.[redis_consts_1.REDIS_LOCKS_HASH_KEY_PROP_NAME] ?? redis_consts_1.REDIS_DEFAULT_LOCKS_HASH_KEY;
26
+ this.locksHashKey = locksHashKey;
26
27
  this.value = (0, node_crypto_1.randomUUID)();
27
28
  }
28
29
  /**
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.REDIS_DEFAULT_LOCK_ACQUIRE_RETRY_DELAY_MS = exports.REDIS_DEFAULT_LOCKS_HASH_KEY = exports.REDIS_OPTIONS_TOKEN = void 0;
4
+ exports.REDIS_OPTIONS_TOKEN = Symbol('REDIS_OPTIONS');
5
+ exports.REDIS_DEFAULT_LOCKS_HASH_KEY = '__locks__';
6
+ exports.REDIS_DEFAULT_LOCK_ACQUIRE_RETRY_DELAY_MS = 500;
@@ -1,29 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getRetryInterval = exports.resolveKeyPrefix = void 0;
4
- exports.resolveConnectionName = resolveConnectionName;
5
- exports.getRedisToken = getRedisToken;
6
4
  const redis_consts_1 = require("./redis.consts");
7
- /**
8
- * @param {string | RedisForRootParams} arg
9
- * @return {string}
10
- */
11
- function resolveConnectionName(arg) {
12
- if (typeof arg === 'string')
13
- return arg.toUpperCase();
14
- if (arg.connectionName && arg.connectionName.trim() !== '')
15
- return arg.connectionName.trim().toUpperCase();
16
- return `${arg.hostname}:${arg.port}/db${arg.db ?? 0}>${arg.keyPrefix ?? ''}`.toUpperCase();
17
- }
18
- /**
19
- * @param {string | RedisForRootParams} arg
20
- * @return {string}
21
- */
22
- function getRedisToken(arg) {
23
- if (typeof arg === 'string')
24
- return `${redis_consts_1.REDIS_INSTANCE_TOKEN_PREFIX}${arg}`.toUpperCase();
25
- return `${redis_consts_1.REDIS_INSTANCE_TOKEN_PREFIX}${resolveConnectionName(arg)}`.toUpperCase();
26
- }
27
5
  /**
28
6
  * @param {string} prefix
29
7
  * @return {string}
@@ -0,0 +1,30 @@
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 RedisModule_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.RedisModule = void 0;
11
+ const common_1 = require("@nestjs/common");
12
+ const redis_core_module_1 = require("./redis-core.module");
13
+ let RedisModule = RedisModule_1 = class RedisModule {
14
+ /**
15
+ * @param {RedisRegisterOptions} options
16
+ * @return {DynamicModule}
17
+ */
18
+ static register(options) {
19
+ return {
20
+ module: RedisModule_1,
21
+ imports: [
22
+ redis_core_module_1.RedisCoreModule.register(options)
23
+ ]
24
+ };
25
+ }
26
+ };
27
+ exports.RedisModule = RedisModule;
28
+ exports.RedisModule = RedisModule = RedisModule_1 = __decorate([
29
+ (0, common_1.Module)({})
30
+ ], RedisModule);
@@ -1,4 +1,4 @@
1
1
  export { RedisModule } from './redis.module';
2
- export type { RedisForRootParams } from './redis.types';
3
- export { InjectRedis } from './redis.decorators';
2
+ export type { RedisRegisterOptions } from './redis.types';
3
+ export { RedisLockFactory } from './redis-lock.factory';
4
4
  export { RedisLock, type RedisLockWaitAcquireParams } from './redis-lock';
@@ -1,13 +1,18 @@
1
1
  import { type DynamicModule } from '@nestjs/common';
2
- import type { RedisForRootParams } from './redis.types';
2
+ import type { RedisRegisterOptions } from './redis.types';
3
3
  export declare class RedisCoreModule {
4
4
  /**
5
- * @param {RedisForRootParams} params
5
+ * @param {RedisRegisterOptions} options
6
6
  * @return {DynamicModule}
7
7
  */
8
- static forRoot(params: RedisForRootParams): DynamicModule;
8
+ static register(options: RedisRegisterOptions): DynamicModule;
9
+ /**
10
+ * @param {RedisRegisterOptions} options
11
+ * @return {ValueProvider}
12
+ * @private
13
+ */
14
+ private static createOptionsProvider;
9
15
  /**
10
- * @param {RedisForRootParams} params
11
16
  * @return {FactoryProvider}
12
17
  * @private
13
18
  */
@@ -1,4 +1,4 @@
1
- import { type Redis } from 'ioredis';
1
+ import Redis from 'ioredis';
2
2
  export type RedisLockWaitAcquireParams = {
3
3
  lockTTL: number;
4
4
  retryInterval?: number | number[] | ((iteration: number) => number);
@@ -18,8 +18,10 @@ export declare class RedisLock {
18
18
  /**
19
19
  * @param {Redis} redis
20
20
  * @param {string} name
21
+ * @param {string} locksHashKey
22
+ * @protected
21
23
  */
22
- constructor(redis: Redis, name: string);
24
+ constructor(redis: Redis, name: string, locksHashKey: string);
23
25
  /**
24
26
  * @param {RedisLockWaitAcquireParams} params
25
27
  * @return {Promise<boolean>}
@@ -0,0 +1,17 @@
1
+ import Redis from 'ioredis';
2
+ import type { RedisRegisterOptions } from './redis.types';
3
+ import { RedisLock } from './redis-lock';
4
+ export declare class RedisLockFactory {
5
+ private readonly options;
6
+ private readonly redis;
7
+ /**
8
+ * @param {RedisRegisterOptions} options
9
+ * @param {Redis} redis
10
+ */
11
+ constructor(options: RedisRegisterOptions, redis: Redis);
12
+ /**
13
+ * @param {string} name
14
+ * @return {RedisLock}
15
+ */
16
+ create(name: string): RedisLock;
17
+ }
@@ -1,5 +1,3 @@
1
- export declare const REDIS_INSTANCE_TOKEN_PREFIX: "REDIS_INSTANCE_";
2
- export declare const REDIS_DEFAULT_CONNECTION_NAME = "default";
3
- export declare const REDIS_LOCKS_HASH_KEY_PROP_NAME = "__$__locksHashKey__";
1
+ export declare const REDIS_OPTIONS_TOKEN: unique symbol;
4
2
  export declare const REDIS_DEFAULT_LOCKS_HASH_KEY = "__locks__";
5
- export declare const REDIS_DEFAULT_LOCK_ACQUIRE_RETRY_DELAY_MS = 100;
3
+ export declare const REDIS_DEFAULT_LOCK_ACQUIRE_RETRY_DELAY_MS = 500;
@@ -1,9 +1,4 @@
1
- import type { RedisForRootParams } from './redis.types';
2
1
  import { type RedisLockWaitAcquireParams } from './redis-lock';
3
- export declare function resolveConnectionName(instanceName: string): string;
4
- export declare function resolveConnectionName(params: RedisForRootParams): string;
5
- export declare function getRedisToken(connectionName: string): string;
6
- export declare function getRedisToken(params: RedisForRootParams): string;
7
2
  /**
8
3
  * @param {string} prefix
9
4
  * @return {string}
@@ -1,9 +1,9 @@
1
1
  import { type DynamicModule } from '@nestjs/common';
2
- import type { RedisForRootParams } from './redis.types';
2
+ import type { RedisRegisterOptions } from './redis.types';
3
3
  export declare class RedisModule {
4
4
  /**
5
- * @param {RedisForRootParams} params
5
+ * @param {RedisRegisterOptions} options
6
6
  * @return {DynamicModule}
7
7
  */
8
- static forRoot(params: RedisForRootParams): DynamicModule;
8
+ static register(options: RedisRegisterOptions): DynamicModule;
9
9
  }
@@ -1,13 +1,5 @@
1
- export type RedisForRootParams = {
2
- connectionName?: string;
3
- hostname: string;
4
- port: number;
5
- username: string;
6
- password: string;
1
+ import { type RedisOptions } from 'ioredis';
2
+ export type RedisRegisterOptions = RedisOptions & {
7
3
  keyPrefix?: string;
8
- db?: number;
9
- connectionTimeout?: number;
10
- lazy?: boolean;
11
- isDefault?: boolean;
12
4
  locksHashKey?: string;
13
5
  };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@jsnw/nestjs-ioredis",
3
- "version": "1.3.0",
4
- "description": "NestJS module for integrating Redis using ioredis with support for multiple connections and dependency injection",
5
- "main": "./dist/index.js",
3
+ "version": "2.0.0",
4
+ "description": "NestJS module for integrating Redis",
5
+ "main": "./dist/lib/index.js",
6
6
  "types": "./dist/types/index.d.ts",
7
7
  "exports": {
8
8
  ".": {
9
- "require": "./dist/index.js",
9
+ "require": "./dist/lib/index.js",
10
10
  "types": "./dist/types/index.d.ts"
11
11
  }
12
12
  },
@@ -1,115 +0,0 @@
1
- "use strict";
2
- var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
- function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
- var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
- var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
- var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
- var _, done = false;
8
- for (var i = decorators.length - 1; i >= 0; i--) {
9
- var context = {};
10
- for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
- for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
- context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
- var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
- if (kind === "accessor") {
15
- if (result === void 0) continue;
16
- if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
- if (_ = accept(result.get)) descriptor.get = _;
18
- if (_ = accept(result.set)) descriptor.set = _;
19
- if (_ = accept(result.init)) initializers.unshift(_);
20
- }
21
- else if (_ = accept(result)) {
22
- if (kind === "field") initializers.unshift(_);
23
- else descriptor[key] = _;
24
- }
25
- }
26
- if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
- done = true;
28
- };
29
- var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
- var useValue = arguments.length > 2;
31
- for (var i = 0; i < initializers.length; i++) {
32
- value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
- }
34
- return useValue ? value : void 0;
35
- };
36
- Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.RedisCoreModule = void 0;
38
- const common_1 = require("@nestjs/common");
39
- const ioredis_1 = require("ioredis");
40
- const redis_instances_manager_1 = require("./redis-instances-manager");
41
- const redis_consts_1 = require("./redis.consts");
42
- const redis_helpers_1 = require("./redis.helpers");
43
- let RedisCoreModule = (() => {
44
- let _classDecorators = [(0, common_1.Global)(), (0, common_1.Module)({
45
- providers: [
46
- redis_instances_manager_1.RedisInstancesManager
47
- ]
48
- })];
49
- let _classDescriptor;
50
- let _classExtraInitializers = [];
51
- let _classThis;
52
- var RedisCoreModule = class {
53
- static { _classThis = this; }
54
- static {
55
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
56
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
57
- RedisCoreModule = _classThis = _classDescriptor.value;
58
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
59
- __runInitializers(_classThis, _classExtraInitializers);
60
- }
61
- /**
62
- * @param {RedisForRootParams} params
63
- * @return {DynamicModule}
64
- */
65
- static forRoot(params) {
66
- const providers = [];
67
- const redisProvider = this.createRedisProvider(params);
68
- providers.push(redisProvider);
69
- if (!!params.isDefault)
70
- providers.push({
71
- provide: (0, redis_helpers_1.getRedisToken)(redis_consts_1.REDIS_DEFAULT_CONNECTION_NAME),
72
- useExisting: (0, redis_helpers_1.getRedisToken)(params)
73
- });
74
- return {
75
- module: RedisCoreModule,
76
- providers: providers,
77
- exports: providers
78
- };
79
- }
80
- /**
81
- * @param {RedisForRootParams} params
82
- * @return {FactoryProvider}
83
- * @private
84
- */
85
- static createRedisProvider(params) {
86
- const redisToken = (0, redis_helpers_1.getRedisToken)(params), keyPrefix = (0, redis_helpers_1.resolveKeyPrefix)(params.keyPrefix);
87
- return {
88
- provide: redisToken,
89
- useFactory: (im) => {
90
- const existingInstance = im.getInstance(redisToken);
91
- if (existingInstance)
92
- return existingInstance;
93
- const instance = new ioredis_1.Redis({
94
- host: params.hostname,
95
- port: params.port,
96
- username: params.username,
97
- password: params.password,
98
- db: params.db ?? 0,
99
- connectTimeout: params.connectionTimeout ?? 10_000,
100
- lazyConnect: !!params.lazy,
101
- keyPrefix: keyPrefix
102
- });
103
- instance[redis_consts_1.REDIS_LOCKS_HASH_KEY_PROP_NAME] = params.locksHashKey;
104
- im.addInstance(redisToken, instance);
105
- return instance;
106
- },
107
- inject: [
108
- redis_instances_manager_1.RedisInstancesManager
109
- ]
110
- };
111
- }
112
- };
113
- return RedisCoreModule = _classThis;
114
- })();
115
- exports.RedisCoreModule = RedisCoreModule;
@@ -1,101 +0,0 @@
1
- "use strict";
2
- var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
- function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
- var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
- var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
- var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
- var _, done = false;
8
- for (var i = decorators.length - 1; i >= 0; i--) {
9
- var context = {};
10
- for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
- for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
- context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
- var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
- if (kind === "accessor") {
15
- if (result === void 0) continue;
16
- if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
- if (_ = accept(result.get)) descriptor.get = _;
18
- if (_ = accept(result.set)) descriptor.set = _;
19
- if (_ = accept(result.init)) initializers.unshift(_);
20
- }
21
- else if (_ = accept(result)) {
22
- if (kind === "field") initializers.unshift(_);
23
- else descriptor[key] = _;
24
- }
25
- }
26
- if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
- done = true;
28
- };
29
- var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
- var useValue = arguments.length > 2;
31
- for (var i = 0; i < initializers.length; i++) {
32
- value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
- }
34
- return useValue ? value : void 0;
35
- };
36
- Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.RedisInstancesManager = void 0;
38
- const common_1 = require("@nestjs/common");
39
- const redis_helpers_1 = require("./redis.helpers");
40
- let RedisInstancesManager = (() => {
41
- let _classDecorators = [(0, common_1.Injectable)()];
42
- let _classDescriptor;
43
- let _classExtraInitializers = [];
44
- let _classThis;
45
- var RedisInstancesManager = class {
46
- static { _classThis = this; }
47
- static {
48
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
49
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
50
- RedisInstancesManager = _classThis = _classDescriptor.value;
51
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
52
- __runInitializers(_classThis, _classExtraInitializers);
53
- }
54
- /**
55
- * @type {Map<string, Redis>}
56
- * @private
57
- */
58
- instances = new Map();
59
- /**
60
- * @param {string | RedisForRootParams} token
61
- * @param {Redis} instance
62
- */
63
- addInstance(token, instance) {
64
- token = typeof token === 'string' ? token : (0, redis_helpers_1.getRedisToken)(token);
65
- if (this.instances.has(token))
66
- return;
67
- this.instances.set(token, instance);
68
- }
69
- /**
70
- * @param {string | RedisForRootParams} token
71
- * @return {Redis | null}
72
- */
73
- getInstance(token) {
74
- token = typeof token === 'string' ? token : (0, redis_helpers_1.getRedisToken)(token);
75
- return this.instances.get(token) ?? null;
76
- }
77
- /**
78
- * @param {string | RedisForRootParams} token
79
- * @return {boolean}
80
- */
81
- hasInstance(token) {
82
- token = typeof token === 'string' ? token : (0, redis_helpers_1.getRedisToken)(token);
83
- return this.instances.has(token);
84
- }
85
- /**
86
- * @return {Redis[]}
87
- */
88
- getAllInstances() {
89
- return Array.from(this.instances.values());
90
- }
91
- /**
92
- * @return {Promise<void>}
93
- */
94
- async onApplicationShutdown() {
95
- await Promise.allSettled(this.getAllInstances()
96
- .map(instance => instance.quit()));
97
- }
98
- };
99
- return RedisInstancesManager = _classThis;
100
- })();
101
- exports.RedisInstancesManager = RedisInstancesManager;
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.REDIS_DEFAULT_LOCK_ACQUIRE_RETRY_DELAY_MS = exports.REDIS_DEFAULT_LOCKS_HASH_KEY = exports.REDIS_LOCKS_HASH_KEY_PROP_NAME = exports.REDIS_DEFAULT_CONNECTION_NAME = exports.REDIS_INSTANCE_TOKEN_PREFIX = void 0;
4
- exports.REDIS_INSTANCE_TOKEN_PREFIX = 'REDIS_INSTANCE_';
5
- exports.REDIS_DEFAULT_CONNECTION_NAME = 'default';
6
- exports.REDIS_LOCKS_HASH_KEY_PROP_NAME = '__$__locksHashKey__';
7
- exports.REDIS_DEFAULT_LOCKS_HASH_KEY = '__locks__';
8
- exports.REDIS_DEFAULT_LOCK_ACQUIRE_RETRY_DELAY_MS = 100;
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InjectRedis = void 0;
4
- const common_1 = require("@nestjs/common");
5
- const redis_consts_1 = require("./redis.consts");
6
- const redis_helpers_1 = require("./redis.helpers");
7
- /**
8
- * @param {string} [connectionName = REDIS_DEFAULT_CONNECTION_NAME]
9
- * @return {PropertyDecorator & ParameterDecorator}
10
- * @constructor
11
- */
12
- const InjectRedis = (connectionName = redis_consts_1.REDIS_DEFAULT_CONNECTION_NAME) => (0, common_1.Inject)((0, redis_helpers_1.getRedisToken)(connectionName));
13
- exports.InjectRedis = InjectRedis;
@@ -1,69 +0,0 @@
1
- "use strict";
2
- var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
- function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
- var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
- var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
- var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
- var _, done = false;
8
- for (var i = decorators.length - 1; i >= 0; i--) {
9
- var context = {};
10
- for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
- for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
- context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
- var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
- if (kind === "accessor") {
15
- if (result === void 0) continue;
16
- if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
- if (_ = accept(result.get)) descriptor.get = _;
18
- if (_ = accept(result.set)) descriptor.set = _;
19
- if (_ = accept(result.init)) initializers.unshift(_);
20
- }
21
- else if (_ = accept(result)) {
22
- if (kind === "field") initializers.unshift(_);
23
- else descriptor[key] = _;
24
- }
25
- }
26
- if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
- done = true;
28
- };
29
- var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
- var useValue = arguments.length > 2;
31
- for (var i = 0; i < initializers.length; i++) {
32
- value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
- }
34
- return useValue ? value : void 0;
35
- };
36
- Object.defineProperty(exports, "__esModule", { value: true });
37
- exports.RedisModule = void 0;
38
- const common_1 = require("@nestjs/common");
39
- const redis_core_module_1 = require("./redis-core.module");
40
- let RedisModule = (() => {
41
- let _classDecorators = [(0, common_1.Module)({})];
42
- let _classDescriptor;
43
- let _classExtraInitializers = [];
44
- let _classThis;
45
- var RedisModule = class {
46
- static { _classThis = this; }
47
- static {
48
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
49
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
50
- RedisModule = _classThis = _classDescriptor.value;
51
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
52
- __runInitializers(_classThis, _classExtraInitializers);
53
- }
54
- /**
55
- * @param {RedisForRootParams} params
56
- * @return {DynamicModule}
57
- */
58
- static forRoot(params) {
59
- return {
60
- module: RedisModule,
61
- imports: [
62
- redis_core_module_1.RedisCoreModule.forRoot(params)
63
- ]
64
- };
65
- }
66
- };
67
- return RedisModule = _classThis;
68
- })();
69
- exports.RedisModule = RedisModule;
@@ -1,24 +0,0 @@
1
- import { type OnApplicationShutdown } from '@nestjs/common';
2
- import { type Redis } from 'ioredis';
3
- import type { RedisForRootParams } from './redis.types';
4
- export declare class RedisInstancesManager implements OnApplicationShutdown {
5
- /**
6
- * @type {Map<string, Redis>}
7
- * @private
8
- */
9
- private readonly instances;
10
- addInstance(resolvedToken: string, instance: Redis): void;
11
- addInstance(unresolvedToken: RedisForRootParams, instance: Redis): void;
12
- getInstance(resolvedToken: string): Redis | null;
13
- getInstance(unresolvedToken: RedisForRootParams): Redis | null;
14
- hasInstance(resolvedToken: string): boolean;
15
- hasInstance(unresolvedToken: RedisForRootParams): boolean;
16
- /**
17
- * @return {Redis[]}
18
- */
19
- getAllInstances(): Redis[];
20
- /**
21
- * @return {Promise<void>}
22
- */
23
- onApplicationShutdown(): Promise<void>;
24
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * @param {string} [connectionName = REDIS_DEFAULT_CONNECTION_NAME]
3
- * @return {PropertyDecorator & ParameterDecorator}
4
- * @constructor
5
- */
6
- export declare const InjectRedis: (connectionName?: string) => PropertyDecorator & ParameterDecorator;
File without changes