UNPKG

nestjs-event-sourcing-lib

Version:

A comprehensive Event Sourcing and CQRS library for NestJS applications

219 lines 10.3 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var EventSourcingModule_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.EventStoreLoadError = exports.EventStoreSaveError = exports.EventStoreConnectionError = exports.ProjectionDecorator = exports.EventDecorator = exports.OnEvent = exports.CommandDecorator = exports.CommandHandlerDecorator = exports.AggregateRootDecorator = exports.MongoDBEventStore = exports.PostgresEventStore = exports.EventBus = exports.CommandHandler = exports.Projection = exports.AggregateRoot = exports.Command = exports.Event = exports.EventSourcingModule = exports.EVENT_STORE_TOKEN = void 0; const common_1 = require("@nestjs/common"); const core_1 = require("@nestjs/core"); const cqrs_1 = require("@nestjs/cqrs"); // Core exports const event_1 = require("./core/event"); Object.defineProperty(exports, "Event", { enumerable: true, get: function () { return event_1.Event; } }); const command_1 = require("./core/command"); Object.defineProperty(exports, "Command", { enumerable: true, get: function () { return command_1.Command; } }); const aggregate_root_1 = require("./core/aggregate-root"); Object.defineProperty(exports, "AggregateRoot", { enumerable: true, get: function () { return aggregate_root_1.AggregateRoot; } }); const projection_1 = require("./core/projection"); Object.defineProperty(exports, "Projection", { enumerable: true, get: function () { return projection_1.Projection; } }); // Services const command_handler_1 = require("./services/command.handler"); Object.defineProperty(exports, "CommandHandler", { enumerable: true, get: function () { return command_handler_1.CommandHandler; } }); const event_publisher_1 = require("./services/event.publisher"); Object.defineProperty(exports, "EventBus", { enumerable: true, get: function () { return event_publisher_1.EventBus; } }); // Event Stores const postgres_event_store_1 = require("./event-stores/postgres.event-store"); Object.defineProperty(exports, "PostgresEventStore", { enumerable: true, get: function () { return postgres_event_store_1.PostgresEventStore; } }); const mongodb_event_store_1 = require("./event-stores/mongodb.event-store"); Object.defineProperty(exports, "MongoDBEventStore", { enumerable: true, get: function () { return mongodb_event_store_1.MongoDBEventStore; } }); const prisma_event_store_1 = require("./event-stores/prisma.event-store"); exports.EVENT_STORE_TOKEN = 'EVENT_STORE'; /** * Main module for Event Sourcing */ let EventSourcingModule = EventSourcingModule_1 = class EventSourcingModule { /** * Synchronous module registration */ static forRoot(options) { const eventStoreProvider = this.createEventStoreProvider(options.eventStore); const configProvider = this.createConfigProvider(options); return { module: EventSourcingModule_1, imports: [ core_1.DiscoveryModule, cqrs_1.CqrsModule, ], providers: [ configProvider, eventStoreProvider, command_handler_1.CommandHandler, event_publisher_1.EventBus, { provide: 'EVENT_SOURCING_OPTIONS', useValue: options, }, ], exports: [ exports.EVENT_STORE_TOKEN, command_handler_1.CommandHandler, event_publisher_1.EventBus, 'EVENT_SOURCING_OPTIONS', ], global: true, }; } /** * Asynchronous module registration */ static forRootAsync(options) { const asyncProviders = this.createAsyncProviders(options); return { module: EventSourcingModule_1, imports: [ core_1.DiscoveryModule, cqrs_1.CqrsModule, ], providers: [ ...asyncProviders, { provide: exports.EVENT_STORE_TOKEN, useFactory: async (config) => { const eventStore = this.createEventStoreInstance(config.eventStore); await eventStore.connect(); return eventStore; }, inject: ['EVENT_SOURCING_OPTIONS'], }, command_handler_1.CommandHandler, event_publisher_1.EventBus, ], exports: [ exports.EVENT_STORE_TOKEN, command_handler_1.CommandHandler, event_publisher_1.EventBus, 'EVENT_SOURCING_OPTIONS', ], global: true, }; } /** * Register aggregates for specific module */ static forFeature(aggregates = []) { const providers = aggregates.map(aggregate => ({ provide: aggregate, useClass: aggregate, })); return { module: EventSourcingModule_1, providers, exports: providers, }; } /** * Creates Event Store provider */ static createEventStoreProvider(config) { return { provide: exports.EVENT_STORE_TOKEN, useFactory: async () => { const eventStore = this.createEventStoreInstance(config); await eventStore.connect(); return eventStore; }, }; } /** * Creates configuration provider */ static createConfigProvider(options) { return { provide: 'EVENT_SOURCING_OPTIONS', useValue: options, }; } /** * Creates async providers */ static createAsyncProviders(options) { if (options.useFactory) { return [ { provide: 'EVENT_SOURCING_OPTIONS', useFactory: options.useFactory, inject: options.inject || [], }, ]; } if (options.useClass) { return [ { provide: 'EVENT_SOURCING_OPTIONS', useFactory: async (optionsFactory) => optionsFactory.createOptions(), inject: [options.useClass], }, { provide: options.useClass, useClass: options.useClass, }, ]; } if (options.useExisting) { return [ { provide: 'EVENT_SOURCING_OPTIONS', useFactory: async (optionsFactory) => optionsFactory.createOptions(), inject: [options.useExisting], }, ]; } throw new Error('Invalid EventSourcingModuleAsyncOptions'); } /** * Creates Event Store instance based on type */ static createEventStoreInstance(config) { const connectionOptions = { connectionString: config.connectionString, }; switch (config.type) { case 'postgres': return new postgres_event_store_1.PostgresEventStore(connectionOptions); case 'mongodb': return new mongodb_event_store_1.MongoDBEventStore(connectionOptions); case 'prisma': return new prisma_event_store_1.PrismaEventStore(connectionOptions); case 'eventstore': // TODO: Implement EventStoreDB throw new Error('EventStoreDB implementation is not yet available'); default: throw new Error(`Unsupported event store type: ${config.type}`); } } }; exports.EventSourcingModule = EventSourcingModule; EventSourcingModule.logger = new common_1.Logger(EventSourcingModule_1.name); exports.EventSourcingModule = EventSourcingModule = EventSourcingModule_1 = __decorate([ (0, common_1.Global)(), (0, common_1.Module)({}) ], EventSourcingModule); var aggregate_decorator_1 = require("./decorators/aggregate.decorator"); Object.defineProperty(exports, "AggregateRootDecorator", { enumerable: true, get: function () { return aggregate_decorator_1.AggregateRoot; } }); var command_decorator_1 = require("./decorators/command.decorator"); Object.defineProperty(exports, "CommandHandlerDecorator", { enumerable: true, get: function () { return command_decorator_1.CommandHandler; } }); Object.defineProperty(exports, "CommandDecorator", { enumerable: true, get: function () { return command_decorator_1.Command; } }); var event_decorator_1 = require("./decorators/event.decorator"); Object.defineProperty(exports, "OnEvent", { enumerable: true, get: function () { return event_decorator_1.OnEvent; } }); Object.defineProperty(exports, "EventDecorator", { enumerable: true, get: function () { return event_decorator_1.Event; } }); var projection_decorator_1 = require("./decorators/projection.decorator"); Object.defineProperty(exports, "ProjectionDecorator", { enumerable: true, get: function () { return projection_decorator_1.Projection; } }); var event_store_interface_1 = require("./core/event-store.interface"); Object.defineProperty(exports, "EventStoreConnectionError", { enumerable: true, get: function () { return event_store_interface_1.EventStoreConnectionError; } }); Object.defineProperty(exports, "EventStoreSaveError", { enumerable: true, get: function () { return event_store_interface_1.EventStoreSaveError; } }); Object.defineProperty(exports, "EventStoreLoadError", { enumerable: true, get: function () { return event_store_interface_1.EventStoreLoadError; } }); //# sourceMappingURL=event-sourcing.module.js.map