@nestjs-cqrs-eventsourcing/core
Version:
Event sourcing for nestjs CQRS
59 lines (58 loc) • 2.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Event = void 0;
const debug_1 = __importDefault(require("debug"));
const lodash_1 = __importDefault(require("lodash"));
const debug = (0, debug_1.default)('eventstore:event');
class Event {
constructor(eventStream, payload, eventMappings = {}) {
this.eventMappings = {};
if (!eventStream) {
const errStreamMsg = 'eventstream not injected!';
debug(errStreamMsg);
throw new Error(errStreamMsg);
}
if (!payload) {
const errEvtMsg = 'event not injected!';
debug(errEvtMsg);
throw new Error(errEvtMsg);
}
if (!eventStream.aggregateId) {
const errAggIdMsg = 'eventstream.aggregateId not injected!';
debug(errAggIdMsg);
throw new Error(errAggIdMsg);
}
if (!lodash_1.default.isArray(eventStream.uncommittedEvents)) {
const errAggIdMsg = 'eventstream.uncommittedEvents not injected!';
debug(errAggIdMsg);
throw new Error(errAggIdMsg);
}
this.eventMappings = eventMappings || {};
this.id = String(eventStream.aggregateId);
this.aggregateId = String(eventStream.aggregateId);
this.aggregate = String(eventStream.aggregate);
this.context = eventStream.context ? String(eventStream.context) : undefined;
this.streamRevision = -1;
this.commitId = '';
this.commitSequence = -1;
this.timestamp = undefined;
this.payload = payload || null;
this.position = undefined;
}
applyMappings() {
if (!this.eventMappings) {
throw new Error('No eventMappings defined');
}
const keys = lodash_1.default.keys(this.eventMappings);
for (const key of keys) {
const extraEventValue = this[key];
if (extraEventValue !== undefined && extraEventValue !== null) {
lodash_1.default.set(this.payload, this.eventMappings[key], extraEventValue);
}
}
}
}
exports.Event = Event;