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

Dynamic Module providers: useFactory not working when using same token #14099

Closed
4 of 15 tasks
Fksg opened this issue Oct 31, 2024 · 3 comments
Closed
4 of 15 tasks

Dynamic Module providers: useFactory not working when using same token #14099

Fksg opened this issue Oct 31, 2024 · 3 comments
Labels
needs triage This issue has not been looked into

Comments

@Fksg
Copy link

Fksg commented Oct 31, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When using a dynamic module with useFactory to provide a simple value, I observe unexpected behavior when injecting tokens in multiple modules. Specifically, when creating two modules with different values for the token, both instances end up with the same value, instead of retaining their respective values.

Reproduction

  1. Define a dynamic module as follows:
@Module({})
class DynamicModule {
    static register(value: string) {
        return {
            module: DynamicModule,
            providers: [{ provide: "TOKEN", useFactory: () => value }],
            exports: ["TOKEN"]
        }
    }
}
  1. Create two modules that import DynamicModule with different values:
@Module({ imports: [DynamicModule.register("Module1")] })
class Module1 {
    constructor(@Inject("TOKEN") token: string) {
        console.log(`Should display Module1: ${token}`)
    }
}

@Module({ imports: [DynamicModule.register("Module2")] })
class Module2 {
    constructor(@Inject("TOKEN") token: string) {
        console.log(`Should display Module2: ${token}`)
    }
}

@Module({ imports: [Module1, Module2] })
class AppModule {
}

NestFactory.create(AppModule).then(app => app.listen(3000))
  1. When running the above code, the output shows the same token for both modules:
Should display Module2: Module1
Should display Module1: Module1
  1. However, if I replace useFactory with useValue, the tokens are displayed correctly:
Should display Module1: Module1
Should display Module2: Module2

Minimum reproduction code

https://github.com/Fksg/dynamic-module/blob/main/src/main.ts

Steps to reproduce

No response

Expected behavior

Each module should display its respective token as defined by its register call.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

10.4.6

Packages versions

"@nestjs/common": "10.4.6"
"@nestjs/core": "10.4.6"

Node.js version

20.17.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@Fksg Fksg added the needs triage This issue has not been looked into label Oct 31, 2024
@micalevisk
Copy link
Member

I believe that this is expected.

image

To circumvent that, you can:

  1. replace useFactory with useValue, or
  2. add a 'no-op' custom value provider to that dynamic module so that it will receive an unique ID every time you call the dynamic module (or an unique ID based on its parameters, for example; you could use the object-hash lib). See how @nestjs/typeorm does that here: https://github.com/nestjs/typeorm/blob/92f193cc0e19fda68a83ea58c79494c816524efd/lib/typeorm-core.module.ts#L113-L116

@Fksg
Copy link
Author

Fksg commented Oct 31, 2024

I need to use useFactory and the same token ID.

@kamilmysliwiec
Copy link
Member

This will be fixed in the next major release #13336

@nestjs nestjs locked and limited conversation to collaborators Nov 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants