Hi! I have searched around on how to use Redis as a db, since the current code in my app looks like hell. I have found this answer(#1007602665716469800 message) from jmcdo29 . However, it got me thinking on how can I handle the client errors and disconnections when working with this kind of provider configuration.
I noticed that exists a nestjs-redis module that use DynamicModule to handle all of this, but I didn't like that I need to register the module at the root of my app, making it global.
Will I need to create a DynamicModule or would it be possible to at least handle the disconnection from the provider config?
Here is the current code that I wrote based on the previous link:
import Redis, { RedisOptions } from 'ioredis';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
const REDIS_DB_FOR_CASE_TREE = 0;
@Module({
imports: [ConfigModule],
providers: [
{
provide: 'REDIS_OPTIONS',
inject: [ConfigService],
useFactory: configService =>
({
db: REDIS_DB_FOR_CASE_TREE,
host: configService.get('REDIS_HOST'),
port: configService.get('REDIS_PORT', 6379),
} as RedisOptions),
},
{
provide: 'REDIS',
inject: ['REDIS_OPTIONS'],
useFactory: (options: RedisOptions) => new Redis(options),
},
],
exports: ['REDIS'],
})
export class RedisModule {}