-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomain-event.ts
40 lines (36 loc) · 1.19 KB
/
domain-event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
export interface IDomainEvent<T = null> {
// When the event occured
timestamp: string;
// id for the Aggregate Root that this event belongs to
aggregateId: string;
// support multi-tenant applications
tenantId: string | null;
// Optional Event data
data: T | null;
}
// Prototype for functions that will consume Domain Events
export type DomainEventHandler<
T = null,
E extends IDomainEvent<T> = IDomainEvent<T>,
> = (event: E) => void | Promise<void>;
export type IEventConstructorContext = {
aggregateId: IDomainEvent['aggregateId'];
tenantId?: IDomainEvent['tenantId'];
};
/**
* Base Domain Event class.
*
* All Domain Events MUST extend this class.
*/
export abstract class AbstractDomainEvent<T = null> implements IDomainEvent<T> {
public readonly data: IDomainEvent<T>['data'];
public readonly timestamp: IDomainEvent<T>['timestamp'];
public readonly aggregateId: IDomainEvent<T>['aggregateId'];
public readonly tenantId: IDomainEvent<T>['tenantId'];
constructor(ctx: IEventConstructorContext, data?: T) {
this.aggregateId = ctx.aggregateId;
this.timestamp = new Date().toISOString();
this.tenantId = ctx.tenantId ?? null;
this.data = data ?? null;
}
}