Skip to content

addCorrelationId

addCorrelationId<T>(data): WithCorrelationId<T>

Defined in: correlation.store.ts:163

Stamp the current correlation ID onto a data object.

This is the producer-side counterpart to WithCorrelation:

  • addCorrelationId(data) → stamps the ID when enqueuing a job / publishing a message
  • @WithCorrelation() → extracts the ID when processing the job / handling the message

Both default to the 'correlationId' key, so they work together out of the box.

T extends Record<string, unknown>

T

The payload to enrich. Must be a plain object, not an array. A shallow copy is returned; the original is not mutated.

WithCorrelationId<T>

A new object with correlationId added.

If data is not a plain object.

// Bull / BullMQ
await queue.add('send-email', addCorrelationId({ userId, email }));
// RabbitMQ (ClientProxy)
this.client.emit('user.created', addCorrelationId(payload));
// Kafka
await this.producer.send({
topic: 'orders',
messages: [{ value: JSON.stringify(addCorrelationId(order)) }],
});
// PostgreSQL NOTIFY
await sql`SELECT pg_notify('events', ${JSON.stringify(addCorrelationId(data))})`;
// ⚠️ Arrays must be wrapped — passing one directly throws:
// addCorrelationId([item1, item2]); // ❌ TypeError
addCorrelationId({ items: [item1, item2] }); // ✅