Kind: Type
Source: packages/common/module-utils/interfaces/configurable-module-async-options.interface.ts
Part of: Common
Interface that must be implemented by the module options factory class. Method key varies depending on the "FactoryClassMethodKey" type argument.
ConfigurableModuleOptionsFactory defines the contract for classes that asynchronously produce options for a configurable NestJS module. The factory method name is determined by the FactoryClassMethodKey generic type, allowing modules to use descriptive methods such as createOptions or createCacheOptions.
Definition
tsRecord< `${FactoryClassMethodKey}`, () => Promise<ModuleOptions> | ModuleOptions >
Diagram
mermaidgraph LR Consumer[Module Consumer] --> AsyncOptions[Async Module Options] AsyncOptions --> FactoryClass[Factory Class] FactoryClass --> FactoryInterface[ConfigurableModuleOptionsFactory] FactoryInterface --> FactoryMethod["FactoryClassMethodKey method"] FactoryMethod --> ModuleOptions[Resolved Module Options] ModuleOptions --> ConfigurableModule[Configurable Module]
Usage
tsimport { Injectable } from '@nestjs/common';
import { ConfigurableModuleOptionsFactory } from '@nestjs/common';
interface DatabaseModuleOptions {
host: string;
port: number;
database: string;
}
@Injectable()
export class DatabaseOptionsFactory
implements ConfigurableModuleOptionsFactory<
DatabaseModuleOptions,
'createDatabaseOptions'
>
{
async createDatabaseOptions(): Promise<DatabaseModuleOptions> {
return {
host: process.env.DATABASE_HOST ?? 'localhost',
port: Number(process.env.DATABASE_PORT ?? 5432),
database: process.env.DATABASE_NAME ?? 'app',
};
}
}
AI Coding Instructions
- Implement the method name specified by the
FactoryClassMethodKeygeneric argument exactly; NestJS uses this key to invoke the factory. - Return the module options object directly or wrap it in a
Promisefor asynchronous configuration loading. - Mark factory classes with
@Injectable()when they depend on other providers, such asConfigService. - Keep configuration validation and environment-variable parsing inside the factory method before returning module options.
- Ensure the resolved options type matches the configurable module's expected options interface.
Was this page helpful?