Skip to content

Commit

Permalink
Fixed client module issue with ConfigModule. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamimaj authored Oct 20, 2023
1 parent 2050088 commit 52b55c4
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 45 deletions.
4 changes: 4 additions & 0 deletions examples/client-app/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
REDIS_CONSUMER=api-1
REDIS_CONSUMER_GROUP=api
REDIS_MAX_BLOCK_TIME_MS=5000
REDIS_URL=0.0.0.0:6379
71 changes: 64 additions & 7 deletions examples/client-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/client-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@tamimaj/nestjs-redis-streams": "file:../..",
Expand Down
52 changes: 39 additions & 13 deletions examples/client-app/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@tamimaj/nestjs-redis-streams';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';

// Any class that implements createRedisStreamClientModuleOptions method.
// ConfigService is a good candidate.
Expand All @@ -21,18 +22,42 @@ class ClassOptions {

@Module({
imports: [
// Register / forRoot.
RedisStreamClientModule.register({
connection: { url: '0.0.0.0:6379' },
streams: { consumer: 'api-1', block: 5000, consumerGroup: 'api' },
responseStreams: ['users:created', 'users:created:copy'],
}),
///////////////////////////////////
// SYNC CONFIGURATION
///////////////////////////////////

// registerAsync / forRootAsync.
// RedisStreamClientModule.forRoot({
// connection: { url: '0.0.0.0:6379' },
// streams: { consumer: 'api-1', block: 5000, consumerGroup: 'api' },
// responseStreams: ['users:created', 'users:created:copy'],
// }),

///////////////////////////////////////////
// ASYNC CONFIGURATION with ConfigModule
///////////////////////////////////////////

ConfigModule.forRoot(),
RedisStreamClientModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => {
return {
connection: {
url: configService.get('REDIS_URL'),
},
streams: {
consumer: configService.get('REDIS_CONSUMER'),
consumerGroup: configService.get('REDIS_CONSUMER_GROUP'),
block: configService.get('REDIS_MAX_BLOCK_TIME_MS'),
},
responseStreams: ['users:created', 'users:created:copy'],
};
},
inject: [ConfigService],
}),

//////////////////////
// Use Factory. //
//////////////////////
///////////////////////////////////////////
// ASYNC CONFIGURATION with useFactory
///////////////////////////////////////////

// RedisStreamClientModule.registerAsync({
// useFactory: async () => {
Expand All @@ -44,9 +69,10 @@ class ClassOptions {
// },
// }),

//////////////////////
// Use Class. //
//////////////////////
///////////////////////////////////////////////////////////////////////////
// ASYNC CONFIGURATION with useClass
// class must implement createRedisStreamClientModuleOptions method.
///////////////////////////////////////////////////////////////////////////

// RedisStreamClientModule.forRootAsync({
// useClass: ClassOptions,
Expand Down
8 changes: 5 additions & 3 deletions examples/users-microservice/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions lib/redis-stream-client.core-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
} from './interfaces';
import { RedisStreamClient } from './redis.client';

const REDIS_STREAM_CLIENT_MODULE_OPTIONS = 'REDIS_STREAM_CLIENT_MODULE_OPTIONS';

@Global()
@Module({})
export class RedisStreamClientCoreModule {
Expand All @@ -26,14 +28,22 @@ export class RedisStreamClientCoreModule {
public static forRootAsync(
options: RedisStreamModuleAsyncOptions,
): DynamicModule {
console.log('forRootAsync options', options);
const redisStreamClientProvider: Provider = {
provide: RedisStreamClient,
useFactory: (options: ClientConstructorOptions) => {
return new RedisStreamClient(options);
},
inject: [REDIS_STREAM_CLIENT_MODULE_OPTIONS],
};

return {
module: RedisStreamClientCoreModule,
imports: options.imports,
providers: [...this.createAsyncProviders(options)], // here we let the logic to create the provider pending on the type of the
// useFactory, useClass or useExisting
exports: [RedisStreamClient], // this means we will export the RedisStreamClient provider
providers: [
...this.createAsyncProviders(options),
redisStreamClientProvider,
],
exports: [redisStreamClientProvider],
};
}

Expand Down Expand Up @@ -70,27 +80,17 @@ export class RedisStreamClientCoreModule {
// if is a useFactory, get options then return the RedisStreamClient
if (options.useFactory) {
return {
provide: RedisStreamClient,
useFactory: async () => {
const clientOptions = await options.useFactory();
return new RedisStreamClient(clientOptions);
},
provide: REDIS_STREAM_CLIENT_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}

// if is a useClass or useExisting,
// get the options from the ProvidedClass.createRedisStreamClientModuleOptions()
// that must be implemented by the provided class.
return {
provide: RedisStreamClient,
async useFactory(
provide: REDIS_STREAM_CLIENT_MODULE_OPTIONS,
useFactory: async (
optionsFactory: RedisStreamClientModuleOptionsFactory,
): Promise<RedisStreamClient> {
const options =
await optionsFactory.createRedisStreamClientModuleOptions();
return new RedisStreamClient(options);
},
) => optionsFactory.createRedisStreamClientModuleOptions(),
inject: [options.useClass || options.useExisting],
};
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tamimaj/nestjs-redis-streams",
"version": "1.0.0",
"version": "1.0.1",
"description": "Redis Streams Transport for NestJS.",
"author": "Tamim Abbas Aljuratli <https://tamim.es>",
"private": false,
Expand Down

0 comments on commit 52b55c4

Please sign in to comment.