@event-driven-io/emmett 0.26.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -75,13 +75,15 @@ type CombinedReadEventMetadata<EventType extends Event = Event, EventMetaDataTyp
75
75
  type ReadEvent<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata> = EventType & {
76
76
  metadata: CombinedReadEventMetadata<EventType, EventMetaDataType>;
77
77
  };
78
- type ReadEventMetadata<GlobalPosition = undefined, StreamPosition = BigIntStreamPosition> = Readonly<{
78
+ type CommonReadEventMetadata<StreamPosition = BigIntStreamPosition> = Readonly<{
79
79
  eventId: string;
80
80
  streamPosition: StreamPosition;
81
81
  streamName: string;
82
- }> & (GlobalPosition extends undefined ? object : {
82
+ }>;
83
+ type WithGlobalPosition<GlobalPosition> = Readonly<{
83
84
  globalPosition: GlobalPosition;
84
- });
85
+ }>;
86
+ type ReadEventMetadata<GlobalPosition = undefined, StreamPosition = BigIntStreamPosition> = CommonReadEventMetadata<StreamPosition> & (GlobalPosition extends undefined ? {} : WithGlobalPosition<GlobalPosition>);
85
87
  type AnyReadEventMetadata = ReadEventMetadata<any, any>;
86
88
  type ReadEventMetadataWithGlobalPosition<GlobalPosition = BigIntGlobalPosition> = ReadEventMetadata<GlobalPosition>;
87
89
  type ReadEventMetadataWithoutGlobalPosition<StreamPosition = BigIntStreamPosition> = ReadEventMetadata<undefined, StreamPosition>;
@@ -173,7 +175,7 @@ interface EventStore<ReadEventMetadataType extends AnyReadEventMetadata = AnyRea
173
175
  readStream<EventType extends Event>(streamName: string, options?: ReadStreamOptions<StreamPositionTypeOfReadEventMetadata<ReadEventMetadataType>>): Promise<ReadStreamResult<EventType, ReadEventMetadataType>>;
174
176
  appendToStream<EventType extends Event>(streamName: string, events: EventType[], options?: AppendToStreamOptions<StreamPositionTypeOfReadEventMetadata<ReadEventMetadataType>>): Promise<AppendToStreamResult<StreamPositionTypeOfReadEventMetadata<ReadEventMetadataType>>>;
175
177
  }
176
- type EventStoreReadEventMetadata<Store extends EventStore> = Store extends EventStore<infer ReadEventMetadataType> ? ReadEventMetadataType extends ReadEventMetadata<infer GV, infer SV> ? ReadEventMetadata<GV, SV> & ReadEventMetadataType : never : never;
178
+ type EventStoreReadEventMetadata<Store extends EventStore> = Store extends EventStore<infer T> ? T extends CommonReadEventMetadata<infer SP> ? T extends WithGlobalPosition<infer GP> ? ReadEventMetadata<GP, SP> & T : ReadEventMetadata<undefined, SP> & T : never : never;
177
179
  type GlobalPositionTypeOfEventStore<Store extends EventStore> = GlobalPositionTypeOfReadEventMetadata<EventStoreReadEventMetadata<Store>>;
178
180
  type StreamPositionTypeOfEventStore<Store extends EventStore> = StreamPositionTypeOfReadEventMetadata<EventStoreReadEventMetadata<Store>>;
