Skip to content

Commit

Permalink
feat(rabbitmq): adds consistent rabbitmq config
Browse files Browse the repository at this point in the history
use community convention of forRoot and forRootAsync to bootstrap module

fix #34
  • Loading branch information
WonderPanda committed Apr 7, 2019
1 parent 9bba7cb commit 0923f62
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './mixins';
export * from './options';
33 changes: 33 additions & 0 deletions packages/common/src/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Type } from '@nestjs/common';
import { ModuleMetadata, Provider } from '@nestjs/common/interfaces';

export interface OptionsFactory<T> {
createOptions(): Promise<T> | T;
}

export interface AsyncOptionsFactoryProvider<T>
extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<OptionsFactory<T>>;
useClass?: Type<OptionsFactory<T>>;
useFactory?: (...args: any[]) => Promise<T> | T;
inject?: any[];
}

export function createAsyncOptionsProvider<T>(
provide: string | symbol | Type<any>,
options: AsyncOptionsFactoryProvider<T>
): Provider {
if (options.useFactory) {
return {
provide,
useFactory: options.useFactory,
inject: options.inject || []
};
}
return {
provide,
useFactory: async (optionsFactory: OptionsFactory<T>) =>
await optionsFactory.createOptions(),
inject: [options.useExisting || options.useClass]
};
}
1 change: 1 addition & 0 deletions packages/rabbitmq/src/rabbitmq.constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const RABBIT_HANDLER = Symbol('RABBIT_HANDLER');
export const RABBIT_CONFIG = Symbol('RABBIT_CONFIG');
70 changes: 68 additions & 2 deletions packages/rabbitmq/src/rabbitmq.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { DiscoveryModule, DiscoveryService } from '@nestjs-plus/discovery';
import { DynamicModule, Logger, Module, OnModuleInit } from '@nestjs/common';
import {
DynamicModule,
Logger,
Module,
OnModuleInit,
Provider
} from '@nestjs/common';
import {
AsyncOptionsFactoryProvider,
createAsyncOptionsProvider
} from '@nestjs-plus/common';
import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator';
import { groupBy } from 'lodash';
import { AmqpConnection } from './amqp/connection';
import { RABBIT_HANDLER } from './rabbitmq.constants';
import { RABBIT_HANDLER, RABBIT_CONFIG } from './rabbitmq.constants';
import { RabbitHandlerConfig, RabbitMQConfig } from './rabbitmq.interfaces';

@Module({
Expand All @@ -18,6 +28,36 @@ export class RabbitMQModule implements OnModuleInit {
private readonly externalContextCreator: ExternalContextCreator
) {}

public static forRootAsync(
asyncOptionsFactoryProvider: AsyncOptionsFactoryProvider<RabbitMQConfig>
): DynamicModule {
return {
module: RabbitMQModule,
exports: [AmqpConnection],
imports: asyncOptionsFactoryProvider.imports,
providers: this.createAsyncProviders(asyncOptionsFactoryProvider)
};
}

public static forRoot(config: RabbitMQConfig): DynamicModule {
return {
module: RabbitMQModule,
providers: [
{
provide: AmqpConnection,
useFactory: async (): Promise<AmqpConnection> => {
const connection = new AmqpConnection(config);
await connection.init();
const logger = new Logger(RabbitMQModule.name);
logger.log('Successfully connected to RabbitMQ');
return connection;
}
}
],
exports: [AmqpConnection]
};
}

public static build(config: RabbitMQConfig): DynamicModule {
return {
module: RabbitMQModule,
Expand Down Expand Up @@ -89,4 +129,30 @@ export class RabbitMQModule implements OnModuleInit {
);
}
}

private static createAsyncProviders(
asyncOptionsFactoryProvider: AsyncOptionsFactoryProvider<RabbitMQConfig>
): Provider[] {
const optionsProvider = createAsyncOptionsProvider(
RABBIT_CONFIG,
asyncOptionsFactoryProvider
);

if (
asyncOptionsFactoryProvider.useExisting ||
asyncOptionsFactoryProvider.useFactory
) {
return [optionsProvider];
}

return asyncOptionsFactoryProvider.useClass
? [
optionsProvider,
{
provide: asyncOptionsFactoryProvider.useClass,
useClass: asyncOptionsFactoryProvider.useClass
}
]
: [];
}
}
2 changes: 1 addition & 1 deletion packages/rabbitmq/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"rootDir": "./src"
},
"include": ["./src"],
"references": [{ "path": "../discovery" }]
"references": [{ "path": "../discovery" }, { "path": "../common" }]
}

0 comments on commit 0923f62

Please sign in to comment.