Skip to content

Commit 52b55c4

Browse files
authored
Fixed client module issue with ConfigModule. (#9)
1 parent 2050088 commit 52b55c4

File tree

8 files changed

+135
-45
lines changed

8 files changed

+135
-45
lines changed

examples/client-app/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REDIS_CONSUMER=api-1
2+
REDIS_CONSUMER_GROUP=api
3+
REDIS_MAX_BLOCK_TIME_MS=5000
4+
REDIS_URL=0.0.0.0:6379

examples/client-app/package-lock.json

Lines changed: 64 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/client-app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"dependencies": {
2424
"@nestjs/common": "^9.0.0",
25+
"@nestjs/config": "^3.1.1",
2526
"@nestjs/core": "^9.0.0",
2627
"@nestjs/platform-express": "^9.0.0",
2728
"@tamimaj/nestjs-redis-streams": "file:../..",

examples/client-app/src/app.module.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '@tamimaj/nestjs-redis-streams';
66
import { AppController } from './app.controller';
77
import { AppService } from './app.service';
8+
import { ConfigModule, ConfigService } from '@nestjs/config';
89

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

2223
@Module({
2324
imports: [
24-
// Register / forRoot.
25-
RedisStreamClientModule.register({
26-
connection: { url: '0.0.0.0:6379' },
27-
streams: { consumer: 'api-1', block: 5000, consumerGroup: 'api' },
28-
responseStreams: ['users:created', 'users:created:copy'],
29-
}),
25+
///////////////////////////////////
26+
// SYNC CONFIGURATION
27+
///////////////////////////////////
3028

31-
// registerAsync / forRootAsync.
29+
// RedisStreamClientModule.forRoot({
30+
// connection: { url: '0.0.0.0:6379' },
31+
// streams: { consumer: 'api-1', block: 5000, consumerGroup: 'api' },
32+
// responseStreams: ['users:created', 'users:created:copy'],
33+
// }),
34+
35+
///////////////////////////////////////////
36+
// ASYNC CONFIGURATION with ConfigModule
37+
///////////////////////////////////////////
38+
39+
ConfigModule.forRoot(),
40+
RedisStreamClientModule.forRootAsync({
41+
imports: [ConfigModule],
42+
useFactory: (configService: ConfigService) => {
43+
return {
44+
connection: {
45+
url: configService.get('REDIS_URL'),
46+
},
47+
streams: {
48+
consumer: configService.get('REDIS_CONSUMER'),
49+
consumerGroup: configService.get('REDIS_CONSUMER_GROUP'),
50+
block: configService.get('REDIS_MAX_BLOCK_TIME_MS'),
51+
},
52+
responseStreams: ['users:created', 'users:created:copy'],
53+
};
54+
},
55+
inject: [ConfigService],
56+
}),
3257

33-
//////////////////////
34-
// Use Factory. //
35-
//////////////////////
58+
///////////////////////////////////////////
59+
// ASYNC CONFIGURATION with useFactory
60+
///////////////////////////////////////////
3661

3762
// RedisStreamClientModule.registerAsync({
3863
// useFactory: async () => {
@@ -44,9 +69,10 @@ class ClassOptions {
4469
// },
4570
// }),
4671

47-
//////////////////////
48-
// Use Class. //
49-
//////////////////////
72+
///////////////////////////////////////////////////////////////////////////
73+
// ASYNC CONFIGURATION with useClass
74+
// class must implement createRedisStreamClientModuleOptions method.
75+
///////////////////////////////////////////////////////////////////////////
5076

5177
// RedisStreamClientModule.forRootAsync({
5278
// useClass: ClassOptions,

examples/users-microservice/package-lock.json

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/redis-stream-client.core-module.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
} from './interfaces';
77
import { RedisStreamClient } from './redis.client';
88

9+
const REDIS_STREAM_CLIENT_MODULE_OPTIONS = 'REDIS_STREAM_CLIENT_MODULE_OPTIONS';
10+
911
@Global()
1012
@Module({})
1113
export class RedisStreamClientCoreModule {
@@ -26,14 +28,22 @@ export class RedisStreamClientCoreModule {
2628
public static forRootAsync(
2729
options: RedisStreamModuleAsyncOptions,
2830
): DynamicModule {
29-
console.log('forRootAsync options', options);
31+
const redisStreamClientProvider: Provider = {
32+
provide: RedisStreamClient,
33+
useFactory: (options: ClientConstructorOptions) => {
34+
return new RedisStreamClient(options);
35+
},
36+
inject: [REDIS_STREAM_CLIENT_MODULE_OPTIONS],
37+
};
3038

3139
return {
3240
module: RedisStreamClientCoreModule,
3341
imports: options.imports,
34-
providers: [...this.createAsyncProviders(options)], // here we let the logic to create the provider pending on the type of the
35-
// useFactory, useClass or useExisting
36-
exports: [RedisStreamClient], // this means we will export the RedisStreamClient provider
42+
providers: [
43+
...this.createAsyncProviders(options),
44+
redisStreamClientProvider,
45+
],
46+
exports: [redisStreamClientProvider],
3747
};
3848
}
3949

@@ -70,27 +80,17 @@ export class RedisStreamClientCoreModule {
7080
// if is a useFactory, get options then return the RedisStreamClient
7181
if (options.useFactory) {
7282
return {
73-
provide: RedisStreamClient,
74-
useFactory: async () => {
75-
const clientOptions = await options.useFactory();
76-
return new RedisStreamClient(clientOptions);
77-
},
83+
provide: REDIS_STREAM_CLIENT_MODULE_OPTIONS,
84+
useFactory: options.useFactory,
7885
inject: options.inject || [],
7986
};
8087
}
8188

82-
// if is a useClass or useExisting,
83-
// get the options from the ProvidedClass.createRedisStreamClientModuleOptions()
84-
// that must be implemented by the provided class.
8589
return {
86-
provide: RedisStreamClient,
87-
async useFactory(
90+
provide: REDIS_STREAM_CLIENT_MODULE_OPTIONS,
91+
useFactory: async (
8892
optionsFactory: RedisStreamClientModuleOptionsFactory,
89-
): Promise<RedisStreamClient> {
90-
const options =
91-
await optionsFactory.createRedisStreamClientModuleOptions();
92-
return new RedisStreamClient(options);
93-
},
93+
) => optionsFactory.createRedisStreamClientModuleOptions(),
9494
inject: [options.useClass || options.useExisting],
9595
};
9696
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tamimaj/nestjs-redis-streams",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Redis Streams Transport for NestJS.",
55
"author": "Tamim Abbas Aljuratli <https://tamim.es>",
66
"private": false,

0 commit comments

Comments
 (0)