Skip to content

Commit 7c8a2a4

Browse files
fix: Code config improvement + readme
1 parent 36d1a4b commit 7c8a2a4

File tree

5 files changed

+12
-29
lines changed

5 files changed

+12
-29
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ This configuration provides a solid foundation for integrating redis as part of
124124
| **Property** | **Description** | **Default Value** |
125125
|----------------------------------------|----------------------------------------------------------------------|-------------------|
126126
| **`name`** | Name of the Redis channel (e.g., `'redis-command'`). | |
127-
| **`connection`** | URI for the Redis connection`. | |
127+
| **`connection`** | Redis connection configuration (host, port, password, db). | |
128128
| **`queue`** | The Redis queue to consume messages from (e.g., `'my_app.command'`). | |
129129
| **`enableConsumer`** | Enables or disables the consumer for this channel. | `true` |
130130
| **`avoidErrorsForNotExistedHandlers`** | Avoid errors if no handler is available for the message. | `false` |
131+
| **`keyPrefix`** | Optional prefix for keys stored in Redis. | |
131132

132133
---
133134

src/channel/redis.channel-config.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { ChannelConfig } from '@nestjstools/messaging';
2+
23
export class RedisChannelConfig extends ChannelConfig {
34
public readonly connection: Connection;
45
public readonly queue: string;
6+
public readonly keyPrefix?: string;
57

68
constructor({
79
name,
@@ -11,6 +13,7 @@ export class RedisChannelConfig extends ChannelConfig {
1113
avoidErrorsForNotExistedHandlers,
1214
middlewares,
1315
normalizer,
16+
keyPrefix,
1417
}: RedisChannelConfig) {
1518
super(
1619
name,
@@ -21,6 +24,7 @@ export class RedisChannelConfig extends ChannelConfig {
2124
);
2225
this.connection = connection;
2326
this.queue = queue;
27+
this.keyPrefix = keyPrefix;
2428
}
2529
}
2630

@@ -29,9 +33,4 @@ interface Connection {
2933
port: number;
3034
password?: string;
3135
db?: number;
32-
/**
33-
* This prefix is not used as RedisOptions keyPrefix, it is used as prefix for BullMQ
34-
* Read more: https://github.com/taskforcesh/bullmq/issues/1219#issuecomment-1113903785
35-
*/
36-
keyPrefix?: string;
3736
}

src/channel/redis.channel.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ export class RedisChannel extends Channel<RedisChannelConfig> {
99
constructor(config: RedisChannelConfig) {
1010
super(config);
1111
this.queue = new Queue(config.queue, {
12-
connection: {
13-
host: config.connection.host,
14-
port: config.connection.port,
15-
password: config.connection.password,
16-
db: config.connection.db,
17-
},
18-
prefix: config.connection.keyPrefix,
12+
connection: this.config.connection,
13+
prefix: config.keyPrefix,
1914
});
2015
}
2116
}

src/consumer/redis-messaging.consumer.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ export class RedisMessagingConsumer
2626
dispatcher.dispatch(new ConsumerMessage(job.data, job.name));
2727
},
2828
{
29-
connection: {
30-
host: channel.config.connection.host,
31-
port: channel.config.connection.port,
32-
password: channel.config.connection.password,
33-
db: channel.config.connection.db,
34-
},
35-
prefix: channel.config.connection.keyPrefix,
29+
connection: this.channel.config.connection,
30+
prefix: channel.config.keyPrefix,
3631
},
3732
);
3833

@@ -53,10 +48,3 @@ export class RedisMessagingConsumer
5348
}
5449
}
5550
}
56-
57-
interface RabbitMQMessage {
58-
contentType: string;
59-
body: object;
60-
routingKey: string;
61-
headers: { [key: string]: string };
62-
}

src/message-bus/redis-message.bus.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { RedisChannel } from '../channel/redis.channel';
77
export class RedisMessageBus implements IMessageBus {
88
constructor(private readonly redisChannel: RedisChannel) {}
99

10-
async dispatch(message: RoutingMessage): Promise<object | void> {
11-
this.redisChannel.queue.add(message.messageRoutingKey, message.message);
10+
async dispatch(message: RoutingMessage): Promise<void> {
11+
await this.redisChannel.queue.add(message.messageRoutingKey, message.message);
1212
}
1313
}

0 commit comments

Comments
 (0)