UNPKG

nestjs-event-sourcing-lib

Version:

A comprehensive Event Sourcing and CQRS library for NestJS applications

170 lines 4.99 kB
import { Event } from './event'; /** * Options for Event Store connection */ export interface EventStoreConnectionOptions { connectionString: string; database?: string; collection?: string; tableName?: string; retryAttempts?: number; retryDelay?: number; } /** * Result of saving events */ export interface SaveEventsResult { success: boolean; savedEventsCount: number; lastEventVersion: number; error?: string; } /** * Options for getting events */ export interface GetEventsOptions { fromVersion?: number; toVersion?: number; limit?: number; eventTypes?: string[]; } /** * Result of getting events */ export interface GetEventsResult { events: Event[]; hasMore: boolean; totalCount: number; } /** * Aggregate snapshot data */ export interface AggregateSnapshot { aggregateId: string; aggregateType: string; version: number; data: Record<string, any>; timestamp: Date; } /** * Event Store statistics */ export interface EventStoreStats { totalEvents: number; totalAggregates: number; totalSnapshots: number; oldestEvent?: Date; newestEvent?: Date; } /** * Event Store connection error */ export declare class EventStoreConnectionError extends Error { readonly originalError?: Error | undefined; constructor(message: string, originalError?: Error | undefined); } /** * Event save error */ export declare class EventStoreSaveError extends Error { readonly originalError?: Error | undefined; constructor(message: string, originalError?: Error | undefined); } /** * Event load error */ export declare class EventStoreLoadError extends Error { readonly originalError?: Error | undefined; constructor(message: string, originalError?: Error | undefined); } /** * Interface for Event Store */ export interface EventStore { /** * Connects to Event Store */ connect(): Promise<void>; /** * Disconnects from Event Store */ disconnect(): Promise<void>; /** * Checks if Event Store is connected */ isConnected(): boolean; /** * Saves events for aggregate */ saveEvents(aggregateId: string, events: Event[], expectedVersion?: number): Promise<SaveEventsResult>; /** * Gets events for aggregate */ getEvents(aggregateId: string, options?: GetEventsOptions): Promise<GetEventsResult>; /** * Gets all events for aggregate */ getAllEvents(aggregateId: string): Promise<Event[]>; /** * Saves aggregate snapshot */ saveSnapshot(snapshot: AggregateSnapshot): Promise<void>; /** * Gets latest aggregate snapshot */ getSnapshot(aggregateId: string): Promise<AggregateSnapshot | null>; /** * Deletes aggregate snapshot */ deleteSnapshot(aggregateId: string): Promise<void>; /** * Gets events by type */ getEventsByType(eventType: string, options?: GetEventsOptions): Promise<GetEventsResult>; /** * Gets events in time range */ getEventsByTimeRange(from: Date, to: Date, options?: GetEventsOptions): Promise<GetEventsResult>; /** * Deletes all events for aggregate */ deleteAggregate(aggregateId: string): Promise<void>; /** * Gets Event Store statistics */ getStats(): Promise<EventStoreStats>; /** * Clears all data (used for testing) */ clear(): Promise<void>; /** * Checks Event Store health */ healthCheck(): Promise<boolean>; } /** * Base abstract class for Event Store implementation */ export declare abstract class BaseEventStore implements EventStore { protected connected: boolean; protected options: EventStoreConnectionOptions; constructor(options: EventStoreConnectionOptions); abstract connect(): Promise<void>; abstract disconnect(): Promise<void>; abstract saveEvents(aggregateId: string, events: Event[], expectedVersion?: number): Promise<SaveEventsResult>; abstract getEvents(aggregateId: string, options?: GetEventsOptions): Promise<GetEventsResult>; abstract saveSnapshot(snapshot: AggregateSnapshot): Promise<void>; abstract getSnapshot(aggregateId: string): Promise<AggregateSnapshot | null>; abstract deleteSnapshot(aggregateId: string): Promise<void>; abstract getEventsByType(eventType: string, options?: GetEventsOptions): Promise<GetEventsResult>; abstract getEventsByTimeRange(from: Date, to: Date, options?: GetEventsOptions): Promise<GetEventsResult>; abstract deleteAggregate(aggregateId: string): Promise<void>; abstract getStats(): Promise<EventStoreStats>; abstract clear(): Promise<void>; abstract healthCheck(): Promise<boolean>; isConnected(): boolean; getAllEvents(aggregateId: string): Promise<Event[]>; protected setConnected(connected: boolean): void; protected getConnectionOptions(): EventStoreConnectionOptions; } //# sourceMappingURL=event-store.interface.d.ts.map