Skip to content

Commit

Permalink
Merge pull request #700 from fraktalio/feature/unsuitable_intersection
Browse files Browse the repository at this point in the history
Unsuitable intersection of `different` types
  • Loading branch information
idugalic authored Mar 14, 2024
2 parents ad51604 + 9775d0c commit 5983b4f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 38 deletions.
7 changes: 4 additions & 3 deletions src/lib/application/eventsourcing-aggregate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,18 @@ class EventRepositoryImpl
}

async save(
eList: readonly (Evt & CmdMetadata)[],
eList: readonly Evt[],
commandMetadata: CmdMetadata,
versionProvider: (e: Evt) => Promise<Version | null>
): Promise<readonly (Evt & Version & EvtMetadata)[]> {
//mapping the Commands metadata into Events metadata !!!
const savedEvents: readonly (Evt & Version & EvtMetadata)[] =
await Promise.all(
eList.map(async (e: Evt & CmdMetadata, index) => ({
eList.map(async (e: Evt, index) => ({
kind: e.kind,
value: e.value,
version: ((await versionProvider(e))?.version ?? 0) + index + 1,
traceId: e.traceId,
traceId: commandMetadata.traceId,
}))
);
storage.concat(savedEvents);
Expand Down
38 changes: 18 additions & 20 deletions src/lib/application/eventsourcing-aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ export interface IEventRepository<C, E, V, CM, EM> {
/**
* Save events
*
* @param events - list of Events with Command Metadata
* @param events - list of Events
* @param commandMetadata - Command Metadata of the command that initiated `events`
* @param versionProvider - A provider for the Latest Event in this stream and its Version/Sequence
* @return a list of newly saved Event(s) of type `E` with Version of type `V` and with Event Metadata of type `EM`
*/
readonly save: (
events: readonly (E & CM)[],
events: readonly E[],
commandMetadata: CM,
versionProvider: (e: E) => Promise<V | null>
) => Promise<readonly (E & V & EM)[]>;
}
Expand Down Expand Up @@ -231,20 +233,19 @@ export class EventSourcingAggregate<C, S, E, V, CM, EM>
}

async save(
events: readonly (E & CM)[],
events: readonly E[],
commandMetadata: CM,
versionProvider: (e: E) => Promise<V | null>
): Promise<readonly (E & V & EM)[]> {
return this.eventRepository.save(events, versionProvider);
return this.eventRepository.save(events, commandMetadata, versionProvider);
}

async handle(command: C & CM): Promise<readonly (E & V & EM)[]> {
const currentEvents = await this.eventRepository.fetch(command);

return this.eventRepository.save(
this.computeNewEvents(currentEvents, command).map((evt) => ({
...evt,
...command,
})),
this.computeNewEvents(currentEvents, command),
command,
async () => currentEvents[currentEvents.length - 1]
);
}
Expand Down Expand Up @@ -288,25 +289,22 @@ export class EventSourcingOrchestratingAggregate<C, S, E, V, CM, EM>
}

async save(
events: readonly (E & CM)[],
events: readonly E[],
commandMetadata: CM,
versionProvider: (e: E) => Promise<V | null>
): Promise<readonly (E & V & EM)[]> {
return this.eventRepository.save(events, versionProvider);
return this.eventRepository.save(events, commandMetadata, versionProvider);
}

async handle(command: C & CM): Promise<readonly (E & V & EM)[]> {
const currentEvents = await this.eventRepository.fetch(command);
return this.eventRepository.save(
(
await this.computeNewEvents(
currentEvents,
command,
async (cmd: C & CM) => await this.eventRepository.fetch(cmd)
)
).map((event) => ({
...event,
...command,
})),
await this.computeNewEvents(
currentEvents,
command,
async (cmd: C & CM) => await this.eventRepository.fetch(cmd)
),
command,
this.versionProvider.bind(this)
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/application/materialized-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ class ViewStateRepositoryImpl
return storage;
}
async save(
s: ViewState & EventMetadata,
s: ViewState,
_: EventMetadata,
v: Version | null
): Promise<ViewState & Version> {
storage = {
Expand Down
14 changes: 10 additions & 4 deletions src/lib/application/materialized-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ export interface IViewStateRepository<E, S, V, EM> {
* Save state
*
* @param state - State and Event Metadata of type `S & EM`
* @param eventMetadata - Event Metadata of type `EM`
* @param version - State version of type `V | null`
* @return newly saved State and Version of type `S` & `V`
*/
readonly save: (state: S & EM, version: V | null) => Promise<S & V>;
readonly save: (
state: S,
eventMetadata: EM,
version: V | null
) => Promise<S & V>;
}

/**
Expand Down Expand Up @@ -97,8 +102,8 @@ export class MaterializedView<S, E, V, EM>
return this.viewStateRepository.fetch(event);
}

async save(state: S & EM, version: V | null): Promise<S & V> {
return this.viewStateRepository.save(state, version);
async save(state: S, eventMetadata: EM, version: V | null): Promise<S & V> {
return this.viewStateRepository.save(state, eventMetadata, version);
}

async handle(event: E & EM): Promise<S & V> {
Expand All @@ -108,7 +113,8 @@ export class MaterializedView<S, E, V, EM>
event
);
return this.viewStateRepository.save(
{ ...(newState as S), ...(event as EM) },
newState,
event as EM,
currentStateAndVersion as V
);
}
Expand Down
9 changes: 6 additions & 3 deletions src/lib/application/statestored-aggregate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class StateRepositoryImpl
async fetch(_c: Cmd): Promise<(State & Version) | null> {
return stateStorage;
}
async save(s: State & Cmd): Promise<State & Version> {
async save(s: State, _: Cmd): Promise<State & Version> {
stateStorage = {
evenNumber: s.evenNumber,
oddNumber: s.oddNumber,
Expand All @@ -269,12 +269,15 @@ class StateAndMetadataRepositoryImpl
async fetch(_c: Cmd): Promise<(State & Version & StateMetadata) | null> {
return stateAndMetadataStorage;
}
async save(s: State & CmdMetadata): Promise<State & Version & StateMetadata> {
async save(
s: State,
cm: CmdMetadata
): Promise<State & Version & StateMetadata> {
stateAndMetadataStorage = {
evenNumber: s.evenNumber,
oddNumber: s.oddNumber,
version: stateAndMetadataStorage.version + 1,
traceId: s.traceId,
traceId: cm.traceId,
};
return stateAndMetadataStorage;
}
Expand Down
29 changes: 22 additions & 7 deletions src/lib/application/statestored-aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ export interface IStateRepository<C, S, V, CM, SM> {
* You can update/save the item/state, but only if the `version` number in the storage has not changed.
*
* @param state - State with Command Metadata of type `S & CM`
* @param commandMetadata - Command Metadata of the command that initiated the `state`
* @param version - The current version of the state
* @return newly saved State of type `S & V & SM`
*/
readonly save: (state: S & CM, version: V | null) => Promise<S & V & SM>;
readonly save: (
state: S,
commandMetadata: CM,
version: V | null
) => Promise<S & V & SM>;
}

/**
Expand Down Expand Up @@ -175,8 +180,12 @@ export class StateStoredAggregate<C, S, E, V, CM, SM>
return this.stateRepository.fetch(command);
}

async save(state: S & CM, version: V | null): Promise<S & V & SM> {
return this.stateRepository.save(state, version);
async save(
state: S,
commandMetadata: CM,
version: V | null
): Promise<S & V & SM> {
return this.stateRepository.save(state, commandMetadata, version);
}

async handle(command: C & CM): Promise<S & V & SM> {
Expand All @@ -186,7 +195,8 @@ export class StateStoredAggregate<C, S, E, V, CM, SM>
command
);
return this.stateRepository.save(
{ ...newState, ...(command as CM) },
newState,
command as CM,
currentState as V
);
}
Expand Down Expand Up @@ -224,8 +234,12 @@ export class StateStoredOrchestratingAggregate<C, S, E, V, CM, SM>
return this.stateRepository.fetch(command);
}

async save(state: S & CM, version: V | null): Promise<S & V & SM> {
return this.stateRepository.save(state, version);
async save(
state: S,
commandMetadata: CM,
version: V | null
): Promise<S & V & SM> {
return this.stateRepository.save(state, commandMetadata, version);
}

async handle(command: C & CM): Promise<S & V & SM> {
Expand All @@ -235,7 +249,8 @@ export class StateStoredOrchestratingAggregate<C, S, E, V, CM, SM>
command
);
return this.stateRepository.save(
{ ...newState, ...(command as CM) },
newState,
command as CM,
currentState as V
);
}
Expand Down

0 comments on commit 5983b4f

Please sign in to comment.