nestjs-event-sourcing-lib
Version:
A comprehensive Event Sourcing and CQRS library for NestJS applications
44 lines • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Command = void 0;
const class_validator_1 = require("class-validator");
const uuid_1 = require("uuid");
/**
* Base class for all commands in CQRS system
*/
class Command {
constructor(aggregateId, metadata) {
this.commandId = (0, uuid_1.v4)();
this.aggregateId = aggregateId;
this.timestamp = new Date();
this.metadata = metadata;
}
async validate() {
await (0, class_validator_1.validateOrReject)(this);
}
getCommandName() {
return this.constructor.name;
}
/**
* Serializes command to JSON
*/
toJSON() {
return {
commandId: this.commandId,
aggregateId: this.aggregateId,
timestamp: this.timestamp,
metadata: this.metadata,
commandName: this.getCommandName(),
...this.getPayload(),
};
}
/**
* Gets command payload (all properties except metadata)
*/
getPayload() {
const { commandId, aggregateId, timestamp, metadata, ...payload } = this;
return payload;
}
}
exports.Command = Command;
//# sourceMappingURL=command.js.map