Skip to content

Commit

Permalink
Merge pull request #699 from fraktalio/feature/infrastructure_and_met…
Browse files Browse the repository at this point in the history
…adata

Infrastructure interfaces extended to include `metadata`
  • Loading branch information
idugalic authored Mar 14, 2024
2 parents 536814f + 606d1c8 commit ca8df0a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/lib/application/eventsourcing-aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export interface IEventRepository<C, E, V, CM, EM> {
/**
* Fetch events
*
* @param command - Command of type `C`
* @param command - Command of type `C` with metadata of type `CM`
*
* @return list of Events with Version and Event Metadata
*/
readonly fetch: (command: C) => Promise<readonly (E & V & EM)[]>;
readonly fetch: (command: C & CM) => Promise<readonly (E & V & EM)[]>;

/**
* Get the latest event stream version / sequence
Expand Down Expand Up @@ -138,7 +138,7 @@ export abstract class EventComputation<C, S, E> implements IDecider<C, S, E> {
* An abstract algorithm to compute new events based on the old events and the command being handled.
* It returns all the events, including the events created by handling commands which are triggered by Saga - orchestration included.
*/
export abstract class EventOrchestratingComputation<C, S, E>
export abstract class EventOrchestratingComputation<C, S, E, CM>
implements IDecider<C, S, E>, ISaga<E, C>
{
protected constructor(
Expand Down Expand Up @@ -175,8 +175,8 @@ export abstract class EventOrchestratingComputation<C, S, E>

protected async computeNewEvents(
events: readonly E[],
command: C,
fetch: (c: C) => Promise<readonly E[]>
command: C & CM,
fetch: (c: C & CM) => Promise<readonly E[]>
): Promise<readonly E[]> {
// eslint-disable-next-line functional/no-let
let resultingEvents = this.computeNewEventsInternally(events, command);
Expand Down Expand Up @@ -222,7 +222,7 @@ export class EventSourcingAggregate<C, S, E, V, CM, EM>
super(decider);
}

async fetch(command: C): Promise<readonly (E & V & EM)[]> {
async fetch(command: C & CM): Promise<readonly (E & V & EM)[]> {
return this.eventRepository.fetch(command);
}

Expand Down Expand Up @@ -268,7 +268,7 @@ export class EventSourcingAggregate<C, S, E, V, CM, EM>
* @author Иван Дугалић / Ivan Dugalic / @idugalic
*/
export class EventSourcingOrchestratingAggregate<C, S, E, V, CM, EM>
extends EventOrchestratingComputation<C, S, E>
extends EventOrchestratingComputation<C, S, E, CM>
implements IEventSourcingOrchestratingAggregate<C, S, E, V, CM, EM>
{
constructor(
Expand All @@ -279,7 +279,7 @@ export class EventSourcingOrchestratingAggregate<C, S, E, V, CM, EM>
super(decider, saga);
}

async fetch(command: C): Promise<readonly (E & V & EM)[]> {
async fetch(command: C & CM): Promise<readonly (E & V & EM)[]> {
return this.eventRepository.fetch(command);
}

Expand All @@ -301,7 +301,7 @@ export class EventSourcingOrchestratingAggregate<C, S, E, V, CM, EM>
await this.computeNewEvents(
currentEvents,
command,
async (cmd: C) => await this.eventRepository.fetch(cmd)
async (cmd: C & CM) => await this.eventRepository.fetch(cmd)
)
).map((event) => ({
...event,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/application/materialized-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface IViewStateRepository<E, S, V, EM> {
*
* @return current state / `S` with version / `V`, or NULL
*/
readonly fetch: (event: E) => Promise<(S & V) | null>;
readonly fetch: (event: E & EM) => Promise<(S & V) | null>;
/**
* Save state
*
Expand Down Expand Up @@ -93,7 +93,7 @@ export class MaterializedView<S, E, V, EM>
return this.view.evolve(state, event);
}

async fetch(event: E): Promise<(S & V) | null> {
async fetch(event: E & EM): Promise<(S & V) | null> {
return this.viewStateRepository.fetch(event);
}

Expand Down
12 changes: 6 additions & 6 deletions src/lib/application/statestored-aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export interface IStateRepository<C, S, V, CM, SM> {
/**
* Fetch state, version and metadata
*
* @param command - Command payload
* @param command - Command payload of type C with metadata of type `CM`
* @return current State/[S], Version/[V] and State Metadata/[SM]
*/
readonly fetch: (command: C) => Promise<(S & V & SM) | null>;
readonly fetch: (command: C & CM) => Promise<(S & V & SM) | null>;

/**
* Save state (with optimistic locking)
Expand Down Expand Up @@ -171,7 +171,7 @@ export class StateStoredAggregate<C, S, E, V, CM, SM>
) {
super(decider);
}
async fetch(command: C): Promise<(S & V & SM) | null> {
async fetch(command: C & CM): Promise<(S & V & SM) | null> {
return this.stateRepository.fetch(command);
}

Expand All @@ -180,7 +180,7 @@ export class StateStoredAggregate<C, S, E, V, CM, SM>
}

async handle(command: C & CM): Promise<S & V & SM> {
const currentState = await this.stateRepository.fetch(command as C);
const currentState = await this.stateRepository.fetch(command);
const newState = this.computeNewState(
currentState ? currentState : this.decider.initialState,
command
Expand Down Expand Up @@ -220,7 +220,7 @@ export class StateStoredOrchestratingAggregate<C, S, E, V, CM, SM>
super(decider, saga);
}

async fetch(command: C): Promise<(S & V & SM) | null> {
async fetch(command: C & CM): Promise<(S & V & SM) | null> {
return this.stateRepository.fetch(command);
}

Expand All @@ -229,7 +229,7 @@ export class StateStoredOrchestratingAggregate<C, S, E, V, CM, SM>
}

async handle(command: C & CM): Promise<S & V & SM> {
const currentState = await this.stateRepository.fetch(command as C);
const currentState = await this.stateRepository.fetch(command);
const newState = this.computeNewState(
currentState ? currentState : this.decider.initialState,
command
Expand Down

0 comments on commit ca8df0a

Please sign in to comment.