Skip to content

feat: support async module intialization#2

Merged
BJTheCod3r merged 3 commits intomainfrom
feat/support-async-module-intialization
Nov 5, 2025
Merged

feat: support async module intialization#2
BJTheCod3r merged 3 commits intomainfrom
feat/support-async-module-intialization

Conversation

@tobycodes
Copy link
Contributor

@tobycodes tobycodes commented Nov 3, 2025

Overview

Added full asynchronous configuration support to NotificationModule following NestJS best practices. The module now supports three async configuration patterns: useFactory, useClass, and useExisting.

What Was Added

NotificationOptionsFactory

  • Interface for creating factory classes that provide module options
  • Supports both sync and async option resolution via createNotificationOptions()

NotificationModuleAsyncOptions

  • Configuration interface for async module registration
  • Extends ModuleMetadata to support imports
  • Includes useFactory, useClass, useExisting, and inject options

NotificationModule.forRootAsync()

  • Configures the module with async options
  • Supports dependency injection for configuration

NOTIFICATION_MODULE_OPTIONS

  • Symbol token for injecting module configuration
  • Used internally by the module lifecycle

Usage Examples

useFactory Pattern

NotificationModule.forRootAsync({
    imports: [ConfigModule],
    useFactory: (config: ConfigService) => ({
        autoDiscoverNotifications: config.get('AUTO_DISCOVER'),
        worker: {
            enabled: config.get('WORKER_ENABLED'),
            blockTimeoutSeconds: config.get('WORKER_TIMEOUT'),
        },
        databaseAdapter: {
            provide: DATABASE_ADAPTER,
            useClass: PrismaNotificationsAdapter,
        },
    }),
    inject: [ConfigService],
})

useClass Pattern

@Injectable()
class NotificationConfigService implements NotificationOptionsFactory {
    constructor(private config: ConfigService) {}

    async createNotificationOptions(): Promise<NotificationModuleOptions> {
        return {
            worker: { enabled: this.config.get('WORKER_ENABLED') },
            autoDiscoverNotifications: false,
        };
    }
}

NotificationModule.forRootAsync({
    imports: [ConfigModule],
    useClass: NotificationConfigService,
})

useExisting Pattern

NotificationModule.forRootAsync({
    imports: [ConfigModule],
    useExisting: NotificationConfigService,
})

@BJTheCod3r BJTheCod3r merged commit 2a3946a into main Nov 5, 2025
1 check passed
@BJTheCod3r BJTheCod3r deleted the feat/support-async-module-intialization branch November 6, 2025 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants