|
1 | | -import { Inject, applyDecorators } from '@nestjs/common'; |
| 1 | +import { applyDecorators } from '@nestjs/common'; |
| 2 | +import { Inject, Injectable } from '@nestjs/common'; |
| 3 | +import { ConfigService } from '../configs/config.service'; |
2 | 4 |
|
3 | | -export const prefixesForRedisClients: string[] = []; |
| 5 | +export const prefixesForRedisClients: string[] = new Array<string>(); |
4 | 6 |
|
5 | | -/* Inject a *single* named RedisService instance |
6 | | - * @RedisServer('AUTH') myRedis: RedisService |
7 | | - */ |
8 | | -export function RedisServer(prefix = '') { |
| 7 | +export function RedisServer(prefix: string = '') { |
| 8 | + console.log('RedisServer', prefix); |
9 | 9 | if (!prefixesForRedisClients.includes(prefix)) { |
10 | 10 | prefixesForRedisClients.push(prefix); |
11 | 11 | } |
12 | 12 | return Inject(`RedisService${prefix}`); |
13 | 13 | } |
14 | 14 |
|
15 | | -/* Inject *all* prefixed Redis clients |
16 | | - * constructor(@RedisServers() ...args: RedisService[]) {} |
17 | | - */ |
18 | | -export function RedisServers() { |
19 | | - return applyDecorators(...prefixesForRedisClients.map((p) => Inject(`RedisService${p}`))); |
| 15 | +// This function now expects a ConfigService instance to be passed in |
| 16 | +export function RedisServers(configService: ConfigService) { |
| 17 | + const configRedisServers = configService.get('REDIS_SERVERS'); |
| 18 | + const decorators = []; |
| 19 | + if (configRedisServers && typeof configRedisServers === 'object') { |
| 20 | + for (const key in configRedisServers) { |
| 21 | + decorators.push(Inject(`REDIS_${key}_CLIENT`)); |
| 22 | + } |
| 23 | + } |
| 24 | + //console.log('decorators', decorators); |
| 25 | + return applyDecorators(...decorators); |
20 | 26 | } |
0 commit comments