@ashu_guo/nest-redis
v1.0.5
Published
ioredis utilities module for nestjs framework
Downloads
1
Readme
Description
ioredis utilities module for nestjs framework
Installation
$ npm i --save @ashu_guo/nest-redis
Usage
Import RedisModule
:
import {RedisModule} from '@ashu_guo/nest-redis';
@Module({
imports: [RedisModule.register({
host: 'localhost',
port: 6379,
username: 'xxxxx',
password: '*****',
})],
providers: [...],
})
export class UsersModule {
}
Inject RedisClient
:
import {InjectRedisClient, RedisClient} from '@ashu_guo/nest-redis';
@Injectable()
export class UsersService {
constructor(@InjectRedisClient() private readonly redisClient: RedisClient) {
}
}
Async options
Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case,
use registerAsync()
method, that provides a couple of various ways to deal with async data.
1. Use factory
@Module({
imports: [
RedisModule.registerAsync({
useFactory: () => ({
host: 'localhost',
port: 6379,
username: 'xxxxx',
password: '*****',
})
})
]
})
export class AppModule {
}
Obviously, our factory behaves like every other one (might be async
and is able to inject dependencies
through inject
).
RedisModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
host: configService.get<string>('REDIS_HOST'),
port: configService.get<number>('REDIS_PORT'),
username: configService.get<string>('REDIS_USERNAME'),
password: configService.get<string>('REDIS_PASSWORD'),
}),
inject: [ConfigService],
})
2. Use class
RedisModule.registerAsync({
useClass: RedisConfigService
});
Above construction will instantiate RedisConfigService
inside RedisModule
and will leverage it to create options
object.
import {RedisOptionsFactory} from '@ashu_guo/nest-redis';
import {RedisOptions} from 'ioredis';
export class RedisConfigService implements RedisOptionsFactory {
createRedisOptions(): RedisOptions {
return {
host: 'localhost',
port: 6379,
username: 'xxxxxx',
password: '******',
};
}
}
3. Use existing
RedisModule.registerAsync({
imports: [ConfigModule],
useExisting: ConfigService,
})
It works the same as useClass
with one critical difference - RedisModule
will lookup imported modules to reuse
already created ConfigService
, instead of instantiating it on its own.
Used By
API Spec
The RedisModule
takes an options
object:
License
Nest is MIT licensed.