nestjs: Dynamic module for dynamically insert controllers y routes module

1k views Asked by At

im trying to build a dynamic module for dynamically insert additional endpoints to certain routes modules (adding a generic crud endpoints):

crud.module.ts

@Module({})
export class CrudModule {
  static register(config): DynamicModule {
    const controller = getController(config.route);
    return {
      module: TestModule,
      controllers: [controller],
    };
  }
}

getController.ts

function getController(route: string): Type<any> {
  @ApiTags(`${route}`)
  @Controller(`${route}/crud`)
  class CrudController {
    @Get()
    get() {
      return route;
    }
  }

  return CrudController;
}

export { getController };

And adding to specified routes modules

route1.module.ts

@Module({
  imports: [
    CrudModule.register({
      route: 'route1',
    }),
  ],
  providers: [UserService],
  controllers: [UserController],
})
export class Route1Module {}

route2.module.ts

@Module({
  imports: [
    CrudModule.register({
      route: 'route2',
    }),
  ],
  providers: [UserService],
  controllers: [UserController],
})
export class Route2Module {}

routes.module.ts

    @Module({
      imports: [Route1Module, Route2Module],
    })
    export class RoutesModule {}

The problem is that the routes generated from CrudModule only is being mapped for the first imported route module (in this case, just for Route1Module), I need a bit help here for get mapping the routes from crud for every Module where is being registered

================ EXTRA INFO

Adding some logs about is loading the CrudModule in the different route modules

@Module({})
export class CrudModule {
  static register(config): DynamicModule {
    console.log({ route: config.route })
    const controller = getController(config.route);
    return {
      module: TestModule,
      controllers: [controller],
    };
  }
}
[6:37:40 PM] Found 0 errors. Watching for file changes.

**{ route: 'route1' }
{ route: 'route2' }**
[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [NestFactory] Starting Nest application...
[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [InstanceLoader] AppModule dependencies initialized +21ms
[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [InstanceLoader] RoutesModule dependencies initialized +0ms
*[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [InstanceLoader] Route1Module dependencies initialized +0ms
[Nest] 90418  - 11/13/2022, 6:37:40 PM*     LOG [InstanceLoader] Route2Module dependencies initialized +0ms
[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [InstanceLoader] CacheModule dependencies initialized +0ms
**[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [InstanceLoader] CrudModule dependencies initialized +0ms**
[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [InstanceLoader] InterceptorsModule dependencies initialized +0ms
**[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [RoutesResolver] CrudController {/route1/crud}: +225ms
[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [RouterExplorer] Mapped {/route1/crud, GET} route +1ms**
[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [NestApplication] Nest application successfully started +0ms
[Nest] 90418  - 11/13/2022, 6:37:40 PM     LOG [Application is running on: http://localhost:3000]

The log trigger both module loaded, but only once instance of CrudModule is initialised

============= SOLUTION

Thinking about the dependencies initialised and the overriding between controllers/modules naming.

I testing about to dynamically change the name of controller loaded by each module where is being used

@Module({})
export class CrudModule {
  static registre(route: string): DynamicModule {
    const Controller = getController(route);
    Object.defineProperty(Controller, 'name', { value: `${route}Controller` });
    console.log(Controller);
    return {
      module: CrudModule,
      controllers: [Controller],
      providers: [Controller],
      exports: [Controller],
    };
  }
}

I got finally both dynamic controllers being initialised and working correctly

[class route1Controller]
[class route2Controller]
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [NestFactory] Starting Nest application...
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [InstanceLoader] AppModule dependencies initialized +21ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [InstanceLoader] RoutesModule dependencies initialized +0ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [InstanceLoader] Route1Module dependencies initialized +0ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [InstanceLoader] Route2Module dependencies initialized +0ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [InstanceLoader] CacheModule dependencies initialized +0ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [InstanceLoader] CrudModule dependencies initialized +0ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [InstanceLoader] CrudModule dependencies initialized +0ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [InstanceLoader] InterceptorsModule dependencies initialized +1ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [RoutesResolver] route1Controller {/route1/crud}: +225ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [RouterExplorer] Mapped {/route1/crud, GET} route +1ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [RoutesResolver] route2Controller {/route2/crud}: +0ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [RouterExplorer] Mapped {/route2/crud, GET} route +0ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [NestApplication] Nest application successfully started +1ms
[Nest] 90617  - 11/13/2022, 6:51:28 PM     LOG [Application is running on: http://localhost:3000]
``
0

There are 0 answers