UNPKG

nestjs-event-sourcing-lib

Version:

A comprehensive Event Sourcing and CQRS library for NestJS applications

85 lines 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Projection = void 0; /** * Base class for projections (read models) in CQRS system */ class Projection { constructor(id) { /** * Last processed event version */ this.lastEventVersion = 0; this.id = id; this.lastUpdated = new Date(); } getId() { return this.id; } getLastEventVersion() { return this.lastEventVersion; } getLastUpdated() { return this.lastUpdated; } /** * Updates metadata after event processing */ updateMetadata(eventVersion) { this.lastUpdated = new Date(); if (eventVersion !== undefined) { this.lastEventVersion = eventVersion; } } /** * Checks if projection supports given event type */ supportsEvent(event) { return true; } /** * Serializes projection to JSON */ toJSON() { return { id: this.id, lastEventVersion: this.lastEventVersion, lastUpdated: this.lastUpdated, ...this.getProjectionData(), }; } /** * Gets projection data for serialization */ getProjectionData() { const { id, lastEventVersion, lastUpdated, ...data } = this; return data; } /** * Restores projection from JSON */ static fromJSON(json) { const { id, lastEventVersion, lastUpdated, ...data } = json; const projection = new this(id); projection.lastEventVersion = lastEventVersion; projection.lastUpdated = new Date(lastUpdated); Object.assign(projection, data); return projection; } /** * Resets projection to initial state */ reset() { this.lastEventVersion = 0; this.lastUpdated = new Date(); this.resetProjectionData(); } /** * Resets projection data (should be overridden in projection) */ resetProjectionData() { // Default implementation does nothing } } exports.Projection = Projection; //# sourceMappingURL=projection.js.map