#How to include dynamic module more that once

13 messages · Page 1 of 1 (latest)

ancient otter
#

I have a dynamic module that i include using a forRoot method. however, i think want other modules to be able to use this modules providers. But I get and error noting that the provider does not seem to be included.

Do I need another static method to return a cached module?

drowsy vine
#

Do you want to configure the dynamic module once and re-use it?

ancient otter
#

yes

#

I guess the forRoot should be called before other modules try to use it though

drowsy vine
#

I'd make a wrapper module for it that does

@Module({
  imports: [DynamicModule.forRoot(options)],
  exports: [DynamicModule],
})
export class DyanicWrapperMoudle {}

And then add DynamicWrapperModule where you need the confgured dynamic module

ancient otter
#

I see

#

I would think this would be a common pattern? there is a convention with naming the static methods. You don't think the second static method is a good idea?

drowsy vine
#

You could use a second static method. @golevelup/nestjs-module actually included one for situations like this. It used an observable race between a timeout and retrieving the existing configuration, so there would never really be a timewhen the timeout would hit. I can find that setup if you want

ancient otter
#

Id be interested to peek at it. basically i am struggling with naming the wrapper module because they are both the same exact thing😂 ...so previous conventions helpful to review

drowsy vine
ancient otter
#

Cool thanks a lot. I like the ExternallyConfigured so i might do the wrapper module and just Module and ExternallyConfiguredModule when importing. That solves my naming dilema

ancient otter
#

jsut for late onlookers. i ended up doing the dynamic-module-test because I like being able to run the forRootcall at my AppModule level, instead of nested behind a wrapper module! This is nice trick and should be in the docs this way i think