Μετάβαση στο περιεχόμενο
Από την έκδοση 0.5.0 τα πακέτα συνεχίζουν ως @cqrs-ddd, με τεκμηρίωση στο aristoteliss.github.io/ddd-cqrs· οι εφαρμογές NestJS προσθέτουν το @cqrs-ddd/nestjs. Αυτός ο ιστότοπος τεκμηριώνει τη σειρά 0.4.x.

runWithCorrelationId

Αυτό το περιεχόμενο δεν είναι ακόμη διαθέσιμο στη γλώσσα σου.

runWithCorrelationId<T>(correlationId, fn): T

Defined in: correlation.store.ts:113

Run a callback within a correlation context.

Use this in non-HTTP entry points (Bull processors, RabbitMQ handlers, WebSocket gateways, cron jobs, etc.) to propagate an external correlation ID into any CQRS commands or queries dispatched inside the callback.

If correlationId is missing, empty, or not printable ASCII without surrounding spaces (the check every write to the store applies), the id is resolved via getCorrelationId (the current one, or a new one from correlationSource.create()), so fn always runs with a correlation id. A pipeline dispatched inside fn takes it as its context.correlationId.

T

string | undefined

The correlation ID to propagate. If missing or malformed, falls back to getCorrelationId.

() => T

The callback to execute within the correlation context.

T

// Bull processor
@Processor('my-queue')
export class MyProcessor {
@Process()
async handle(job: Job) {
return runWithCorrelationId(job.data.correlationId, () =>
this.commandBus.execute(new MyCommand(job.data)),
);
}
}
// RabbitMQ microservice handler
@MessagePattern('user.created')
async handle(@Payload() data: any, @Ctx() ctx: RmqContext) {
const id = ctx.getMessage().properties.correlationId;
return runWithCorrelationId(id, () =>
this.commandBus.execute(new MyCommand(data)),
);
}
// Cron job
@Cron('0 * * * *')
async hourlySync() {
return runWithCorrelationId(randomUUID(), () =>
this.commandBus.execute(new SyncCommand()),
);
}