Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make global module as an option #175

Open
lhr0909 opened this issue Jan 7, 2022 · 8 comments
Open

Make global module as an option #175

lhr0909 opened this issue Jan 7, 2022 · 8 comments

Comments

@lhr0909
Copy link

lhr0909 commented Jan 7, 2022

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.

@lhr0909
Copy link
Author

lhr0909 commented Jan 7, 2022

I can submit a PR if you want me to (not with the above patch but exposing global as an option).

@liaoliaots
Copy link
Owner

@lhr0909 thanks for opening the issue!
It's a good suggestion and I will look into it ASAP.

@liaoliaots liaoliaots mentioned this issue Feb 19, 2022
11 tasks
@liaoliaots
Copy link
Owner

liaoliaots commented Feb 21, 2022

please follow the steps below:

  1. bump @liaoliaots/nestjs-redis to 6.0.0
npm install --save @liaoliaots/nestjs-redis@6
  1. 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 {}
  1. 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 {}
  1. test
npm run start:dev

@lhr0909
Copy link
Author

lhr0909 commented Feb 26, 2022

好的!这两天我会整合测试一下。感谢🙏

@AvantaR
Copy link

AvantaR commented Mar 1, 2022

@liaoliaots

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.

@lhr0909
Copy link
Author

lhr0909 commented Mar 2, 2022

@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 😄

@AvantaR
Copy link

AvantaR commented Mar 2, 2022

Ok, it was not related to this module, but to the circular imports in my code. Thanks.

@liaoliaots
Copy link
Owner

liaoliaots commented Mar 2, 2022

@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.

Screenshot_20220302_233757

If import CatsService first, it works as expected:

Screenshot_20220302_233830

- - OR - -
You can use RedisModule in the global scope, that error will not exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants