Skip to content

Commit

Permalink
Fixed the event store read event metadata inference
Browse files Browse the repository at this point in the history
Now, it'll be possible to use tryPublishMessagesAfterCommit method
without type errors
  • Loading branch information
oskardudycz committed Jan 25, 2025
1 parent 52a3ba7 commit 1dae4a8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import {
ExpectedVersionConflictError,
assertExpectedVersionMatchesCurrent,
ExpectedVersionConflictError,
filterProjections,
tryPublishMessagesAfterCommit,
type AggregateStreamOptions,
type AggregateStreamResult,
type AppendToStreamOptions,
type AppendToStreamResult,
type Closeable,
type DefaultEventStoreOptions,
type Event,
type EventStore,
type ProjectionRegistration,
type ReadEvent,
type ReadEventMetadataWithoutGlobalPosition,
type ReadStreamOptions,
type ReadStreamResult,
type DefaultEventStoreOptions,
} from '@event-driven-io/emmett';
import {
MongoClient,
Expand Down Expand Up @@ -394,7 +394,6 @@ class MongoDBEventStoreImplementation implements MongoDBEventStore, Closeable {
}

await tryPublishMessagesAfterCommit<MongoDBEventStore>(
// @ts-expect-error Issues with `globalPosition` not being present causing the type for metadata to expect `never`
eventsToAppend,
this.options.hooks,
// {
Expand Down
10 changes: 7 additions & 3 deletions src/packages/emmett/src/eventStore/eventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import type {
AnyReadEventMetadata,
BigIntGlobalPosition,
BigIntStreamPosition,
CommonReadEventMetadata,
Event,
GlobalPositionTypeOfReadEventMetadata,
ReadEvent,
ReadEventMetadata,
StreamPositionTypeOfReadEventMetadata,
WithGlobalPosition,
} from '../typing';
import type { AfterEventStoreCommitHandler } from './afterCommit';
//import type { GlobalSubscriptionEvent } from './events';
Expand Down Expand Up @@ -52,9 +54,11 @@ export interface EventStore<
}

export type EventStoreReadEventMetadata<Store extends EventStore> =
Store extends EventStore<infer ReadEventMetadataType>
? ReadEventMetadataType extends ReadEventMetadata<infer GV, infer SV>
? ReadEventMetadata<GV, SV> & ReadEventMetadataType
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;

Expand Down
5 changes: 4 additions & 1 deletion src/packages/emmett/src/eventStore/inMemoryEventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ export const getInMemoryEventStore = (
currentStreamVersion === InMemoryEventStoreDefaultStreamVersion,
};

await tryPublishMessagesAfterCommit(newEvents, eventStoreOptions?.hooks);
await tryPublishMessagesAfterCommit<InMemoryEventStore>(
newEvents,
eventStoreOptions?.hooks,
);

return result;
},
Expand Down
22 changes: 14 additions & 8 deletions src/packages/emmett/src/typing/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,23 @@ export type ReadEvent<
metadata: CombinedReadEventMetadata<EventType, EventMetaDataType>;
};

export type CommonReadEventMetadata<StreamPosition = BigIntStreamPosition> =
Readonly<{
eventId: string;
streamPosition: StreamPosition;
streamName: string;
}>;

export type WithGlobalPosition<GlobalPosition> = Readonly<{
globalPosition: GlobalPosition;
}>;

export type ReadEventMetadata<
GlobalPosition = undefined,
StreamPosition = BigIntStreamPosition,
> = Readonly<{
eventId: string;
streamPosition: StreamPosition;
streamName: string;
}> &
(GlobalPosition extends undefined
? object
: { globalPosition: GlobalPosition });
> = CommonReadEventMetadata<StreamPosition> &
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
(GlobalPosition extends undefined ? {} : WithGlobalPosition<GlobalPosition>);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyReadEventMetadata = ReadEventMetadata<any, any>;
Expand Down

0 comments on commit 1dae4a8

Please sign in to comment.