nestjs-event-sourcing-lib
Version:
A comprehensive Event Sourcing and CQRS library for NestJS applications
209 lines • 5.35 kB
TypeScript
import { Event } from '../core/event';
import { Command } from '../core/command';
import { AggregateRoot } from '../core/aggregate-root';
import { Projection } from '../core/projection';
/**
* Event Store provider types
*/
export type EventStoreType = 'postgres' | 'mongodb' | 'eventstore' | 'memory';
/**
* Event Sourcing module configuration
*/
export interface EventSourcingModuleOptions {
eventStore: {
type: EventStoreType;
connectionString: string;
database?: string;
options?: Record<string, any>;
};
aggregates?: Array<new (...args: any[]) => AggregateRoot>;
projections?: Array<new (...args: any[]) => Projection>;
sagas?: Array<new (...args: any[]) => any>;
eventHandlers?: Array<new (...args: any[]) => any>;
commandHandlers?: Array<new (...args: any[]) => any>;
snapshotFrequency?: number;
enableSnapshots?: boolean;
eventBus?: {
type: 'local' | 'kafka' | 'rabbitmq';
options?: Record<string, any>;
};
logger?: boolean;
monitoring?: boolean;
}
/**
* Async Event Sourcing module configuration
*/
export interface EventSourcingModuleAsyncOptions {
imports?: any[];
inject?: any[];
useFactory?: (...args: any[]) => Promise<EventSourcingModuleOptions> | EventSourcingModuleOptions;
useClass?: new (...args: any[]) => EventSourcingModuleOptionsFactory;
useExisting?: any;
}
/**
* Factory for creating configuration
*/
export interface EventSourcingModuleOptionsFactory {
createEventSourcingOptions(): Promise<EventSourcingModuleOptions> | EventSourcingModuleOptions;
}
/**
* Command execution result
*/
export interface CommandResult<T = any> {
success: boolean;
result?: T;
events?: Event[];
error?: string;
aggregateId?: string;
version?: number;
}
/**
* Command execution context
*/
export interface CommandContext {
commandId: string;
aggregateId: string;
userId?: string;
sessionId?: string;
metadata?: Record<string, any>;
timestamp: Date;
}
/**
* Command handler
*/
export interface CommandHandler<T extends Command = Command, R = any> {
handle(command: T, context?: CommandContext): Promise<CommandResult<R>>;
}
/**
* Event handler
*/
export interface EventHandler<T extends Event = Event> {
handle(event: T): Promise<void>;
}
/**
* Aggregate repository
*/
export interface AggregateRepository<T extends AggregateRoot = AggregateRoot> {
save(aggregate: T): Promise<void>;
findById(id: string): Promise<T | null>;
delete(id: string): Promise<void>;
}
/**
* Projection repository
*/
export interface ProjectionRepository<T extends Projection = Projection> {
save(projection: T): Promise<void>;
findById(id: string): Promise<T | null>;
findAll(): Promise<T[]>;
delete(id: string): Promise<void>;
rebuild(): Promise<void>;
}
/**
* Event Bus for publishing events
*/
export interface EventBus {
publish(event: Event): Promise<void>;
publishAll(events: Event[]): Promise<void>;
subscribe(eventType: string, handler: EventHandler): void;
unsubscribe(eventType: string, handler: EventHandler): void;
}
/**
* Query for projections
*/
export interface Query<T = any> {
execute(): Promise<T>;
}
/**
* Query Bus for handling queries
*/
export interface QueryBus {
execute<T>(query: Query<T>): Promise<T>;
}
/**
* Snapshot Store for saving snapshots
*/
export interface SnapshotStore {
save(aggregateId: string, snapshot: any, version: number): Promise<void>;
load(aggregateId: string): Promise<{
snapshot: any;
version: number;
} | null>;
delete(aggregateId: string): Promise<void>;
}
/**
* Event metadata for Event Bus
*/
export interface EventMetadata {
eventId: string;
aggregateId: string;
eventType: string;
version: number;
timestamp: Date;
correlationId?: string;
causationId?: string;
userId?: string;
sessionId?: string;
}
/**
* Event wrapper with metadata
*/
export interface EventWrapper {
event: Event;
metadata: EventMetadata;
}
/**
* Event filter
*/
export interface EventFilter {
eventTypes?: string[];
aggregateIds?: string[];
fromTimestamp?: Date;
toTimestamp?: Date;
fromVersion?: number;
toVersion?: number;
}
/**
* Logging configuration
*/
export interface LoggingOptions {
enabled: boolean;
level: 'debug' | 'info' | 'warn' | 'error';
logCommands?: boolean;
logEvents?: boolean;
logQueries?: boolean;
logErrors?: boolean;
}
/**
* Monitoring configuration
*/
export interface MonitoringOptions {
enabled: boolean;
metricsCollector?: 'prometheus' | 'custom';
healthChecks?: boolean;
performance?: boolean;
}
/**
* Event Sourcing metrics
*/
export interface EventSourcingMetrics {
commandsProcessed: number;
eventsPublished: number;
queriesExecuted: number;
aggregatesLoaded: number;
projectionsUpdated: number;
errors: number;
averageCommandProcessingTime: number;
averageEventProcessingTime: number;
}
/**
* System health status
*/
export interface HealthStatus {
status: 'healthy' | 'degraded' | 'unhealthy';
eventStore: boolean;
eventBus: boolean;
projections: boolean;
timestamp: Date;
details?: Record<string, any>;
}
//# sourceMappingURL=index.d.ts.map