HINT: By default, the ClusterModule is a Global module.
HINT: If you don't set the namespace for a client, its namespace is set to "default". Please note that you shouldn't have multiple client without a namespace, or with the same namespace, otherwise they will get overridden.
If set to true, all clients will be closed automatically on nestjs application shutdown. To use closeClient, you must enable listeners by calling app.enableShutdownHooks(). Read more about the application shutdown.
readyLog
boolean
false
false
If set to true, then ready logging will be displayed when the client is ready.
errorLog
boolean
true
false
If set to true, then errors that occurred while connecting will be displayed by the built-in logger.
Client name. If client name is not given then it will be called "default". Different clients must have different names. You can import DEFAULT_CLUSTER_NAMESPACE to use it.
nodes
{ host?: string; port?: number }[] | string[] | number[]
undefined
true
List of cluster nodes.
onClientCreated
function
undefined
false
Function to be executed as soon as the client is created.
// just a simple exampleimport{Module,ValueProvider}from"@nestjs/common";import{ClusterModule,ClusterModuleOptions}from"@liaoliaots/nestjs-redis";constMyOptionsSymbol=Symbol("options");constMyOptionsProvider:ValueProvider<ClusterModuleOptions>={provide:MyOptionsSymbol,useValue:{config:{nodes:[{host:"localhost",port:16380}],redisOptions:{password:"authpassword"},},},};@Module({imports:[ClusterModule.forRootAsync({useFactory(options:ClusterModuleOptions){returnoptions;},inject:[MyOptionsSymbol],extraProviders:[MyOptionsProvider],}),],})exportclassAppModule{}
... or via useExisting, if you wish to use an existing configuration provider imported from a different module.
import{Module}from"@nestjs/common";import{ClusterModule}from"@liaoliaots/nestjs-redis";@Module({imports:[ClusterModule.forRoot({config:{nodes:[{host:"localhost",port:16380}],redisOptions:{password:"authpassword"},// or with URL// nodes: ['redis://:authpassword@localhost:16380']},}),],})exportclassAppModule{}
By default, the ClusterModule is a Global module, ClusterService and all cluster instances are registered in the global scope. Once defined, they're available everywhere.
You can change this behavior by isGlobal parameter:
// cats.module.tsimport{Module}from"@nestjs/common";import{ClusterModule}from"@liaoliaots/nestjs-redis";import{CatsService}from"./cats.service";import{CatsController}from"./cats.controller";@Module({imports:[ClusterModule.forRoot({config:{nodes:[{host:"localhost",port:16380}],redisOptions:{password:"authpassword"},},},false// <-- providers are registered in the module scope),],providers:[CatsService],controllers:[CatsController],})exportclassCatsModule{}
This package exposes getClusterToken() function that returns an internal injection token based on the provided context. Using this token, you can provide a mock implementation of the cluster instance using any of the standard custom provider techniques, including useClass, useValue, and useFactory.