nestjs-event-sourcing-lib
Version:
A comprehensive Event Sourcing and CQRS library for NestJS applications
55 lines • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Event = void 0;
const uuid_1 = require("uuid");
/**
* Base class for all events in Event Sourcing system
*/
class Event {
constructor(aggregateId, payload, version = 1) {
this.id = (0, uuid_1.v4)();
this.aggregateId = aggregateId;
this.eventType = this.constructor.name;
this.timestamp = new Date();
this.version = version;
if (payload) {
Object.assign(this, payload);
}
}
/**
* Serializes event to JSON
*/
toJSON() {
return {
id: this.id,
aggregateId: this.aggregateId,
eventType: this.eventType,
timestamp: this.timestamp,
version: this.version,
...this.getPayload(),
};
}
/**
* Gets event payload (all properties except metadata)
*/
getPayload() {
const { id, aggregateId, eventType, timestamp, version, ...payload } = this;
return payload;
}
/**
* Creates event instance from JSON
*/
static fromJSON(json) {
const { id, aggregateId, eventType, timestamp, version, ...payload } = json;
const event = Object.create(this.prototype);
event.id = id;
event.aggregateId = aggregateId;
event.eventType = eventType;
event.timestamp = new Date(timestamp);
event.version = version;
Object.assign(event, payload);
return event;
}
}
exports.Event = Event;
//# sourceMappingURL=event.js.map