Make global module as an option
lhr0909 opened this issue Β· 8 comments
Hi! π
Firstly, thanks for your work on this project! π
I would like to set up the redis module in a non-global way, so is there a chance we can make it configurable? Thanks!
Here is the diff that solved my problem:
diff --git a/node_modules/@liaoliaots/nestjs-redis/dist/redis/redis.module.js b/node_modules/@liaoliaots/nestjs-redis/dist/redis/redis.module.js
index 8d6de0a..d72312e 100644
--- a/node_modules/@liaoliaots/nestjs-redis/dist/redis/redis.module.js
+++ b/node_modules/@liaoliaots/nestjs-redis/dist/redis/redis.module.js
@@ -29,7 +29,7 @@ let RedisModule = RedisModule_1 = class RedisModule {
...redisClientProviders
];
return {
- global: true,
+ global: false,
module: RedisModule_1,
providers,
exports: [redis_manager_1.RedisManager, ...redisClientProviders]
This issue body was partially generated by patch-package.
I can submit a PR if you want me to (not with the above patch but exposing global
as an option).
@lhr0909 thanks for opening the issue!
It's a good suggestion and I will look into it ASAP.
please follow the steps below:
- bump
@liaoliaots/nestjs-redis
to6.0.0
npm install --save @liaoliaots/nestjs-redis@6
- create a shared module:
// shared-redis.module.ts
import { Module } from '@nestjs/common';
import { RedisModule } from '@liaoliaots/nestjs-redis';
@Module({
imports: [
RedisModule.forRoot(
{
readyLog: true,
config: {
host: '127.0.0.1',
port: 6380,
password: 'bitnami'
}
},
false // <--- make the module non global
)
],
exports: [RedisModule] // <--- important: share this module
})
export class SharedRedis {}
- import
SharedRedis
to your business module:
// cats.module.ts
import { Module } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CatsController } from './cats.controller';
import { SharedRedis } from '../shared-redis.module';
@Module({
imports: [SharedRedis], // <---
providers: [CatsService],
controllers: [CatsController]
})
export class CatsModule {}
- test
npm run start:dev
ε₯½ηοΌθΏδΈ€ε€©ζδΌζ΄εζ΅θ―δΈδΈγζθ°’π
Does it work for you? I've got error as below:
[Nest] 603402 - 03/01/2022, 2:44:19 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the HistoryRepository (?). Please make sure that the argument RedisModule:default at index [0] is available in the HistoryModule context.
Potential solutions:
- If RedisModule:default is a provider, is it part of the current HistoryModule?
- If RedisModule:default is exported from a separate @Module, is that module imported within HistoryModule?
@Module({
imports: [ /* the Module containing RedisModule:default */ ]
})
Everything looks fine in the module's configuration.
@AvantaR I upgraded on my end and it worked as expected (I didn't do the shared module trick though). Curious why this happens. Maybe @liaoliaots can help π
Ok, it was not related to this module, but to the circular imports in my code. Thanks.
@AvantaR It may be useful for you to import files in the correct order:
As mentioned above, decorator @InjectRedis
is used in CatsService
. If import SharedRedis
before CatsService
, nest will throw an error, as shown below. Because some methods in RedisModule
will get called after the @InjectRedis
calls and therefore RedisModule
will create/re-export providers correctly.
If import CatsService
first, it works as expected:
- - OR - -
You can use RedisModule
in the global scope, that error will not exist.