@jsnw/nestjs-ioredis 1.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 +207 -0
- package/dist/index.js +7 -0
- package/dist/redis-core.module.js +114 -0
- package/dist/redis-instances-manager.js +101 -0
- package/dist/redis.consts.js +5 -0
- package/dist/redis.decorators.js +13 -0
- package/dist/redis.helpers.js +42 -0
- package/dist/redis.module.js +69 -0
- package/dist/redis.types.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/redis-core.module.d.ts +15 -0
- package/dist/types/redis-instances-manager.d.ts +24 -0
- package/dist/types/redis.consts.d.ts +2 -0
- package/dist/types/redis.decorators.d.ts +6 -0
- package/dist/types/redis.helpers.d.ts +10 -0
- package/dist/types/redis.module.d.ts +9 -0
- package/dist/types/redis.types.d.ts +13 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# @jsnw/nestjs-ioredis
|
|
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
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm i -s @jsnw/nestjs-ioredis ioredis
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### Basic Usage
|
|
23
|
+
|
|
24
|
+
Import `RedisModule` in your app module and configure it with `forRoot()`:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { Module } from '@nestjs/common';
|
|
28
|
+
import { RedisModule } from '@jsnw/nestjs-ioredis';
|
|
29
|
+
|
|
30
|
+
@Module({
|
|
31
|
+
imports: [
|
|
32
|
+
RedisModule.forRoot({
|
|
33
|
+
hostname: 'localhost',
|
|
34
|
+
port: 6379,
|
|
35
|
+
username: 'default',
|
|
36
|
+
password: 'your-password',
|
|
37
|
+
isDefault: true,
|
|
38
|
+
}),
|
|
39
|
+
],
|
|
40
|
+
})
|
|
41
|
+
export class AppModule {}
|
|
42
|
+
```
|
|
43
|
+
|
|
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
|
+
## License
|
|
202
|
+
|
|
203
|
+
MIT
|
|
204
|
+
|
|
205
|
+
## Author
|
|
206
|
+
|
|
207
|
+
Pavlo Baliuk (jsnow0177@gmail.com)
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectRedis = exports.RedisModule = void 0;
|
|
4
|
+
var redis_module_1 = require("./redis.module");
|
|
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; } });
|
|
@@ -0,0 +1,114 @@
|
|
|
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
|
+
im.addInstance(redisToken, instance);
|
|
104
|
+
return instance;
|
|
105
|
+
},
|
|
106
|
+
inject: [
|
|
107
|
+
redis_instances_manager_1.RedisInstancesManager
|
|
108
|
+
]
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return RedisCoreModule = _classThis;
|
|
113
|
+
})();
|
|
114
|
+
exports.RedisCoreModule = RedisCoreModule;
|
|
@@ -0,0 +1,101 @@
|
|
|
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;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
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';
|
|
@@ -0,0 +1,13 @@
|
|
|
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;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveKeyPrefix = void 0;
|
|
4
|
+
exports.resolveConnectionName = resolveConnectionName;
|
|
5
|
+
exports.getRedisToken = getRedisToken;
|
|
6
|
+
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
|
+
/**
|
|
28
|
+
* @param {string} prefix
|
|
29
|
+
* @return {string}
|
|
30
|
+
*/
|
|
31
|
+
const resolveKeyPrefix = (prefix) => {
|
|
32
|
+
if (!prefix)
|
|
33
|
+
return '';
|
|
34
|
+
prefix = prefix.trim();
|
|
35
|
+
if (prefix.endsWith(':')) {
|
|
36
|
+
if (prefix.length === 1)
|
|
37
|
+
return '';
|
|
38
|
+
return prefix;
|
|
39
|
+
}
|
|
40
|
+
return prefix + ':';
|
|
41
|
+
};
|
|
42
|
+
exports.resolveKeyPrefix = resolveKeyPrefix;
|
|
@@ -0,0 +1,69 @@
|
|
|
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;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import type { RedisForRootParams } from './redis.types';
|
|
3
|
+
export declare class RedisCoreModule {
|
|
4
|
+
/**
|
|
5
|
+
* @param {RedisForRootParams} params
|
|
6
|
+
* @return {DynamicModule}
|
|
7
|
+
*/
|
|
8
|
+
static forRoot(params: RedisForRootParams): DynamicModule;
|
|
9
|
+
/**
|
|
10
|
+
* @param {RedisForRootParams} params
|
|
11
|
+
* @return {FactoryProvider}
|
|
12
|
+
* @private
|
|
13
|
+
*/
|
|
14
|
+
private static createRedisProvider;
|
|
15
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RedisForRootParams } from './redis.types';
|
|
2
|
+
export declare function resolveConnectionName(instanceName: string): string;
|
|
3
|
+
export declare function resolveConnectionName(params: RedisForRootParams): string;
|
|
4
|
+
export declare function getRedisToken(connectionName: string): string;
|
|
5
|
+
export declare function getRedisToken(params: RedisForRootParams): string;
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} prefix
|
|
8
|
+
* @return {string}
|
|
9
|
+
*/
|
|
10
|
+
export declare const resolveKeyPrefix: (prefix?: string) => string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import type { RedisForRootParams } from './redis.types';
|
|
3
|
+
export declare class RedisModule {
|
|
4
|
+
/**
|
|
5
|
+
* @param {RedisForRootParams} params
|
|
6
|
+
* @return {DynamicModule}
|
|
7
|
+
*/
|
|
8
|
+
static forRoot(params: RedisForRootParams): DynamicModule;
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type RedisForRootParams = {
|
|
2
|
+
connectionName?: string;
|
|
3
|
+
hostname: string;
|
|
4
|
+
port: number;
|
|
5
|
+
username: string;
|
|
6
|
+
password: string;
|
|
7
|
+
keyPrefix?: string;
|
|
8
|
+
db?: number;
|
|
9
|
+
connectionTimeout?: number;
|
|
10
|
+
lazy?: boolean;
|
|
11
|
+
s: any;
|
|
12
|
+
isDefault?: boolean;
|
|
13
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jsnw/nestjs-ioredis",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "NestJS module for integrating Redis using ioredis with support for multiple connections and dependency injection",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/types/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": "./dist/index.js",
|
|
10
|
+
"types": "./dist/types/index.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"clean": "rimraf ./dist",
|
|
15
|
+
"build": "npm run clean && tsc -p tsconfig.json",
|
|
16
|
+
"test": "jest --passWithNoTests",
|
|
17
|
+
"test:watch": "jest --watch",
|
|
18
|
+
"test:coverage": "jest --coverage",
|
|
19
|
+
"prepublishOnly": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"./dist",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+ssh://git@github.com/pvbaliuk/jsnw-nestjs-ioredis"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/pvbaliuk/jsnw-nestjs-ioredis/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/pvbaliuk/jsnw-nestjs-ioredis#readme",
|
|
33
|
+
"keywords": [
|
|
34
|
+
"nestjs",
|
|
35
|
+
"redis",
|
|
36
|
+
"ioredis"
|
|
37
|
+
],
|
|
38
|
+
"author": {
|
|
39
|
+
"name": "Pavlo Baliuk",
|
|
40
|
+
"email": "jsnow0177@gmail.com"
|
|
41
|
+
},
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@nestjs/common": "^11.1.14",
|
|
45
|
+
"ioredis": "^5.9.3"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@nestjs/common": "^11.1.14",
|
|
49
|
+
"ioredis": "^5.9.3",
|
|
50
|
+
"jest": "^30.2.0",
|
|
51
|
+
"rimraf": "^6.1.3",
|
|
52
|
+
"ts-jest": "^29.4.6",
|
|
53
|
+
"typescript": "^5.9.3"
|
|
54
|
+
}
|
|
55
|
+
}
|