Dynamic Modules

Preview — 3 of 10 questions

A static register() method on a dynamic module must return what?

javascript
import { DynamicModule, Module } from '@nestjs/common';

@Module({})
export class ConfigModule {
  static register(options: ConfigOptions): DynamicModule {
    return {
      module: ConfigModule,
      providers: [{ provide: 'CONFIG_OPTIONS', useValue: options }],
      exports: ['CONFIG_OPTIONS'],
    };
  }
}
AA plain provider object
BA DynamicModule object containing at least the module property plus optional imports/providers/exports/controllers/global
CThe class instance of the module
DA Promise<void>

Which statement best captures the conventional distinction among these dynamic-module methods?

javascript
@Module({})
export class CacheModule {
  static forRootAsync(opts: CacheAsyncOptions): DynamicModule {
    return {
      module: CacheModule,
      imports: opts.imports,
      providers: [
        { provide: 'CACHE_OPTS', useFactory: opts.useFactory, inject: opts.inject ?? [] },
      ],
      exports: ['CACHE_OPTS'],
    };
  }
}
Aregister() configures a module per-import with options, forRoot() configures a shared/global setup once, and *Async() variants resolve options asynchronously via factories/injected providers
BThey are interchangeable names with identical behavior
CforRoot() is for testing only; register() is for production
DforRootAsync() runs synchronously despite the name

What does ConfigurableModuleBuilder from @nestjs/common help you avoid?

javascript
import { ConfigurableModuleBuilder } from '@nestjs/common';

export interface MyOptions { apiKey: string; }

export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } =
  new ConfigurableModuleBuilder<MyOptions>().build();

@Module({})
export class MyModule extends ConfigurableModuleClass {}
AWriting controllers entirely
BImporting reflect-metadata
CThe repetitive boilerplate of authoring register/registerAsync (and their option-token plumbing) for configurable dynamic modules
DDefining DTOs

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.