Skip to content

Commit

Permalink
docs: use with mikro-orm
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed Jan 28, 2024
1 parent 1db1fb5 commit 06fdd00
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,65 @@ export class AppService implements OnModuleInit, OnModuleDestroy {
}
}
```

## Q&A

### `KafkaConsumer` not working with `CreateRequestContext` of `mikro-orm`

If you don't pay attention to the order of `CreateRequestContext` decorators,
you may have problems with any of other method decorators, not only `@buka/nestjs-kafka`.

```typescript
import { Injectable } from "@nestjs/common";
import { KafkaConsumer, KafkaConsume, KafkaMessage } from "@buka/nestjs-kafka";
import { CreateRequestContext } from "@mikro-orm/mysql";

// app.consumer.js
@Injectable()
@KafkaConsumer()
export class AppConsumer {
@CreateRequestContext()
// !! KafkaConsume decorator will not work !!
@KafkaConsume("my-topic")
async finishTask(@KafkaMessage() message: string): Promise<void> {
console.log(message);
}
}
```

There are two solutions:

1. [recommend] written as two functions:

```typescript
@Injectable()
@KafkaConsumer()
export class AppConsumer {
@KafkaConsume("my-topic")
async consumeMessage(@KafkaMessage() message: string): Promise<void> {
// ... filter and format message
this.finishTask(JSON.parse(message))
}

@CreateRequestContext()
async finishTask(task: Task): Promise<void> {
// do something
console.log(task);
}
```
1. Pay attention to the order of `CreateRequestContext`:
```typescript
@Injectable()
@KafkaConsumer()
export class AppConsumer {
@KafkaConsume("my-topic")
// use CreateRequestContext as the last decorator
@CreateRequestContext()
async finishTask(@KafkaMessage() message: string): Promise<void> {
// do something
console.log(message);
}
}
```

0 comments on commit 06fdd00

Please sign in to comment.