DynamicModule
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
DynamicModule aren’t singleton
Expected behavior
DynamicModules should be singletons
Minimal reproduction of the problem with instructions
import {Module} from "@nestjs/common";
@Module({
imports: [ModuleC.ForRoot("A")],
controllers: [],
providers: [],
exports: [],
})
export class ModuleA {}
import {Module} from "@nestjs/common";
@Module({
imports: [ModuleC.ForRoot("B")],
controllers: [],
providers: [],
exports: [],
})
export class ModuleB {}
import {Module} from "@nestjs/common";
@Module({
imports: [],
controllers: [],
providers: [],
exports: [],
})
export class ModuleC {
private static _Inc: number = 0;
private static _Entities: string[] = [];
public constructor() {
this._test = ModuleC._Inc;
ModuleC._Inc ++;
}
public static ForRoot(...entities: string[]): void {
ModuleC._Entities = ModuleC._Entities.concat(entities);
return {
module: ModuleC
providers: // createProviders somewhere using our entities
}
}
public onModuleInit(): any {
console.log(this._test);
console.log(ModuleC._Entities);
}
}
will output
1
[ 'A', 'B' ]
0
[ 'A', 'B' ]
I would like to have
0
['A', 'B']
================================== The workaround I found is the following
import {Module} from "@nestjs/common";
ModuleC.Configure("A");
@Module({
imports: [ModuleC],
controllers: [],
providers: [],
exports: [],
})
export class ModuleA {}
import {Module} from "@nestjs/common";
ModuleC.Configure("B");
@Module({
imports: [ModuleC],
controllers: [],
providers: [],
exports: [],
})
export class ModuleB {}
import {Module} from "@nestjs/common";
@Module({
imports: [],
controllers: [],
providers: [],
exports: [],
})
export class ModuleC {
private static _Inc: number = 0;
private static _Entities: string[] = [];
public constructor() {
this._test = ModuleC._Inc;
ModuleC._Inc ++;
}
public static Configure(...entities: string[]): void {
ModuleC._Entities = ModuleC._Entities.concat(entities);
}
public onModuleInit(): any {
console.log(this._test);
console.log(ModuleC._Entities);
}
}
which output
0
[ 'A', 'B' ]
but the obvious is that I can’t export any of my dynamically created providers.
What is the motivation / use case for changing the behavior?
I am creating a DatabaseModule where I want typeorm to know about all the entities that my project will have before creating it.
Environment
Nest version: 5.3.2
For Tooling issues:
- Node version: 10
- Platform: Mac
Quite new to NestJS so I might have missed the obvious. Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
DynamicModule - Wolfram Language Documentation
represents an object which maintains the same local instance of the symbols x, y, … in the course of all evaluations of Dynamic...
Read more >Dynamic modules | NestJS - A progressive Node.js framework
Using dynamic module features, we can make our configuration module dynamic so that the consuming module can use an API to control how...
Read more >What is the right way to construct DynamicModule
Here is a simple DynamicModule . One can imagine that the dynamic output below containing further Dynamic s or only parts of the...
Read more >API with NestJS #70. Defining dynamic modules
Creating a dynamic module. To create a dynamic module, let's add a static method to the EmailModule. email.module.ts ...
Read more >Creating Dynamic Modules in Nest JS Part-1
et's consider what a dynamic module import, where we're passing in a ... import { DynamicModule, Module } from '@nestjs/common'; ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kamilmysliwiec the delegation works good 👍 For whoever coming here
Module A
Module B
Module C
Module C Core
and the output is then
🎉
I would suggest using another module which is shared across all dynamic modules. You could take a look at
@nestjs/typeorm
source code to get some inspiration how it might work underneath 🙂 https://github.com/nestjs/typeormIn terms of the real topic of this issue, dynamic modules with different configurations AREN’T singletons and they cannot be singletons. It doesn’t make too much sense honestly.