179
181
  type EventStoreSession<EventStoreType extends EventStore> = {
@@ -318,9 +320,40 @@ declare const isSubscriptionEvent: (event: Event) => event is GlobalSubscription
318
320
  declare const isNotInternalEvent: (event: Event) => boolean;
319
321
  type GlobalSubscriptionEvent = GlobalStreamCaughtUp;
320
322
 
323
+ type ProjectionHandlingType = 'inline' | 'async';
324
+ type ProjectionHandler<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> = (events: ReadEvent<EventType, EventMetaDataType>[], context: ProjectionHandlerContext) => Promise<void> | void;
325
+ interface ProjectionDefinition<ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> {
326
+ name?: string;
327
+ canHandle: CanHandle<Event>;
328
+ handle: ProjectionHandler<Event, ReadEventMetadataType, ProjectionHandlerContext>;
329
+ }
330
+ interface TypedProjectionDefinition<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> {
331
+ name?: string;
332
+ canHandle: CanHandle<EventType>;
333
+ handle: ProjectionHandler<EventType, EventMetaDataType, ProjectionHandlerContext>;
334
+ }
335
+ type ProjectionRegistration<HandlingType extends ProjectionHandlingType, ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> = {
336
+ type: HandlingType;
337
+ projection: ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>;
338
+ };
339
+ declare const filterProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord>(type: ProjectionHandlingType, projections: ProjectionRegistration<ProjectionHandlingType, ReadEventMetadataType, ProjectionHandlerContext>[]) => ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>[];
340
+ declare const projection: <EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends TypedProjectionDefinition<EventType, EventMetaDataType, ProjectionHandlerContext> = TypedProjectionDefinition<EventType, EventMetaDataType, ProjectionHandlerContext>>(definition: ProjectionDefintionType) => ProjectionDefintionType;
341
+ declare const inlineProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"inline", ReadEventMetadataType, ProjectionHandlerContext>[];
342
+ declare const asyncProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"async", ReadEventMetadataType, ProjectionHandlerContext>[];
343
+ declare const projections: {
344
+ inline: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"inline", ReadEventMetadataType, ProjectionHandlerContext>[];
345
+ async: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"async", ReadEventMetadataType, ProjectionHandlerContext>[];
346
+ };
347
+
321
348
  declare const InMemoryEventStoreDefaultStreamVersion = 0n;
322
349
  type InMemoryEventStore = EventStore<ReadEventMetadataWithGlobalPosition>;
323
- type InMemoryEventStoreOptions = DefaultEventStoreOptions<InMemoryEventStore>;
350
+ type InMemoryReadEventMetadata = ReadEventMetadataWithGlobalPosition;
351
+ type InMemoryProjectionHandlerContext = {
352
+ eventStore: InMemoryEventStore;
353
+ };
354
+ type InMemoryEventStoreOptions = DefaultEventStoreOptions<InMemoryEventStore> & {
355
+ projections?: ProjectionRegistration<'inline', InMemoryReadEventMetadata, InMemoryProjectionHandlerContext>[];
356
+ };
324
357
  type InMemoryReadEvent<EventType extends Event = Event> = ReadEvent<EventType, ReadEventMetadataWithGlobalPosition>;
325
358
  declare const getInMemoryEventStore: (eventStoreOptions?: InMemoryEventStoreOptions) => InMemoryEventStore;
326
359
 
@@ -409,30 +442,15 @@ declare const CommandHandler: <State, StreamEvent extends Event>(options: Comman
409
442
  type DeciderCommandHandlerOptions<State, CommandType extends Command, StreamEvent extends Event> = CommandHandlerOptions<State, StreamEvent> & Decider<State, CommandType, StreamEvent>;
410
443
  declare const DeciderCommandHandler: <State, CommandType extends Command, StreamEvent extends Event>(options: DeciderCommandHandlerOptions<State, CommandType, StreamEvent>) => <Store extends EventStore>(eventStore: Store, id: string, command: CommandType, handleOptions?: HandleOptions<Store>) => Promise<CommandHandlerResult<State, StreamEvent, Store>>;
411
444
 
412
- type ProjectionHandlingType = 'inline' | 'async';
413
- type ProjectionHandler<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> = (events: ReadEvent<EventType, EventMetaDataType>[], context: ProjectionHandlerContext) => Promise<void> | void;
414
- interface ProjectionDefinition<ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> {
415
- name?: string;
416
- canHandle: CanHandle<Event>;
417
- handle: ProjectionHandler<Event, ReadEventMetadataType, ProjectionHandlerContext>;
445
+ interface DocumentsCollection<T> {
446
+ store: (id: string, obj: T) => void;
447
+ delete: (id: string) => void;
448
+ get: (id: string) => T | null;
418
449
  }
419
- interface TypedProjectionDefinition<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> {
420
- name?: string;
421
- canHandle: CanHandle<EventType>;
422
- handle: ProjectionHandler<EventType, EventMetaDataType, ProjectionHandlerContext>;
450
+ interface Database {
451
+ collection: <T>(name: string) => DocumentsCollection<T>;
423
452
  }
424
- type ProjectionRegistration<HandlingType extends ProjectionHandlingType, ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> = {
425
- type: HandlingType;
426
- projection: ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>;
427
- };
428
- declare const filterProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord>(type: ProjectionHandlingType, projections: ProjectionRegistration<ProjectionHandlingType, ReadEventMetadataType, ProjectionHandlerContext>[]) => ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>[];
429
- declare const projection: <EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends TypedProjectionDefinition<EventType, EventMetaDataType, ProjectionHandlerContext> = TypedProjectionDefinition<EventType, EventMetaDataType, ProjectionHandlerContext>>(definition: ProjectionDefintionType) => ProjectionDefintionType;
430
- declare const inlineProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"inline", ReadEventMetadataType, ProjectionHandlerContext>[];
431
- declare const asyncProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"async", ReadEventMetadataType, ProjectionHandlerContext>[];
432
- declare const projections: {
433
- inline: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"inline", ReadEventMetadataType, ProjectionHandlerContext>[];
434
- async: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"async", ReadEventMetadataType, ProjectionHandlerContext>[];
435
- };
453
+ declare const getInMemoryDatabase: () => Database;
436
454
 
437
455
  declare class ParseError extends Error {
438
456
  constructor(text: string);
@@ -665,7 +683,14 @@ declare const assertThatArray: <T>(array: T[]) => {
665
683
 
666
684
  type ErrorCheck<ErrorType> = (error: ErrorType) => boolean;
667
685
  type ThenThrows<ErrorType extends Error> = (() => void) | ((errorConstructor: ErrorConstructor<ErrorType>) => void) | ((errorCheck: ErrorCheck<ErrorType>) => void) | ((errorConstructor: ErrorConstructor<ErrorType>, errorCheck?: ErrorCheck<ErrorType>) => void);
668
- type DeciderSpecfication<Command, Event> = (givenEvents: Event | Event[]) => {
686
+ type AsyncDeciderSpecification<Command, Event> = (givenEvents: Event | Event[]) => {
687
+ when: (command: Command) => {
688
+ then: (expectedEvents: Event | Event[]) => Promise<void>;
689
+ thenNothingHappened: () => Promise<void>;
690
+ thenThrows: <ErrorType extends Error = Error>(...args: Parameters<ThenThrows<ErrorType>>) => Promise<void>;
691
+ };
692
+ };
693
+ type DeciderSpecification<Command, Event> = (givenEvents: Event | Event[]) => {
669
694
  when: (command: Command) => {
670
695
  then: (expectedEvents: Event | Event[]) => void;
671
696
  thenNothingHappened: () => void;
@@ -673,12 +698,18 @@ type DeciderSpecfication<Command, Event> = (givenEvents: Event | Event[]) => {
673
698
  };
674
699
  };
675
700
  declare const DeciderSpecification: {
676
- for: <Command, Event, State>(decider: {
677
- decide: (command: Command, state: State) => Event | Event[];
678
- evolve: (state: State, event: Event) => State;
679
- initialState: () => State;
680
- }) => DeciderSpecfication<Command, Event>;
701
+ for: typeof deciderSpecificationFor;
681
702
  };
703
+ declare function deciderSpecificationFor<Command, Event, State>(decider: {
704
+ decide: (command: Command, state: State) => Event | Event[];
705
+ evolve: (state: State, event: Event) => State;
706
+ initialState: () => State;
707
+ }): DeciderSpecification<Command, Event>;
708
+ declare function deciderSpecificationFor<Command, Event, State>(decider: {
709
+ decide: (command: Command, state: State) => Promise<Event | Event[]>;
710
+ evolve: (state: State, event: Event) => State;
711
+ initialState: () => State;
712
+ }): AsyncDeciderSpecification<Command, Event>;
682
713
 
683
714
  type TestEventStream<EventType extends Event = Event> = [
684
715
  string,
@@ -705,4 +736,4 @@ declare const assertNotEmptyString: (value: unknown) => string;
705
736
  declare const assertPositiveNumber: (value: unknown) => number;
706
737
  declare const assertUnsignedBigInt: (value: string) => bigint;
707
738
 
708
- export { type AcquireLockOptions, type AfterEventStoreCommitHandler, type AggregateStreamOptions, type AggregateStreamResult, type AggregateStreamResultOfEventStore, type AggregateStreamResultWithGlobalPosition, type AnyReadEventMetadata, type AppendStreamResultOfEventStore, type AppendToStreamOptions, type AppendToStreamResult, type AppendToStreamResultWithGlobalPosition, type ArgumentMatcher, AssertionError, type AsyncRetryOptions, type BigIntGlobalPosition, type BigIntStreamPosition, BinaryJsonDecoder, type Brand, type CanHandle, CaughtUpTransformStream, type Closeable, type CombinedReadEventMetadata, type Command, type CommandBus, type CommandDataOf, CommandHandler, type CommandHandlerOptions, type CommandHandlerResult, type CommandHandlerRetryOptions, CommandHandlerStreamVersionConflictRetryOptions, type CommandMetaDataOf, type CommandProcessor, type CommandSender, type CommandTypeOf, CompositeDecoder, ConcurrencyError, type CreateCommandType, type CreateEventType, type Decider, DeciderCommandHandler, type DeciderCommandHandlerOptions, type DeciderSpecfication, DeciderSpecification, type Decoder, type DeepReadonly, type DefaultCommandMetadata, DefaultDecoder, type DefaultEventStoreOptions, type DefaultRecord, type EnqueueTaskOptions, type Equatable, ErrorConstructor, type Event, type EventBus, type EventDataOf, type EventMetaDataOf, type EventProcessor, type EventStore, type EventStoreReadEventMetadata, type EventStoreSession, type EventStoreSessionFactory, type EventStoreWrapper, type EventTypeOf, type EventsPublisher, type ExpectedStreamVersion, type ExpectedStreamVersionGeneral, type ExpectedStreamVersionWithValue, ExpectedVersionConflictError, type Flavour, type GlobalPositionTypeOfEventStore, type GlobalPositionTypeOfReadEventMetadata, type GlobalStreamCaughtUp, GlobalStreamCaughtUpType, type GlobalSubscriptionEvent, type HandleOptions, type InMemoryEventStore, InMemoryEventStoreDefaultStreamVersion, type InMemoryEventStoreOptions, type InMemoryReadEvent, InProcessLock, JSONParser, JsonDecoder, type Lock, type LockOptions, type Mapper, type MapperArgs, type MessageBus, type MessageHandler, type MessageProcessor, type MessageScheduler, type MockedFunction, type Mutable, NO_CONCURRENCY_CHECK, NoRetries, type NonNullable, ObjectDecoder, ParseError, type ParseOptions, type ProjectionDefinition, type ProjectionHandler, type ProjectionHandlingType, type ProjectionRegistration, type ReadEvent, type ReadEventMetadata, type ReadEventMetadataWithGlobalPosition, type ReadEventMetadataWithoutGlobalPosition, type ReadStreamOptions, type ReadStreamResult, type ReleaseLockOptions, STREAM_DOES_NOT_EXIST, STREAM_EXISTS, type ScheduleOptions, type ScheduledMessage, type ScheduledMessageProcessor, type StreamPositionTypeOfEventStore, type StreamPositionTypeOfReadEventMetadata, StreamingCoordinator, StringDecoder, type StringifyOptions, type Task, type TaskContext, TaskProcessor, type TaskProcessorOptions, type TaskQueue, type TaskQueueItem, type TestEventStream, type ThenThrows, type TypedProjectionDefinition, ValidationErrors, type Workflow, type WorkflowCommand, type WorkflowEvent, type WorkflowOutput, WrapEventStore, accept, argMatches, argValue, arrayUtils, assertDeepEqual, assertDoesNotThrow, assertEqual, assertExpectedVersionMatchesCurrent, assertFails, assertFalse, assertIsNotNull, assertIsNull, assertMatches, assertNotDeepEqual, assertNotEmptyString, assertNotEqual, assertOk, assertPositiveNumber, assertRejects, assertThat, assertThatArray, assertThrows, assertThrowsAsync, assertTrue, assertUnsignedBigInt, asyncProjections, asyncRetry, canCreateEventStoreSession, caughtUpEventFrom, collect, command, complete, concatUint8Arrays, deepEquals, error, event, filterProjections, formatDateToUtcYYYYMMDD, forwardToMessageBus, getInMemoryEventStore, getInMemoryMessageBus, globalStreamCaughtUp, ignore, inlineProjections, isEquatable, isExpectedVersionConflictError, isGlobalStreamCaughtUp, isNotInternalEvent, isNumber, isString, isSubscriptionEvent, isSubset, isValidYYYYMMDD, matchesExpectedVersion, merge, nulloSessionFactory, parseDateFromUtcYYYYMMDD, projection, projections, publish, reply, restream, schedule, send, streamGenerators, streamTrackingGlobalPosition, streamTransformations, sum, tryPublishMessagesAfterCommit, verifyThat };
739
+ export { type AcquireLockOptions, type AfterEventStoreCommitHandler, type AggregateStreamOptions, type AggregateStreamResult, type AggregateStreamResultOfEventStore, type AggregateStreamResultWithGlobalPosition, type AnyReadEventMetadata, type AppendStreamResultOfEventStore, type AppendToStreamOptions, type AppendToStreamResult, type AppendToStreamResultWithGlobalPosition, type ArgumentMatcher, AssertionError, type AsyncDeciderSpecification, type AsyncRetryOptions, type BigIntGlobalPosition, type BigIntStreamPosition, BinaryJsonDecoder, type Brand, type CanHandle, CaughtUpTransformStream, type Closeable, type CombinedReadEventMetadata, type Command, type CommandBus, type CommandDataOf, CommandHandler, type CommandHandlerOptions, type CommandHandlerResult, type CommandHandlerRetryOptions, CommandHandlerStreamVersionConflictRetryOptions, type CommandMetaDataOf, type CommandProcessor, type CommandSender, type CommandTypeOf, type CommonReadEventMetadata, CompositeDecoder, ConcurrencyError, type CreateCommandType, type CreateEventType, type Database, type Decider, DeciderCommandHandler, type DeciderCommandHandlerOptions, DeciderSpecification, type Decoder, type DeepReadonly, type DefaultCommandMetadata, DefaultDecoder, type DefaultEventStoreOptions, type DefaultRecord, type DocumentsCollection, type EnqueueTaskOptions, type Equatable, ErrorConstructor, type Event, type EventBus, type EventDataOf, type EventMetaDataOf, type EventProcessor, type EventStore, type EventStoreReadEventMetadata, type EventStoreSession, type EventStoreSessionFactory, type EventStoreWrapper, type EventTypeOf, type EventsPublisher, type ExpectedStreamVersion, type ExpectedStreamVersionGeneral, type ExpectedStreamVersionWithValue, ExpectedVersionConflictError, type Flavour, type GlobalPositionTypeOfEventStore, type GlobalPositionTypeOfReadEventMetadata, type GlobalStreamCaughtUp, GlobalStreamCaughtUpType, type GlobalSubscriptionEvent, type HandleOptions, type InMemoryEventStore, InMemoryEventStoreDefaultStreamVersion, type InMemoryEventStoreOptions, type InMemoryProjectionHandlerContext, type InMemoryReadEvent, type InMemoryReadEventMetadata, InProcessLock, JSONParser, JsonDecoder, type Lock, type LockOptions, type Mapper, type MapperArgs, type MessageBus, type MessageHandler, type MessageProcessor, type MessageScheduler, type MockedFunction, type Mutable, NO_CONCURRENCY_CHECK, NoRetries, type NonNullable, ObjectDecoder, ParseError, type ParseOptions, type ProjectionDefinition, type ProjectionHandler, type ProjectionHandlingType, type ProjectionRegistration, type ReadEvent, type ReadEventMetadata, type ReadEventMetadataWithGlobalPosition, type ReadEventMetadataWithoutGlobalPosition, type ReadStreamOptions, type ReadStreamResult, type ReleaseLockOptions, STREAM_DOES_NOT_EXIST, STREAM_EXISTS, type ScheduleOptions, type ScheduledMessage, type ScheduledMessageProcessor, type StreamPositionTypeOfEventStore, type StreamPositionTypeOfReadEventMetadata, StreamingCoordinator, StringDecoder, type StringifyOptions, type Task, type TaskContext, TaskProcessor, type TaskProcessorOptions, type TaskQueue, type TaskQueueItem, type TestEventStream, type ThenThrows, type TypedProjectionDefinition, ValidationErrors, type WithGlobalPosition, type Workflow, type WorkflowCommand, type WorkflowEvent, type WorkflowOutput, WrapEventStore, accept, argMatches, argValue, arrayUtils, assertDeepEqual, assertDoesNotThrow, assertEqual, assertExpectedVersionMatchesCurrent, assertFails, assertFalse, assertIsNotNull, assertIsNull, assertMatches, assertNotDeepEqual, assertNotEmptyString, assertNotEqual, assertOk, assertPositiveNumber, assertRejects, assertThat, assertThatArray, assertThrows, assertThrowsAsync, assertTrue, assertUnsignedBigInt, asyncProjections, asyncRetry, canCreateEventStoreSession, caughtUpEventFrom, collect, command, complete, concatUint8Arrays, deepEquals, error, event, filterProjections, formatDateToUtcYYYYMMDD, forwardToMessageBus, getInMemoryDatabase, getInMemoryEventStore, getInMemoryMessageBus, globalStreamCaughtUp, ignore, inlineProjections, isEquatable, isExpectedVersionConflictError, isGlobalStreamCaughtUp, isNotInternalEvent, isNumber, isString, isSubscriptionEvent, isSubset, isValidYYYYMMDD, matchesExpectedVersion, merge, nulloSessionFactory, parseDateFromUtcYYYYMMDD, projection, projections, publish, reply, restream, schedule, send, streamGenerators, streamTrackingGlobalPosition, streamTransformations, sum, tryPublishMessagesAfterCommit, verifyThat };
package/dist/index.d.ts CHANGED
@@ -75,13 +75,15 @@ type CombinedReadEventMetadata<EventType extends Event = Event, EventMetaDataTyp
75
75
  type ReadEvent<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata> = EventType & {
76
76
  metadata: CombinedReadEventMetadata<EventType, EventMetaDataType>;
77
77
  };
78
- type ReadEventMetadata<GlobalPosition = undefined, StreamPosition = BigIntStreamPosition> = Readonly<{
78
+ type CommonReadEventMetadata<StreamPosition = BigIntStreamPosition> = Readonly<{
79
79
  eventId: string;
80
80
  streamPosition: StreamPosition;
81
81
  streamName: string;
82
- }> & (GlobalPosition extends undefined ? object : {
82
+ }>;
83
+ type WithGlobalPosition<GlobalPosition> = Readonly<{
83
84
  globalPosition: GlobalPosition;
84
- });
85
+ }>;
86
+ type ReadEventMetadata<GlobalPosition = undefined, StreamPosition = BigIntStreamPosition> = CommonReadEventMetadata<StreamPosition> & (GlobalPosition extends undefined ? {} : WithGlobalPosition<GlobalPosition>);
85
87
  type AnyReadEventMetadata = ReadEventMetadata<any, any>;
86
88
  type ReadEventMetadataWithGlobalPosition<GlobalPosition = BigIntGlobalPosition> = ReadEventMetadata<GlobalPosition>;
87
89
  type ReadEventMetadataWithoutGlobalPosition<StreamPosition = BigIntStreamPosition> = ReadEventMetadata<undefined, StreamPosition>;
@@ -173,7 +175,7 @@ interface EventStore<ReadEventMetadataType extends AnyReadEventMetadata = AnyRea
173
175
  readStream<EventType extends Event>(streamName: string, options?: ReadStreamOptions<StreamPositionTypeOfReadEventMetadata<ReadEventMetadataType>>): Promise<ReadStreamResult<EventType, ReadEventMetadataType>>;
174
176
  appendToStream<EventType extends Event>(streamName: string, events: EventType[], options?: AppendToStreamOptions<StreamPositionTypeOfReadEventMetadata<ReadEventMetadataType>>): Promise<AppendToStreamResult<StreamPositionTypeOfReadEventMetadata<ReadEventMetadataType>>>;
175
177
  }
176
- type EventStoreReadEventMetadata<Store extends EventStore> = Store extends EventStore<infer ReadEventMetadataType> ? ReadEventMetadataType extends ReadEventMetadata<infer GV, infer SV> ? ReadEventMetadata<GV, SV> & ReadEventMetadataType : never : never;
178
+ type EventStoreReadEventMetadata<Store extends EventStore> = Store extends EventStore<infer T> ? T extends CommonReadEventMetadata<infer SP> ? T extends WithGlobalPosition<infer GP> ? ReadEventMetadata<GP, SP> & T : ReadEventMetadata<undefined, SP> & T : never : never;
177
179
  type GlobalPositionTypeOfEventStore<Store extends EventStore> = GlobalPositionTypeOfReadEventMetadata<EventStoreReadEventMetadata<Store>>;
178
180
  type StreamPositionTypeOfEventStore<Store extends EventStore> = StreamPositionTypeOfReadEventMetadata<EventStoreReadEventMetadata<Store>>;
179
181
  type EventStoreSession<EventStoreType extends EventStore> = {
@@ -318,9 +320,40 @@ declare const isSubscriptionEvent: (event: Event) => event is GlobalSubscription
318
320
  declare const isNotInternalEvent: (event: Event) => boolean;
319
321
  type GlobalSubscriptionEvent = GlobalStreamCaughtUp;
320
322
 
323
+ type ProjectionHandlingType = 'inline' | 'async';
324
+ type ProjectionHandler<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> = (events: ReadEvent<EventType, EventMetaDataType>[], context: ProjectionHandlerContext) => Promise<void> | void;
325
+ interface ProjectionDefinition<ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> {
326
+ name?: string;
327
+ canHandle: CanHandle<Event>;
328
+ handle: ProjectionHandler<Event, ReadEventMetadataType, ProjectionHandlerContext>;
329
+ }
330
+ interface TypedProjectionDefinition<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> {
331
+ name?: string;
332
+ canHandle: CanHandle<EventType>;
333
+ handle: ProjectionHandler<EventType, EventMetaDataType, ProjectionHandlerContext>;
334
+ }
335
+ type ProjectionRegistration<HandlingType extends ProjectionHandlingType, ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> = {
336
+ type: HandlingType;
337
+ projection: ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>;
338
+ };
339
+ declare const filterProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord>(type: ProjectionHandlingType, projections: ProjectionRegistration<ProjectionHandlingType, ReadEventMetadataType, ProjectionHandlerContext>[]) => ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>[];
340
+ declare const projection: <EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends TypedProjectionDefinition<EventType, EventMetaDataType, ProjectionHandlerContext> = TypedProjectionDefinition<EventType, EventMetaDataType, ProjectionHandlerContext>>(definition: ProjectionDefintionType) => ProjectionDefintionType;
341
+ declare const inlineProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"inline", ReadEventMetadataType, ProjectionHandlerContext>[];
342
+ declare const asyncProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"async", ReadEventMetadataType, ProjectionHandlerContext>[];
343
+ declare const projections: {
344
+ inline: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"inline", ReadEventMetadataType, ProjectionHandlerContext>[];
345
+ async: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"async", ReadEventMetadataType, ProjectionHandlerContext>[];
346
+ };
347
+
321
348
  declare const InMemoryEventStoreDefaultStreamVersion = 0n;
322
349
  type InMemoryEventStore = EventStore<ReadEventMetadataWithGlobalPosition>;
323
- type InMemoryEventStoreOptions = DefaultEventStoreOptions<InMemoryEventStore>;
350
+ type InMemoryReadEventMetadata = ReadEventMetadataWithGlobalPosition;
351
+ type InMemoryProjectionHandlerContext = {
352
+ eventStore: InMemoryEventStore;
353
+ };
354
+ type InMemoryEventStoreOptions = DefaultEventStoreOptions<InMemoryEventStore> & {
355
+ projections?: ProjectionRegistration<'inline', InMemoryReadEventMetadata, InMemoryProjectionHandlerContext>[];
356
+ };
324
357
  type InMemoryReadEvent<EventType extends Event = Event> = ReadEvent<EventType, ReadEventMetadataWithGlobalPosition>;
325
358
  declare const getInMemoryEventStore: (eventStoreOptions?: InMemoryEventStoreOptions) => InMemoryEventStore;
326
359
 
@@ -409,30 +442,15 @@ declare const CommandHandler: <State, StreamEvent extends Event>(options: Comman
409
442
  type DeciderCommandHandlerOptions<State, CommandType extends Command, StreamEvent extends Event> = CommandHandlerOptions<State, StreamEvent> & Decider<State, CommandType, StreamEvent>;
410
443
  declare const DeciderCommandHandler: <State, CommandType extends Command, StreamEvent extends Event>(options: DeciderCommandHandlerOptions<State, CommandType, StreamEvent>) => <Store extends EventStore>(eventStore: Store, id: string, command: CommandType, handleOptions?: HandleOptions<Store>) => Promise<CommandHandlerResult<State, StreamEvent, Store>>;
411
444
 
412
- type ProjectionHandlingType = 'inline' | 'async';
413
- type ProjectionHandler<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> = (events: ReadEvent<EventType, EventMetaDataType>[], context: ProjectionHandlerContext) => Promise<void> | void;
414
- interface ProjectionDefinition<ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> {
415
- name?: string;
416
- canHandle: CanHandle<Event>;
417
- handle: ProjectionHandler<Event, ReadEventMetadataType, ProjectionHandlerContext>;
445
+ interface DocumentsCollection<T> {
446
+ store: (id: string, obj: T) => void;
447
+ delete: (id: string) => void;
448
+ get: (id: string) => T | null;
418
449
  }
419
- interface TypedProjectionDefinition<EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> {
420
- name?: string;
421
- canHandle: CanHandle<EventType>;
422
- handle: ProjectionHandler<EventType, EventMetaDataType, ProjectionHandlerContext>;
450
+ interface Database {
451
+ collection: <T>(name: string) => DocumentsCollection<T>;
423
452
  }
424
- type ProjectionRegistration<HandlingType extends ProjectionHandlingType, ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord> = {
425
- type: HandlingType;
426
- projection: ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>;
427
- };
428
- declare const filterProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord>(type: ProjectionHandlingType, projections: ProjectionRegistration<ProjectionHandlingType, ReadEventMetadataType, ProjectionHandlerContext>[]) => ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>[];
429
- declare const projection: <EventType extends Event = Event, EventMetaDataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends TypedProjectionDefinition<EventType, EventMetaDataType, ProjectionHandlerContext> = TypedProjectionDefinition<EventType, EventMetaDataType, ProjectionHandlerContext>>(definition: ProjectionDefintionType) => ProjectionDefintionType;
430
- declare const inlineProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"inline", ReadEventMetadataType, ProjectionHandlerContext>[];
431
- declare const asyncProjections: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"async", ReadEventMetadataType, ProjectionHandlerContext>[];
432
- declare const projections: {
433
- inline: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"inline", ReadEventMetadataType, ProjectionHandlerContext>[];
434
- async: <ReadEventMetadataType extends AnyReadEventMetadata = AnyReadEventMetadata, ProjectionHandlerContext extends DefaultRecord = DefaultRecord, ProjectionDefintionType extends ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext> = ProjectionDefinition<ReadEventMetadataType, ProjectionHandlerContext>>(definitions: ProjectionDefintionType[]) => ProjectionRegistration<"async", ReadEventMetadataType, ProjectionHandlerContext>[];
435
- };
453
+ declare const getInMemoryDatabase: () => Database;
436
454
 
437
455
  declare class ParseError extends Error {
438
456
  constructor(text: string);
@@ -665,7 +683,14 @@ declare const assertThatArray: <T>(array: T[]) => {
665
683
 
666
684
  type ErrorCheck<ErrorType> = (error: ErrorType) => boolean;
667
685
  type ThenThrows<ErrorType extends Error> = (() => void) | ((errorConstructor: ErrorConstructor<ErrorType>) => void) | ((errorCheck: ErrorCheck<ErrorType>) => void) | ((errorConstructor: ErrorConstructor<ErrorType>, errorCheck?: ErrorCheck<ErrorType>) => void);
668
- type DeciderSpecfication<Command, Event> = (givenEvents: Event | Event[]) => {
686
+ type AsyncDeciderSpecification<Command, Event> = (givenEvents: Event | Event[]) => {
687
+ when: (command: Command) => {
688
+ then: (expectedEvents: Event | Event[]) => Promise<void>;
689
+ thenNothingHappened: () => Promise<void>;
690
+ thenThrows: <ErrorType extends Error = Error>(...args: Parameters<ThenThrows<ErrorType>>) => Promise<void>;
691
+ };
692
+ };
693
+ type DeciderSpecification<Command, Event> = (givenEvents: Event | Event[]) => {
669
694
  when: (command: Command) => {
670
695
  then: (expectedEvents: Event | Event[]) => void;
671
696
  thenNothingHappened: () => void;
@@ -673,12 +698,18 @@ type DeciderSpecfication<Command, Event> = (givenEvents: Event | Event[]) => {
673
698
  };
674
699
  };
675
700
  declare const DeciderSpecification: {
676
- for: <Command, Event, State>(decider: {
677
- decide: (command: Command, state: State) => Event | Event[];
678
- evolve: (state: State, event: Event) => State;
679
- initialState: () => State;
680
- }) => DeciderSpecfication<Command, Event>;
701
+ for: typeof deciderSpecificationFor;
681
702
  };
703
+ declare function deciderSpecificationFor<Command, Event, State>(decider: {
704
+ decide: (command: Command, state: State) => Event | Event[];
705
+ evolve: (state: State, event: Event) => State;
706
+ initialState: () => State;
707
+ }): DeciderSpecification<Command, Event>;
708
+ declare function deciderSpecificationFor<Command, Event, State>(decider: {
709
+ decide: (command: Command, state: State) => Promise<Event | Event[]>;
710
+ evolve: (state: State, event: Event) => State;
711
+ initialState: () => State;
712
+ }): AsyncDeciderSpecification<Command, Event>;
682
713
 
683
714
  type TestEventStream<EventType extends Event = Event> = [
684
715
  string,
@@ -705,4 +736,4 @@ declare const assertNotEmptyString: (value: unknown) => string;
705
736
  declare const assertPositiveNumber: (value: unknown) => number;
706
737
  declare const assertUnsignedBigInt: (value: string) => bigint;
707
738
 
708
- export { type AcquireLockOptions, type AfterEventStoreCommitHandler, type AggregateStreamOptions, type AggregateStreamResult, type AggregateStreamResultOfEventStore, type AggregateStreamResultWithGlobalPosition, type AnyReadEventMetadata, type AppendStreamResultOfEventStore, type AppendToStreamOptions, type AppendToStreamResult, type AppendToStreamResultWithGlobalPosition, type ArgumentMatcher, AssertionError, type AsyncRetryOptions, type BigIntGlobalPosition, type BigIntStreamPosition, BinaryJsonDecoder, type Brand, type CanHandle, CaughtUpTransformStream, type Closeable, type CombinedReadEventMetadata, type Command, type CommandBus, type CommandDataOf, CommandHandler, type CommandHandlerOptions, type CommandHandlerResult, type CommandHandlerRetryOptions, CommandHandlerStreamVersionConflictRetryOptions, type CommandMetaDataOf, type CommandProcessor, type CommandSender, type CommandTypeOf, CompositeDecoder, ConcurrencyError, type CreateCommandType, type CreateEventType, type Decider, DeciderCommandHandler, type DeciderCommandHandlerOptions, type DeciderSpecfication, DeciderSpecification, type Decoder, type DeepReadonly, type DefaultCommandMetadata, DefaultDecoder, type DefaultEventStoreOptions, type DefaultRecord, type EnqueueTaskOptions, type Equatable, ErrorConstructor, type Event, type EventBus, type EventDataOf, type EventMetaDataOf, type EventProcessor, type EventStore, type EventStoreReadEventMetadata, type EventStoreSession, type EventStoreSessionFactory, type EventStoreWrapper, type EventTypeOf, type EventsPublisher, type ExpectedStreamVersion, type ExpectedStreamVersionGeneral, type ExpectedStreamVersionWithValue, ExpectedVersionConflictError, type Flavour, type GlobalPositionTypeOfEventStore, type GlobalPositionTypeOfReadEventMetadata, type GlobalStreamCaughtUp, GlobalStreamCaughtUpType, type GlobalSubscriptionEvent, type HandleOptions, type InMemoryEventStore, InMemoryEventStoreDefaultStreamVersion, type InMemoryEventStoreOptions, type InMemoryReadEvent, InProcessLock, JSONParser, JsonDecoder, type Lock, type LockOptions, type Mapper, type MapperArgs, type MessageBus, type MessageHandler, type MessageProcessor, type MessageScheduler, type MockedFunction, type Mutable, NO_CONCURRENCY_CHECK, NoRetries, type NonNullable, ObjectDecoder, ParseError, type ParseOptions, type ProjectionDefinition, type ProjectionHandler, type ProjectionHandlingType, type ProjectionRegistration, type ReadEvent, type ReadEventMetadata, type ReadEventMetadataWithGlobalPosition, type ReadEventMetadataWithoutGlobalPosition, type ReadStreamOptions, type ReadStreamResult, type ReleaseLockOptions, STREAM_DOES_NOT_EXIST, STREAM_EXISTS, type ScheduleOptions, type ScheduledMessage, type ScheduledMessageProcessor, type StreamPositionTypeOfEventStore, type StreamPositionTypeOfReadEventMetadata, StreamingCoordinator, StringDecoder, type StringifyOptions, type Task, type TaskContext, TaskProcessor, type TaskProcessorOptions, type TaskQueue, type TaskQueueItem, type TestEventStream, type ThenThrows, type TypedProjectionDefinition, ValidationErrors, type Workflow, type WorkflowCommand, type WorkflowEvent, type WorkflowOutput, WrapEventStore, accept, argMatches, argValue, arrayUtils, assertDeepEqual, assertDoesNotThrow, assertEqual, assertExpectedVersionMatchesCurrent, assertFails, assertFalse, assertIsNotNull, assertIsNull, assertMatches, assertNotDeepEqual, assertNotEmptyString, assertNotEqual, assertOk, assertPositiveNumber, assertRejects, assertThat, assertThatArray, assertThrows, assertThrowsAsync, assertTrue, assertUnsignedBigInt, asyncProjections, asyncRetry, canCreateEventStoreSession, caughtUpEventFrom, collect, command, complete, concatUint8Arrays, deepEquals, error, event, filterProjections, formatDateToUtcYYYYMMDD, forwardToMessageBus, getInMemoryEventStore, getInMemoryMessageBus, globalStreamCaughtUp, ignore, inlineProjections, isEquatable, isExpectedVersionConflictError, isGlobalStreamCaughtUp, isNotInternalEvent, isNumber, isString, isSubscriptionEvent, isSubset, isValidYYYYMMDD, matchesExpectedVersion, merge, nulloSessionFactory, parseDateFromUtcYYYYMMDD, projection, projections, publish, reply, restream, schedule, send, streamGenerators, streamTrackingGlobalPosition, streamTransformations, sum, tryPublishMessagesAfterCommit, verifyThat };
739
+ export { type AcquireLockOptions, type AfterEventStoreCommitHandler, type AggregateStreamOptions, type AggregateStreamResult, type AggregateStreamResultOfEventStore, type AggregateStreamResultWithGlobalPosition, type AnyReadEventMetadata, type AppendStreamResultOfEventStore, type AppendToStreamOptions, type AppendToStreamResult, type AppendToStreamResultWithGlobalPosition, type ArgumentMatcher, AssertionError, type AsyncDeciderSpecification, type AsyncRetryOptions, type BigIntGlobalPosition, type BigIntStreamPosition, BinaryJsonDecoder, type Brand, type CanHandle, CaughtUpTransformStream, type Closeable, type CombinedReadEventMetadata, type Command, type CommandBus, type CommandDataOf, CommandHandler, type CommandHandlerOptions, type CommandHandlerResult, type CommandHandlerRetryOptions, CommandHandlerStreamVersionConflictRetryOptions, type CommandMetaDataOf, type CommandProcessor, type CommandSender, type CommandTypeOf, type CommonReadEventMetadata, CompositeDecoder, ConcurrencyError, type CreateCommandType, type CreateEventType, type Database, type Decider, DeciderCommandHandler, type DeciderCommandHandlerOptions, DeciderSpecification, type Decoder, type DeepReadonly, type DefaultCommandMetadata, DefaultDecoder, type DefaultEventStoreOptions, type DefaultRecord, type DocumentsCollection, type EnqueueTaskOptions, type Equatable, ErrorConstructor, type Event, type EventBus, type EventDataOf, type EventMetaDataOf, type EventProcessor, type EventStore, type EventStoreReadEventMetadata, type EventStoreSession, type EventStoreSessionFactory, type EventStoreWrapper, type EventTypeOf, type EventsPublisher, type ExpectedStreamVersion, type ExpectedStreamVersionGeneral, type ExpectedStreamVersionWithValue, ExpectedVersionConflictError, type Flavour, type GlobalPositionTypeOfEventStore, type GlobalPositionTypeOfReadEventMetadata, type GlobalStreamCaughtUp, GlobalStreamCaughtUpType, type GlobalSubscriptionEvent, type HandleOptions, type InMemoryEventStore, InMemoryEventStoreDefaultStreamVersion, type InMemoryEventStoreOptions, type InMemoryProjectionHandlerContext, type InMemoryReadEvent, type InMemoryReadEventMetadata, InProcessLock, JSONParser, JsonDecoder, type Lock, type LockOptions, type Mapper, type MapperArgs, type MessageBus, type MessageHandler, type MessageProcessor, type MessageScheduler, type MockedFunction, type Mutable, NO_CONCURRENCY_CHECK, NoRetries, type NonNullable, ObjectDecoder, ParseError, type ParseOptions, type ProjectionDefinition, type ProjectionHandler, type ProjectionHandlingType, type ProjectionRegistration, type ReadEvent, type ReadEventMetadata, type ReadEventMetadataWithGlobalPosition, type ReadEventMetadataWithoutGlobalPosition, type ReadStreamOptions, type ReadStreamResult, type ReleaseLockOptions, STREAM_DOES_NOT_EXIST, STREAM_EXISTS, type ScheduleOptions, type ScheduledMessage, type ScheduledMessageProcessor, type StreamPositionTypeOfEventStore, type StreamPositionTypeOfReadEventMetadata, StreamingCoordinator, StringDecoder, type StringifyOptions, type Task, type TaskContext, TaskProcessor, type TaskProcessorOptions, type TaskQueue, type TaskQueueItem, type TestEventStream, type ThenThrows, type TypedProjectionDefinition, ValidationErrors, type WithGlobalPosition, type Workflow, type WorkflowCommand, type WorkflowEvent, type WorkflowOutput, WrapEventStore, accept, argMatches, argValue, arrayUtils, assertDeepEqual, assertDoesNotThrow, assertEqual, assertExpectedVersionMatchesCurrent, assertFails, assertFalse, assertIsNotNull, assertIsNull, assertMatches, assertNotDeepEqual, assertNotEmptyString, assertNotEqual, assertOk, assertPositiveNumber, assertRejects, assertThat, assertThatArray, assertThrows, assertThrowsAsync, assertTrue, assertUnsignedBigInt, asyncProjections, asyncRetry, canCreateEventStoreSession, caughtUpEventFrom, collect, command, complete, concatUint8Arrays, deepEquals, error, event, filterProjections, formatDateToUtcYYYYMMDD, forwardToMessageBus, getInMemoryDatabase, getInMemoryEventStore, getInMemoryMessageBus, globalStreamCaughtUp, ignore, inlineProjections, isEquatable, isExpectedVersionConflictError, isGlobalStreamCaughtUp, isNotInternalEvent, isNumber, isString, isSubscriptionEvent, isSubset, isValidYYYYMMDD, matchesExpectedVersion, merge, nulloSessionFactory, parseDateFromUtcYYYYMMDD, projection, projections, publish, reply, restream, schedule, send, streamGenerators, streamTrackingGlobalPosition, streamTransformations, sum, tryPublishMessagesAfterCommit, verifyThat };
package/dist/index.js CHANGED
@@ -276,6 +276,7 @@ var getInMemoryEventStore = (eventStoreOptions) => {
276
276
  const getAllEventsCount = () => {
277
277
  return Array.from(streams.values()).map((s) => s.length).reduce((p, c) => p + c, 0);
278
278
  };
279
+ const _inlineProjections = (eventStoreOptions?.projections ?? []).filter(({ type }) => type === "inline").map(({ projection: projection2 }) => projection2);
279
280
  return {
280
281
  async aggregateStream(streamName, options) {
281
282
  const { evolve, initialState, read } = options;
@@ -341,7 +342,10 @@ var getInMemoryEventStore = (eventStoreOptions) => {
341
342
  nextExpectedStreamVersion: positionOfLastEventInTheStream,
342
343
  createdNewStream: currentStreamVersion === InMemoryEventStoreDefaultStreamVersion
343
344
  };
344
- await tryPublishMessagesAfterCommit(newEvents, eventStoreOptions?.hooks);
345
+ await tryPublishMessagesAfterCommit(
346
+ newEvents,
347
+ eventStoreOptions?.hooks
348
+ );
345
349
  return result;
346
350
  }
347
351
  //streamEvents: streamingCoordinator.stream,
@@ -716,6 +720,55 @@ var DeciderCommandHandler = (options) => async (eventStore, id, command2, handle
716
720
  );
717
721
  };
718
722
 
723
+ // src/serialization/json/JSONParser.ts
724
+ var ParseError = class extends Error {
725
+ constructor(text) {
726
+ super(`Cannot parse! ${text}`);
727
+ }
728
+ };
729
+ var JSONParser = {
730
+ stringify: (value, options) => {
731
+ return JSON.stringify(
732
+ options?.map ? options.map(value) : value,
733
+ //TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
734
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
735
+ (_, v) => typeof v === "bigint" ? v.toString() : v
736
+ );
737
+ },
738
+ parse: (text, options) => {
739
+ const parsed = JSON.parse(text, options?.reviver);
740
+ if (options?.typeCheck && !options?.typeCheck(parsed))
741
+ throw new ParseError(text);
742
+ return options?.map ? options.map(parsed) : parsed;
743
+ }
744
+ };
745
+
746
+ // src/database/inMemoryDatabase.ts
747
+ var getInMemoryDatabase = () => {
748
+ const storage = /* @__PURE__ */ new Map();
749
+ return {
750
+ collection: (collectionName, _collectionOptions = {}) => {
751
+ const toFullId = (id) => `${collectionName}-${id}`;
752
+ const collection = {
753
+ store: (id, obj) => {
754
+ storage.set(toFullId(id), obj);
755
+ },
756
+ delete: (id) => {
757
+ storage.delete(toFullId(id));
758
+ },
759
+ get: (id) => {
760
+ const result = storage.get(toFullId(id));
761
+ return result ? (
762
+ // Clone to simulate getting new instance on loading
763
+ JSONParser.parse(JSONParser.stringify(result))
764
+ ) : null;
765
+ }
766
+ };
767
+ return collection;
768
+ }
769
+ };
770
+ };
771
+
719
772
  // src/messageBus/index.ts
720
773
  var getInMemoryMessageBus = () => {
721
774
  const allHandlers = /* @__PURE__ */ new Map();
@@ -769,29 +822,6 @@ var getInMemoryMessageBus = () => {
769
822
  };
770
823
  };
771
824
 
772
- // src/serialization/json/JSONParser.ts
773
- var ParseError = class extends Error {
774
- constructor(text) {
775
- super(`Cannot parse! ${text}`);
776
- }
777
- };
778
- var JSONParser = {
779
- stringify: (value, options) => {
780
- return JSON.stringify(
781
- options?.map ? options.map(value) : value,
782
- //TODO: Consider adding support to DateTime and adding specific format to mark that's a bigint
783
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
784
- (_, v) => typeof v === "bigint" ? v.toString() : v
785
- );
786
- },
787
- parse: (text, options) => {
788
- const parsed = JSON.parse(text, options?.reviver);
789
- if (options?.typeCheck && !options?.typeCheck(parsed))
790
- throw new ParseError(text);
791
- return options?.map ? options.map(parsed) : parsed;
792
- }
793
- };
794
-
795
825
  // src/projections/index.ts
796
826
  var filterProjections = (type, projections2) => {
797
827
  const inlineProjections2 = projections2.filter((projection2) => projection2.type === type).map(({ projection: projection2 }) => projection2);
@@ -1429,66 +1459,95 @@ var assertThatArray = (array) => {
1429
1459
 
1430
1460
  // src/testing/deciderSpecification.ts
1431
1461
  var DeciderSpecification = {
1432
- for: (decider) => {
1433
- {
1434
- return (givenEvents) => {
1435
- return {
1436
- when: (command2) => {
1437
- const handle = () => {
1438
- const existingEvents = Array.isArray(givenEvents) ? givenEvents : [givenEvents];
1439
- const currentState = existingEvents.reduce(
1440
- decider.evolve,
1441
- decider.initialState()
1442
- );
1443
- return decider.decide(command2, currentState);
1444
- };
1445
- return {
1446
- then: (expectedEvents) => {
1447
- const resultEvents = handle();
1448
- const resultEventsArray = Array.isArray(resultEvents) ? resultEvents : [resultEvents];
1449
- const expectedEventsArray = Array.isArray(expectedEvents) ? expectedEvents : [expectedEvents];
1450
- assertThatArray(resultEventsArray).containsOnlyElementsMatching(
1451
- expectedEventsArray
1452
- );
1453
- },
1454
- thenNothingHappened: () => {
1455
- const resultEvents = handle();
1456
- const resultEventsArray = Array.isArray(resultEvents) ? resultEvents : [resultEvents];
1457
- assertThatArray(resultEventsArray).isEmpty();
1458
- },
1459
- thenThrows: (...args) => {
1460
- try {
1461
- handle();
1462
- throw new AssertionError("Handler did not fail as expected");
1463
- } catch (error2) {
1464
- if (error2 instanceof AssertionError) throw error2;
1465
- if (args.length === 0) return;
1466
- if (!isErrorConstructor(args[0])) {
1467
- assertTrue(
1468
- args[0](error2),
1469
- `Error didn't match the error condition: ${error2?.toString()}`
1470
- );
1471
- return;
1472
- }
1473
- assertTrue(
1474
- error2 instanceof args[0],
1475
- `Caught error is not an instance of the expected type: ${error2?.toString()}`
1476
- );
1477
- if (args[1]) {
1478
- assertTrue(
1479
- args[1](error2),
1480
- `Error didn't match the error condition: ${error2?.toString()}`
1462
+ for: deciderSpecificationFor
1463
+ };
1464
+ function deciderSpecificationFor(decider) {
1465
+ {
1466
+ return (givenEvents) => {
1467
+ return {
1468
+ when: (command2) => {
1469
+ const handle = () => {
1470
+ const existingEvents = Array.isArray(givenEvents) ? givenEvents : [givenEvents];
1471
+ const currentState = existingEvents.reduce(
1472
+ decider.evolve,
1473
+ decider.initialState()
1474
+ );
1475
+ return decider.decide(command2, currentState);
1476
+ };
1477
+ return {
1478
+ then: (expectedEvents) => {
1479
+ const resultEvents = handle();
1480
+ if (resultEvents instanceof Promise) {
1481
+ return resultEvents.then((events) => {
1482
+ thenHandler(events, expectedEvents);
1483
+ });
1484
+ }
1485
+ thenHandler(resultEvents, expectedEvents);
1486
+ },
1487
+ thenNothingHappened: () => {
1488
+ const resultEvents = handle();
1489
+ if (resultEvents instanceof Promise) {
1490
+ return resultEvents.then((events) => {
1491
+ thenNothingHappensHandler(events);
1492
+ });
1493
+ }
1494
+ thenNothingHappensHandler(resultEvents);
1495
+ },
1496
+ thenThrows: (...args) => {
1497
+ try {
1498
+ const result = handle();
1499
+ if (result instanceof Promise) {
1500
+ return result.then(() => {
1501
+ throw new AssertionError(
1502
+ "Handler did not fail as expected"
1481
1503
  );
1482
- }
1504
+ }).catch((error2) => {
1505
+ thenThrowsErrorHandler(error2, args);
1506
+ });
1483
1507
  }
1508
+ throw new AssertionError("Handler did not fail as expected");
1509
+ } catch (error2) {
1510
+ thenThrowsErrorHandler(error2, args);
1484
1511
  }
1485
- };
1486
- }
1487
- };
1512
+ }
1513
+ };
1514
+ }
1488
1515
  };
1489
- }
1516
+ };
1490
1517
  }
1491
- };
1518
+ }
1519
+ function thenHandler(events, expectedEvents) {
1520
+ const resultEventsArray = Array.isArray(events) ? events : [events];
1521
+ const expectedEventsArray = Array.isArray(expectedEvents) ? expectedEvents : [expectedEvents];
1522
+ assertThatArray(resultEventsArray).containsOnlyElementsMatching(
1523
+ expectedEventsArray
1524
+ );
1525
+ }
1526
+ function thenNothingHappensHandler(events) {
1527
+ const resultEventsArray = Array.isArray(events) ? events : [events];
1528
+ assertThatArray(resultEventsArray).isEmpty();
1529
+ }
1530
+ function thenThrowsErrorHandler(error2, args) {
1531
+ if (error2 instanceof AssertionError) throw error2;
1532
+ if (args.length === 0) return;
1533
+ if (!isErrorConstructor(args[0])) {
1534
+ assertTrue(
1535
+ args[0](error2),
1536
+ `Error didn't match the error condition: ${error2?.toString()}`
1537
+ );
1538
+ return;
1539
+ }
1540
+ assertTrue(
1541
+ error2 instanceof args[0],
1542
+ `Caught error is not an instance of the expected type: ${error2?.toString()}`
1543
+ );
1544
+ if (args[1]) {
1545
+ assertTrue(
1546
+ args[1](error2),
1547
+ `Error didn't match the error condition: ${error2?.toString()}`
1548
+ );
1549
+ }
1550
+ }
1492
1551
 
1493
1552
  // src/testing/wrapEventStore.ts
1494
1553
  var WrapEventStore = (eventStore) => {
@@ -1601,6 +1660,7 @@ export {
1601
1660
  filterProjections,
1602
1661
  formatDateToUtcYYYYMMDD,
1603
1662
  forwardToMessageBus,
1663
+ getInMemoryDatabase,
1604
1664
  getInMemoryEventStore,
1605
1665
  getInMemoryMessageBus,
1606
1666
  globalStreamCaughtUp,