@nathapp/nestjs-redis 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +129 -0
- package/.nvmrc +1 -0
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +212 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/redis-client.provider.d.ts +16 -0
- package/dist/redis-client.provider.js +52 -0
- package/dist/redis-core.module.d.ts +11 -0
- package/dist/redis-core.module.js +71 -0
- package/dist/redis.constants.d.ts +2 -0
- package/dist/redis.constants.js +5 -0
- package/dist/redis.interface.d.ts +11 -0
- package/dist/redis.interface.js +2 -0
- package/dist/redis.module.d.ts +6 -0
- package/dist/redis.module.js +30 -0
- package/dist/redis.service.d.ts +8 -0
- package/dist/redis.service.js +41 -0
- package/index.d.ts +1 -0
- package/index.js +6 -0
- package/nodemon.json +6 -0
- package/package.json +68 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
parser: '@typescript-eslint/parser',
|
|
3
|
+
plugins: [
|
|
4
|
+
'@typescript-eslint',
|
|
5
|
+
'import',
|
|
6
|
+
],
|
|
7
|
+
extends: [
|
|
8
|
+
'plugin:@typescript-eslint/eslint-recommended',
|
|
9
|
+
'plugin:@typescript-eslint/recommended',
|
|
10
|
+
'plugin:jest/recommended',
|
|
11
|
+
'prettier/@typescript-eslint',
|
|
12
|
+
'prettier/react',
|
|
13
|
+
'plugin:prettier/recommended',
|
|
14
|
+
],
|
|
15
|
+
settings: {
|
|
16
|
+
'import/resolver': {
|
|
17
|
+
typescript: {
|
|
18
|
+
project: ['tsconfig.json', 'tsconfig.spec.json'],
|
|
19
|
+
},
|
|
20
|
+
node: {
|
|
21
|
+
extensions: ['.ts', '.tsx', '.js', '.jsx'],
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
'import/ignore': ['\\.coffee$', '\\.(scss|less|css)$', '\\.(svg|png|jpe?g|webp|gif)(\\?.*)?$'],
|
|
25
|
+
},
|
|
26
|
+
globals: {
|
|
27
|
+
window: true,
|
|
28
|
+
document: true,
|
|
29
|
+
process: true,
|
|
30
|
+
__DEV__: true,
|
|
31
|
+
__SERVER__: true,
|
|
32
|
+
},
|
|
33
|
+
parserOptions: {
|
|
34
|
+
sourceType: 'module',
|
|
35
|
+
jsx: true,
|
|
36
|
+
useJSXTextNode: true,
|
|
37
|
+
ecmaVersion: 2020,
|
|
38
|
+
ecmaFeatures: {
|
|
39
|
+
jsx: true,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
env: {
|
|
43
|
+
es6: true,
|
|
44
|
+
browser: true,
|
|
45
|
+
node: true,
|
|
46
|
+
jest: true,
|
|
47
|
+
},
|
|
48
|
+
rules: {
|
|
49
|
+
'jest/valid-title': 0,
|
|
50
|
+
'no-confusing-arrow': 0,
|
|
51
|
+
'@typescript-eslint/indent': 0,
|
|
52
|
+
'operator-linebreak': 0,
|
|
53
|
+
'function-paren-newline': 0,
|
|
54
|
+
'no-param-reassign': 0,
|
|
55
|
+
'comma-dangle': 0,
|
|
56
|
+
'object-curly-newline': 0,
|
|
57
|
+
'implicit-arrow-linebreak': 0,
|
|
58
|
+
'import/prefer-default-export': 0,
|
|
59
|
+
'class-methods-use-this': 0,
|
|
60
|
+
'lines-between-class-members': 0,
|
|
61
|
+
'quote-props': 0,
|
|
62
|
+
'indent': 0,
|
|
63
|
+
'no-nested-ternary': 0,
|
|
64
|
+
'spaced-comment': ['error', 'always', { markers: ['#region', '#endregion', '/'] }],
|
|
65
|
+
'max-len': ['error', { code: 140, ignoreUrls: true }],
|
|
66
|
+
'import/no-extraneous-dependencies': 0,
|
|
67
|
+
'no-unused-vars': [
|
|
68
|
+
'warn',
|
|
69
|
+
{
|
|
70
|
+
argsIgnorePattern: '^(_|[A-Z]+)',
|
|
71
|
+
varsIgnorePattern: '^(_|[A-Z]+)',
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
'import/extensions': [
|
|
75
|
+
'error',
|
|
76
|
+
'ignorePackages',
|
|
77
|
+
{
|
|
78
|
+
js: 'never',
|
|
79
|
+
jsx: 'never',
|
|
80
|
+
ts: 'never',
|
|
81
|
+
tsx: 'never',
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
'prettier/prettier': [
|
|
85
|
+
'error',
|
|
86
|
+
{
|
|
87
|
+
parser: 'typescript',
|
|
88
|
+
printWidth: 140,
|
|
89
|
+
singleQuote: true,
|
|
90
|
+
useTabs: false,
|
|
91
|
+
tabWidth: 2,
|
|
92
|
+
semi: true,
|
|
93
|
+
bracketSpacing: true,
|
|
94
|
+
trailingComma: 'all',
|
|
95
|
+
arrowParens: 'always',
|
|
96
|
+
insertPragma: true,
|
|
97
|
+
quoteProps: 'consistent',
|
|
98
|
+
jsxSingleQuote: false,
|
|
99
|
+
jsxBracketSameLine: false,
|
|
100
|
+
htmlWhitespaceSensivity: 'css',
|
|
101
|
+
proseWrap: 'never',
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
'no-empty-function': 0,
|
|
105
|
+
'no-shadow': 'off',
|
|
106
|
+
'@typescript-eslint/no-shadow': ['error'],
|
|
107
|
+
'@typescript-eslint/no-explicit-any': 'warn',
|
|
108
|
+
'@typescript-eslint/no-unused-vars': [
|
|
109
|
+
'warn',
|
|
110
|
+
{
|
|
111
|
+
argsIgnorePattern: '^(_|[A-Z]+)',
|
|
112
|
+
varsIgnorePattern: '^(_|[A-Z]+)',
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
'react/require-default-props': 0,
|
|
116
|
+
'react/jsx-curly-newline': 0,
|
|
117
|
+
'no-use-before-define': 0,
|
|
118
|
+
'jsx-a11y/anchor-is-valid': 0,
|
|
119
|
+
'no-useless-constructor': 0,
|
|
120
|
+
'react/jsx-one-expression-per-line': 0,
|
|
121
|
+
'react/jsx-wrap-multilines': 0,
|
|
122
|
+
'react/prop-types': 0,
|
|
123
|
+
'react/static-property-placement': 0,
|
|
124
|
+
'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx', '.tsx', '.ts'] }],
|
|
125
|
+
'react/jsx-props-no-spreading': 0,
|
|
126
|
+
// '@typescript-eslint/no-var-requires': 0,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v16.14.2
|
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 KnowYourself
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
|
|
2
|
+
# NOTES
|
|
3
|
+
this is fork from https://github.com/skunight/nestjs-redis and upgrade to nestjs 8
|
|
4
|
+
|
|
5
|
+
# Nestjs Redis
|
|
6
|
+
|
|
7
|
+
Redis component for NestJs.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Installation
|
|
11
|
+
|
|
12
|
+
**Yarn**
|
|
13
|
+
```bash
|
|
14
|
+
yarn add nestjs-redis
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**NPM**
|
|
18
|
+
```bash
|
|
19
|
+
npm install nestjs-redis --save
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Getting Started
|
|
23
|
+
Let's register the RedisModule in `app.module.ts`
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { Module } from '@nestjs/common'
|
|
27
|
+
import { RedisModule} from 'nestjs-redis'
|
|
28
|
+
|
|
29
|
+
@Module({
|
|
30
|
+
imports: [
|
|
31
|
+
RedisModule.register(options)
|
|
32
|
+
],
|
|
33
|
+
})
|
|
34
|
+
export class AppModule {}
|
|
35
|
+
```
|
|
36
|
+
With Async
|
|
37
|
+
```typescript
|
|
38
|
+
import { Module } from '@nestjs/common';
|
|
39
|
+
import { RedisModule} from 'nestjs-redis'
|
|
40
|
+
|
|
41
|
+
@Module({
|
|
42
|
+
imports: [
|
|
43
|
+
RedisModule.forRootAsync({
|
|
44
|
+
useFactory: (configService: ConfigService) => configService.get('redis'), // or use async method
|
|
45
|
+
//useFactory: async (configService: ConfigService) => configService.get('redis'),
|
|
46
|
+
inject:[ConfigService]
|
|
47
|
+
}),
|
|
48
|
+
],
|
|
49
|
+
})
|
|
50
|
+
export class AppModule {}
|
|
51
|
+
```
|
|
52
|
+
And the config file look like this
|
|
53
|
+
With single client
|
|
54
|
+
```typescript
|
|
55
|
+
export default {
|
|
56
|
+
host: process.env.REDIS_HOST,
|
|
57
|
+
port: parseInt(process.env.REDIS_PORT),
|
|
58
|
+
db: parseInt(process.env.REDIS_DB),
|
|
59
|
+
password: process.env.REDIS_PASSWORD,
|
|
60
|
+
keyPrefix: process.env.REDIS_PRIFIX,
|
|
61
|
+
}
|
|
62
|
+
Or
|
|
63
|
+
export default {
|
|
64
|
+
url: 'redis://:authpassword@127.0.0.1:6380/4',
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
With custom error handler
|
|
68
|
+
```typescript
|
|
69
|
+
export default {
|
|
70
|
+
url: 'redis://:authpassword@127.0.0.1:6380/4',
|
|
71
|
+
onClientReady: (client) => {
|
|
72
|
+
client.on('error', (err) => {}
|
|
73
|
+
)},
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
With multi client
|
|
77
|
+
```typescript
|
|
78
|
+
export default [
|
|
79
|
+
{
|
|
80
|
+
name:'test1',
|
|
81
|
+
url: 'redis://:authpassword@127.0.0.1:6380/4',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name:'test2',
|
|
85
|
+
host: process.env.REDIS_HOST,
|
|
86
|
+
port: parseInt(process.env.REDIS_PORT),
|
|
87
|
+
db: parseInt(process.env.REDIS_DB),
|
|
88
|
+
password: process.env.REDIS_PASSWORD,
|
|
89
|
+
keyPrefix: process.env.REDIS_PRIFIX,
|
|
90
|
+
},
|
|
91
|
+
]
|
|
92
|
+
```
|
|
93
|
+
And use in your service
|
|
94
|
+
```typescript
|
|
95
|
+
import { Injectable } from '@nestjs/common';
|
|
96
|
+
import { RedisService } from 'nestjs-redis';
|
|
97
|
+
|
|
98
|
+
@Injectable()
|
|
99
|
+
export class TestService {
|
|
100
|
+
constructor(
|
|
101
|
+
private readonly redisService: RedisService,
|
|
102
|
+
) { }
|
|
103
|
+
async root(): Promise<boolean> {
|
|
104
|
+
const client = await this.redisService.getClient('test')
|
|
105
|
+
return true
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
Options
|
|
110
|
+
```typescript
|
|
111
|
+
interface RedisOptions {
|
|
112
|
+
/**
|
|
113
|
+
* client name. default is a uuid, unique.
|
|
114
|
+
*/
|
|
115
|
+
name?: string;
|
|
116
|
+
url?: string;
|
|
117
|
+
port?: number;
|
|
118
|
+
host?: string;
|
|
119
|
+
/**
|
|
120
|
+
* 4 (IPv4) or 6 (IPv6), Defaults to 4.
|
|
121
|
+
*/
|
|
122
|
+
family?: number;
|
|
123
|
+
/**
|
|
124
|
+
* Local domain socket path. If set the port, host and family will be ignored.
|
|
125
|
+
*/
|
|
126
|
+
path?: string;
|
|
127
|
+
/**
|
|
128
|
+
* TCP KeepAlive on the socket with a X ms delay before start. Set to a non-number value to disable keepAlive.
|
|
129
|
+
*/
|
|
130
|
+
keepAlive?: number;
|
|
131
|
+
connectionName?: string;
|
|
132
|
+
/**
|
|
133
|
+
* If set, client will send AUTH command with the value of this option when connected.
|
|
134
|
+
*/
|
|
135
|
+
password?: string;
|
|
136
|
+
/**
|
|
137
|
+
* Database index to use.
|
|
138
|
+
*/
|
|
139
|
+
db?: number;
|
|
140
|
+
/**
|
|
141
|
+
* When a connection is established to the Redis server, the server might still be loading
|
|
142
|
+
* the database from disk. While loading, the server not respond to any commands.
|
|
143
|
+
* To work around this, when this option is true, ioredis will check the status of the Redis server,
|
|
144
|
+
* and when the Redis server is able to process commands, a ready event will be emitted.
|
|
145
|
+
*/
|
|
146
|
+
enableReadyCheck?: boolean;
|
|
147
|
+
keyPrefix?: string;
|
|
148
|
+
/**
|
|
149
|
+
* When the return value isn't a number, ioredis will stop trying to reconnect.
|
|
150
|
+
* Fixed in: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/15858
|
|
151
|
+
*/
|
|
152
|
+
retryStrategy?(times: number): number | false;
|
|
153
|
+
/**
|
|
154
|
+
* By default, all pending commands will be flushed with an error every
|
|
155
|
+
* 20 retry attempts. That makes sure commands won't wait forever when
|
|
156
|
+
* the connection is down. You can change this behavior by setting
|
|
157
|
+
* `maxRetriesPerRequest`.
|
|
158
|
+
*
|
|
159
|
+
* Set maxRetriesPerRequest to `null` to disable this behavior, and
|
|
160
|
+
* every command will wait forever until the connection is alive again
|
|
161
|
+
* (which is the default behavior before ioredis v4).
|
|
162
|
+
*/
|
|
163
|
+
maxRetriesPerRequest?: number | null;
|
|
164
|
+
/**
|
|
165
|
+
* 1/true means reconnect, 2 means reconnect and resend failed command. Returning false will ignore
|
|
166
|
+
* the error and do nothing.
|
|
167
|
+
*/
|
|
168
|
+
reconnectOnError?(error: Error): boolean | 1 | 2;
|
|
169
|
+
/**
|
|
170
|
+
* By default, if there is no active connection to the Redis server, commands are added to a queue
|
|
171
|
+
* and are executed once the connection is "ready" (when enableReadyCheck is true, "ready" means
|
|
172
|
+
* the Redis server has loaded the database from disk, otherwise means the connection to the Redis
|
|
173
|
+
* server has been established). If this option is false, when execute the command when the connection
|
|
174
|
+
* isn't ready, an error will be returned.
|
|
175
|
+
*/
|
|
176
|
+
enableOfflineQueue?: boolean;
|
|
177
|
+
/**
|
|
178
|
+
* The milliseconds before a timeout occurs during the initial connection to the Redis server.
|
|
179
|
+
* default: 10000.
|
|
180
|
+
*/
|
|
181
|
+
connectTimeout?: number;
|
|
182
|
+
/**
|
|
183
|
+
* After reconnected, if the previous connection was in the subscriber mode, client will auto re-subscribe these channels.
|
|
184
|
+
* default: true.
|
|
185
|
+
*/
|
|
186
|
+
autoResubscribe?: boolean;
|
|
187
|
+
/**
|
|
188
|
+
* If true, client will resend unfulfilled commands(e.g. block commands) in the previous connection when reconnected.
|
|
189
|
+
* default: true.
|
|
190
|
+
*/
|
|
191
|
+
autoResendUnfulfilledCommands?: boolean;
|
|
192
|
+
lazyConnect?: boolean;
|
|
193
|
+
tls?: tls.ConnectionOptions;
|
|
194
|
+
sentinels?: Array<{ host: string; port: number; }>;
|
|
195
|
+
name?: string;
|
|
196
|
+
/**
|
|
197
|
+
* Enable READONLY mode for the connection. Only available for cluster mode.
|
|
198
|
+
* default: false.
|
|
199
|
+
*/
|
|
200
|
+
readOnly?: boolean;
|
|
201
|
+
/**
|
|
202
|
+
* If you are using the hiredis parser, it's highly recommended to enable this option.
|
|
203
|
+
* Create another instance with dropBufferSupport disabled for other commands that you want to return binary instead of string
|
|
204
|
+
*/
|
|
205
|
+
dropBufferSupport?: boolean;
|
|
206
|
+
/**
|
|
207
|
+
* Whether to show a friendly error stack. Will decrease the performance significantly.
|
|
208
|
+
*/
|
|
209
|
+
showFriendlyErrorStack?: boolean;
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
That's it!
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./redis.service"), exports);
|
|
18
|
+
__exportStar(require("./redis.module"), exports);
|
|
19
|
+
__exportStar(require("./redis.interface"), exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as Redis from 'ioredis';
|
|
2
|
+
import { Provider } from '@nestjs/common';
|
|
3
|
+
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface';
|
|
4
|
+
export declare class RedisClientError extends Error {
|
|
5
|
+
}
|
|
6
|
+
export interface RedisClient {
|
|
7
|
+
defaultKey: string;
|
|
8
|
+
clients: Map<string, Redis.Redis>;
|
|
9
|
+
size: number;
|
|
10
|
+
}
|
|
11
|
+
export declare const createClient: () => Provider;
|
|
12
|
+
export declare const createAsyncClientOptions: (options: RedisModuleAsyncOptions) => {
|
|
13
|
+
provide: symbol;
|
|
14
|
+
useFactory: (...args: any[]) => RedisModuleOptions | Promise<RedisModuleOptions> | RedisModuleOptions[] | Promise<RedisModuleOptions[]>;
|
|
15
|
+
inject: any[];
|
|
16
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAsyncClientOptions = exports.createClient = exports.RedisClientError = void 0;
|
|
4
|
+
const Redis = require("ioredis");
|
|
5
|
+
const uuid_1 = require("uuid");
|
|
6
|
+
const redis_constants_1 = require("./redis.constants");
|
|
7
|
+
class RedisClientError extends Error {
|
|
8
|
+
}
|
|
9
|
+
exports.RedisClientError = RedisClientError;
|
|
10
|
+
async function getClient(options) {
|
|
11
|
+
const { onClientReady, url, ...opt } = options;
|
|
12
|
+
const client = url ? new Redis(url) : new Redis(opt);
|
|
13
|
+
if (onClientReady) {
|
|
14
|
+
onClientReady(client);
|
|
15
|
+
}
|
|
16
|
+
return client;
|
|
17
|
+
}
|
|
18
|
+
const createClient = () => ({
|
|
19
|
+
provide: redis_constants_1.REDIS_CLIENT,
|
|
20
|
+
useFactory: async (options) => {
|
|
21
|
+
const clients = new Map();
|
|
22
|
+
let defaultKey = (0, uuid_1.v4)();
|
|
23
|
+
if (Array.isArray(options)) {
|
|
24
|
+
await Promise.all(options.map(async (o) => {
|
|
25
|
+
const key = o.name || defaultKey;
|
|
26
|
+
if (clients.has(key)) {
|
|
27
|
+
throw new RedisClientError(`${o.name || 'default'} client is exists`);
|
|
28
|
+
}
|
|
29
|
+
clients.set(key, await getClient(o));
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (options.name && options.name.length !== 0) {
|
|
34
|
+
defaultKey = options.name;
|
|
35
|
+
}
|
|
36
|
+
clients.set(defaultKey, await getClient(options));
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
defaultKey,
|
|
40
|
+
clients,
|
|
41
|
+
size: clients.size,
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
inject: [redis_constants_1.REDIS_MODULE_OPTIONS],
|
|
45
|
+
});
|
|
46
|
+
exports.createClient = createClient;
|
|
47
|
+
const createAsyncClientOptions = (options) => ({
|
|
48
|
+
provide: redis_constants_1.REDIS_MODULE_OPTIONS,
|
|
49
|
+
useFactory: options.useFactory,
|
|
50
|
+
inject: options.inject,
|
|
51
|
+
});
|
|
52
|
+
exports.createAsyncClientOptions = createAsyncClientOptions;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DynamicModule, OnModuleDestroy } from '@nestjs/common';
|
|
2
|
+
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface';
|
|
3
|
+
import { RedisClient } from './redis-client.provider';
|
|
4
|
+
export declare class RedisCoreModule implements OnModuleDestroy {
|
|
5
|
+
private readonly options;
|
|
6
|
+
private readonly redisClient;
|
|
7
|
+
constructor(options: RedisModuleOptions | RedisModuleOptions[], redisClient: RedisClient);
|
|
8
|
+
static register(options: RedisModuleOptions | RedisModuleOptions[]): DynamicModule;
|
|
9
|
+
static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule;
|
|
10
|
+
onModuleDestroy(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
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 RedisCoreModule_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.RedisCoreModule = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const redis_client_provider_1 = require("./redis-client.provider");
|
|
19
|
+
const redis_constants_1 = require("./redis.constants");
|
|
20
|
+
const redis_service_1 = require("./redis.service");
|
|
21
|
+
let RedisCoreModule = RedisCoreModule_1 = class RedisCoreModule {
|
|
22
|
+
constructor(options, redisClient) {
|
|
23
|
+
this.options = options;
|
|
24
|
+
this.redisClient = redisClient;
|
|
25
|
+
}
|
|
26
|
+
static register(options) {
|
|
27
|
+
return {
|
|
28
|
+
module: RedisCoreModule_1,
|
|
29
|
+
providers: [
|
|
30
|
+
(0, redis_client_provider_1.createClient)(),
|
|
31
|
+
{ provide: redis_constants_1.REDIS_MODULE_OPTIONS, useValue: options },
|
|
32
|
+
],
|
|
33
|
+
exports: [redis_service_1.RedisService],
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
static forRootAsync(options) {
|
|
37
|
+
return {
|
|
38
|
+
module: RedisCoreModule_1,
|
|
39
|
+
imports: options.imports,
|
|
40
|
+
providers: [(0, redis_client_provider_1.createClient)(), (0, redis_client_provider_1.createAsyncClientOptions)(options)],
|
|
41
|
+
exports: [redis_service_1.RedisService],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
onModuleDestroy() {
|
|
45
|
+
const closeConnection = ({ clients, defaultKey }) => (options) => {
|
|
46
|
+
const name = options.name || defaultKey;
|
|
47
|
+
const client = clients.get(name);
|
|
48
|
+
if (client && !options.keepAlive) {
|
|
49
|
+
client.disconnect();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const closeClientConnection = closeConnection(this.redisClient);
|
|
53
|
+
if (Array.isArray(this.options)) {
|
|
54
|
+
this.options.forEach(closeClientConnection);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
closeClientConnection(this.options);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
RedisCoreModule = RedisCoreModule_1 = __decorate([
|
|
62
|
+
(0, common_1.Global)(),
|
|
63
|
+
(0, common_1.Module)({
|
|
64
|
+
providers: [redis_service_1.RedisService],
|
|
65
|
+
exports: [redis_service_1.RedisService],
|
|
66
|
+
}),
|
|
67
|
+
__param(0, (0, common_1.Inject)(redis_constants_1.REDIS_MODULE_OPTIONS)),
|
|
68
|
+
__param(1, (0, common_1.Inject)(redis_constants_1.REDIS_CLIENT)),
|
|
69
|
+
__metadata("design:paramtypes", [Object, Object])
|
|
70
|
+
], RedisCoreModule);
|
|
71
|
+
exports.RedisCoreModule = RedisCoreModule;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ModuleMetadata } from '@nestjs/common/interfaces';
|
|
2
|
+
import { Redis, RedisOptions } from 'ioredis';
|
|
3
|
+
export interface RedisModuleOptions extends RedisOptions {
|
|
4
|
+
name?: string;
|
|
5
|
+
url?: string;
|
|
6
|
+
onClientReady?(client: Redis): void;
|
|
7
|
+
}
|
|
8
|
+
export interface RedisModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
9
|
+
useFactory?: (...args: any[]) => RedisModuleOptions | RedisModuleOptions[] | Promise<RedisModuleOptions> | Promise<RedisModuleOptions[]>;
|
|
10
|
+
inject?: any[];
|
|
11
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { RedisModuleAsyncOptions, RedisModuleOptions } from './redis.interface';
|
|
3
|
+
export declare class RedisModule {
|
|
4
|
+
static register(options: RedisModuleOptions | RedisModuleOptions[]): DynamicModule;
|
|
5
|
+
static forRootAsync(options: RedisModuleAsyncOptions): DynamicModule;
|
|
6
|
+
}
|
|
@@ -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
|
+
static register(options) {
|
|
15
|
+
return {
|
|
16
|
+
module: RedisModule_1,
|
|
17
|
+
imports: [redis_core_module_1.RedisCoreModule.register(options)],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
static forRootAsync(options) {
|
|
21
|
+
return {
|
|
22
|
+
module: RedisModule_1,
|
|
23
|
+
imports: [redis_core_module_1.RedisCoreModule.forRootAsync(options)],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
RedisModule = RedisModule_1 = __decorate([
|
|
28
|
+
(0, common_1.Module)({})
|
|
29
|
+
], RedisModule);
|
|
30
|
+
exports.RedisModule = RedisModule;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Redis } from 'ioredis';
|
|
2
|
+
import { RedisClient } from './redis-client.provider';
|
|
3
|
+
export declare class RedisService {
|
|
4
|
+
private readonly redisClient;
|
|
5
|
+
constructor(redisClient: RedisClient);
|
|
6
|
+
getClient(name?: string): Redis;
|
|
7
|
+
getClients(): Map<string, Redis>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.RedisService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const redis_constants_1 = require("./redis.constants");
|
|
18
|
+
const redis_client_provider_1 = require("./redis-client.provider");
|
|
19
|
+
let RedisService = class RedisService {
|
|
20
|
+
constructor(redisClient) {
|
|
21
|
+
this.redisClient = redisClient;
|
|
22
|
+
}
|
|
23
|
+
getClient(name) {
|
|
24
|
+
if (!name) {
|
|
25
|
+
name = this.redisClient.defaultKey;
|
|
26
|
+
}
|
|
27
|
+
if (!this.redisClient.clients.has(name)) {
|
|
28
|
+
throw new redis_client_provider_1.RedisClientError(`client ${name} does not exist`);
|
|
29
|
+
}
|
|
30
|
+
return this.redisClient.clients.get(name);
|
|
31
|
+
}
|
|
32
|
+
getClients() {
|
|
33
|
+
return this.redisClient.clients;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
RedisService = __decorate([
|
|
37
|
+
(0, common_1.Injectable)(),
|
|
38
|
+
__param(0, (0, common_1.Inject)(redis_constants_1.REDIS_CLIENT)),
|
|
39
|
+
__metadata("design:paramtypes", [Object])
|
|
40
|
+
], RedisService);
|
|
41
|
+
exports.RedisService = RedisService;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./dist";
|
package/index.js
ADDED
package/nodemon.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nathapp/nestjs-redis",
|
|
3
|
+
"version": "1.3.3",
|
|
4
|
+
"description": "a NestJS ioRedis module",
|
|
5
|
+
"author": "skunight",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/skywills/nestjs-redis.git"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "rimraf dist && tsc -p tsconfig.json",
|
|
13
|
+
"precommit": "lint-staged",
|
|
14
|
+
"prepublish": "yarn run build",
|
|
15
|
+
"publish-public": "yarn publish --access public",
|
|
16
|
+
"test": "jest",
|
|
17
|
+
"test:watch": "jest --watch",
|
|
18
|
+
"test:cov": "jest --coverage",
|
|
19
|
+
"test:e2e": "jest --config ./test/jest-e2e.json",
|
|
20
|
+
"lint": "eslint -c .eslintrc.js"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@nestjs/common": "^8.0.0",
|
|
24
|
+
"@nestjs/core": "^8.0.0",
|
|
25
|
+
"@types/ioredis": "*",
|
|
26
|
+
"@types/uuid": "*",
|
|
27
|
+
"ioredis": "^4",
|
|
28
|
+
"reflect-metadata": "*",
|
|
29
|
+
"rxjs": "^7.2.0",
|
|
30
|
+
"uuid": "^8"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@nestjs/testing": "^8.0.0",
|
|
34
|
+
"@types/node": "*",
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "^4",
|
|
36
|
+
"@typescript-eslint/parser": "^4",
|
|
37
|
+
"cz-conventional-changelog": "^3",
|
|
38
|
+
"eslint": "*",
|
|
39
|
+
"eslint-config-prettier": "^6",
|
|
40
|
+
"eslint-plugin-jest": "^24",
|
|
41
|
+
"eslint-plugin-prettier": "^3",
|
|
42
|
+
"jest": "^26",
|
|
43
|
+
"prettier": "^2",
|
|
44
|
+
"rimraf": "*",
|
|
45
|
+
"ts-jest": "^26",
|
|
46
|
+
"typescript": "^4"
|
|
47
|
+
},
|
|
48
|
+
"keywords": [
|
|
49
|
+
"nestjs",
|
|
50
|
+
"nest",
|
|
51
|
+
"redis",
|
|
52
|
+
"nestjs-redis"
|
|
53
|
+
],
|
|
54
|
+
"config": {
|
|
55
|
+
"commitizen": {
|
|
56
|
+
"path": "./node_modules/cz-conventional-changelog"
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"types": "./index.d.ts",
|
|
60
|
+
"bugs": {
|
|
61
|
+
"url": "https://github.com/skywills/nestjs-redis/issues"
|
|
62
|
+
},
|
|
63
|
+
"homepage": "https://github.com/skywills/nestjs-redis#readme",
|
|
64
|
+
"main": "index.js",
|
|
65
|
+
"directories": {
|
|
66
|
+
"lib": "lib"
|
|
67
|
+
}
|
|
68
|
+
}
|