chore: ability to group events on redis event bus (#7655)

* chore: ability to group events on redis event bus

* chore: fix tests

* Update packages/modules/event-bus-redis/src/services/event-bus-redis.ts

Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>

* chore: change shape of input and body data

* chore: fix builds

* chore: address comments

* chore: fix unit test

---------

Co-authored-by: Adrien de Peretti <adrien.deperetti@gmail.com>
This commit is contained in:
Riqwan Thamir
2024-06-10 22:15:43 +02:00
committed by GitHub
parent 3b8160b564
commit 39ddba2491
24 changed files with 924 additions and 732 deletions

View File

@@ -9,70 +9,60 @@ describe("MessageAggregator", function () {
const aggregator = new MessageAggregator()
aggregator.save({
eventName: "ProductVariant.created",
body: {
metadata: {
source: "ProductService",
action: "created",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 999 },
metadata: {
source: "ProductService",
action: "created",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 999 },
})
aggregator.save({
eventName: "Product.created",
body: {
metadata: {
source: "ProductService",
action: "created",
object: "Product",
eventGroupId: "1",
},
data: { id: 1 },
metadata: {
source: "ProductService",
action: "created",
object: "Product",
eventGroupId: "1",
},
data: { id: 1 },
})
aggregator.save({
eventName: "ProductVariant.created",
body: {
metadata: {
source: "ProductService",
action: "created",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 222 },
metadata: {
source: "ProductService",
action: "created",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 222 },
})
aggregator.save({
eventName: "ProductType.detached",
body: {
metadata: {
source: "ProductService",
action: "detached",
object: "ProductType",
eventGroupId: "1",
},
data: { id: 333 },
metadata: {
source: "ProductService",
action: "detached",
object: "ProductType",
eventGroupId: "1",
},
data: { id: 333 },
})
aggregator.save({
eventName: "ProductVariant.updated",
body: {
metadata: {
source: "ProductService",
action: "updated",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 123 },
metadata: {
source: "ProductService",
action: "updated",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 123 },
})
const format = {
groupBy: ["eventName", "body.metadata.object", "body.metadata.action"],
groupBy: ["eventName", "metadata.object", "metadata.action"],
sortBy: {
"body.metadata.object": ["ProductType", "ProductVariant", "Product"],
"body.data.id": "asc",
"metadata.object": ["ProductType", "ProductVariant", "Product"],
"data.id": "asc",
},
}
@@ -85,72 +75,62 @@ describe("MessageAggregator", function () {
expect(allGroups[0]).toEqual([
{
eventName: "ProductType.detached",
body: {
metadata: {
source: "ProductService",
action: "detached",
object: "ProductType",
eventGroupId: "1",
},
data: { id: 333 },
metadata: {
source: "ProductService",
action: "detached",
object: "ProductType",
eventGroupId: "1",
},
data: { id: 333 },
},
])
expect(allGroups[1]).toEqual([
{
eventName: "ProductVariant.updated",
body: {
metadata: {
source: "ProductService",
action: "updated",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 123 },
metadata: {
source: "ProductService",
action: "updated",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 123 },
},
])
expect(allGroups[2]).toEqual([
{
eventName: "ProductVariant.created",
body: {
metadata: {
source: "ProductService",
action: "created",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 222 },
metadata: {
source: "ProductService",
action: "created",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 222 },
},
{
eventName: "ProductVariant.created",
body: {
metadata: {
source: "ProductService",
action: "created",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 999 },
metadata: {
source: "ProductService",
action: "created",
object: "ProductVariant",
eventGroupId: "1",
},
data: { id: 999 },
},
])
expect(allGroups[3]).toEqual([
{
eventName: "Product.created",
body: {
metadata: {
source: "ProductService",
action: "created",
object: "Product",
eventGroupId: "1",
},
data: { id: 1 },
metadata: {
source: "ProductService",
action: "created",
object: "Product",
eventGroupId: "1",
},
data: { id: 1 },
},
])
})

View File

@@ -45,10 +45,8 @@ export function composeMessage(
return {
eventName,
body: {
metadata,
data,
},
metadata,
data,
options,
}
}

View File

@@ -17,12 +17,9 @@ export abstract class AbstractEventBusModuleService
}
abstract emit<T>(
eventName: string,
data: T,
data: EventBusTypes.Message<T> | EventBusTypes.Message<T>[],
options: Record<string, unknown>
): Promise<void>
abstract emit<T>(data: EventBusTypes.EmitData<T>[]): Promise<void>
abstract emit<T>(data: EventBusTypes.Message<T>[]): Promise<void>
/*
Grouped events are useful when you have distributed transactions

View File

@@ -501,6 +501,7 @@ export function abstractModuleServiceFactory<
await this.eventBusModuleService_?.emit(
softDeletedEntities.map(({ id }) => ({
eventName: `${kebabCase(model.name)}.deleted`,
metadata: { source: "", action: "", object: "" },
data: { id },
}))
)