question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I’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:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
nolazybitscommented, Sep 12, 2018

@kamilmysliwiec the delegation works good 👍 For whoever coming here

Module A

import {Module} from "@nestjs/common";
@Module({
    imports: [ModuleC.ForRootAsync("A")],
    controllers: [],
    providers: [],
    exports: [],
})
export class ModuleA {}

Module B

import {Module} from "@nestjs/common";
@Module({
    imports: [ModuleC.ForRootAsync("B")],
    controllers: [],
    providers: [],
    exports: [],
})
export class ModuleB {}

Module C

import {Module} from "@nestjs/common";
@Module({
    imports: [],
    controllers: [],
    providers: [],
    exports: [],
})
export class ModuleC {
    public static ForRootAsync(...entities: string[]): DynamicModule {
        return {
            module: ModuleC,
            imports: [ModuleCCore.ForRootAsync(...entities)],
        };
    }
}

Module C Core

import {Module} from "@nestjs/common";
@Module({
    imports: [],
    controllers: [],
    providers: [],
    exports: [],
})
export class ModuleCCore {
    private static _Inc: number = 0;
    private static _Entities: string[] = [];
    public constructor() {
        this._test = ModuleC._Inc;
        ModuleC._Inc ++;
    }

    public static ForRoot(...entities: string[]): void {
            ModuleCCore._Entities = ModuleCCore._Entities.concat(entities);
            return {
              module: ModuleCCore
              providers: // createProviders somewhere using our entities
            }
    }

    public onModuleInit(): any {
        console.log(this._test);
        console.log(ModuleCCore._Entities);
    }
}

and the output is then

0
[ 'A', 'B' ]

🎉

1reaction
kamilmysliwieccommented, Sep 12, 2018

I am creating a DatabaseModule where I want typeorm to know about all the entities that my project will have before creating it.

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/typeorm

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found