# ConfigurableModuleOptionsFactory

**Kind:** Type

**Source:** [`packages/common/module-utils/interfaces/configurable-module-async-options.interface.ts`](https://github.com/nestjs/nest/blob/master/packages/common/module-utils/interfaces/configurable-module-async-options.interface.ts#L15)

**Part of:** [Common](subsystem-packages-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

```ts
Record< `${FactoryClassMethodKey}`, () => Promise<ModuleOptions> | ModuleOptions >
```

## Diagram

```mermaid
graph 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

```ts
import { 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 `FactoryClassMethodKey` generic argument exactly; NestJS uses this key to invoke the factory.
- Return the module options object directly or wrap it in a `Promise` for asynchronous configuration loading.
- Mark factory classes with `@Injectable()` when they depend on other providers, such as `ConfigService`.
- 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.
