diff --git a/package-lock.json b/package-lock.json index 5c6a92c..8e41f63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fraktalio/fmodel-ts", - "version": "2.0.2", + "version": "2.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fraktalio/fmodel-ts", - "version": "2.0.2", + "version": "2.1.0", "license": "Apache License, Version 2.0", "devDependencies": { "@ava/typescript": "^1.1.1", diff --git a/package.json b/package.json index c531750..8d7d330 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fraktalio/fmodel-ts", - "version": "2.0.2", + "version": "2.1.0", "description": "Functional domain modeling with TypeScript. Optimized for event sourcing and CQRS", "main": "build/main/index.js", "typings": "build/main/index.d.ts", diff --git a/src/lib/application/eventsourcing-aggregate.spec.ts b/src/lib/application/eventsourcing-aggregate.spec.ts index 4dd45a5..c5e3a58 100644 --- a/src/lib/application/eventsourcing-aggregate.spec.ts +++ b/src/lib/application/eventsourcing-aggregate.spec.ts @@ -241,7 +241,7 @@ class EventRepositoryImpl async save( eList: readonly Evt[], commandMetadata: CmdMetadata, - versionProvider: (e: Evt & EvtMetadata) => Promise + versionProvider: (e: Evt) => Promise ): Promise { //mapping the Commands metadata into Events metadata !!! const savedEvents: readonly (Evt & Version & EvtMetadata)[] = @@ -249,12 +249,7 @@ class EventRepositoryImpl eList.map(async (e: Evt, index) => ({ kind: e.kind, value: e.value, - version: - (( - await versionProvider({ ...e, traceId: commandMetadata.traceId }) - )?.version ?? 0) + - index + - 1, + version: ((await versionProvider(e))?.version ?? 0) + index + 1, traceId: commandMetadata.traceId, })) ); @@ -262,7 +257,7 @@ class EventRepositoryImpl return savedEvents; } - async versionProvider(_e: Evt & EvtMetadata): Promise { + async versionProvider(_e: Evt): Promise { return storage[storage.length - 1]; } } diff --git a/src/lib/application/eventsourcing-aggregate.ts b/src/lib/application/eventsourcing-aggregate.ts index 96fc44f..c04692e 100644 --- a/src/lib/application/eventsourcing-aggregate.ts +++ b/src/lib/application/eventsourcing-aggregate.ts @@ -31,33 +31,33 @@ export interface IEventRepository { /** * Fetch events * - * @param command - Command of type `C` with metadata of type `CM` + * @param command - Command of type `C` * * @return list of Events with Version and Event Metadata */ - readonly fetch: (command: C & CM) => Promise; + readonly fetch: (command: C) => Promise; /** - * Get the latest event stream version / sequence + * Get the event stream version / sequence * - * @param event - Event of type `E & EM` + * @param event - Event of type `E` * - * @return the latest version / sequence of the event stream that this event belongs to. + * @return the version / sequence of the event stream that this event belongs to. */ - readonly versionProvider: (event: E & EM) => Promise; + readonly versionProvider: (event: E) => Promise; /** * Save events * * @param events - list of Events * @param commandMetadata - Command Metadata of the command that initiated `events` - * @param versionProvider - A provider for the stream Version/Sequence + * @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[], commandMetadata: CM, - versionProvider: (e: E & EM) => Promise + versionProvider: (e: E) => Promise ) => Promise; } @@ -140,7 +140,7 @@ export abstract class EventComputation implements IDecider { * 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 +export abstract class EventOrchestratingComputation implements IDecider, ISaga { protected constructor( @@ -177,14 +177,14 @@ export abstract class EventOrchestratingComputation protected async computeNewEvents( events: readonly E[], - command: C & CM, - fetch: (c: C & CM) => Promise + command: C, + fetch: (c: C) => Promise ): Promise { // eslint-disable-next-line functional/no-let let resultingEvents = this.computeNewEventsInternally(events, command); await asyncForEach( resultingEvents.flatMap((evt) => this.saga.react(evt)), - async (cmd) => { + async (cmd: C) => { const newEvents = this.computeNewEvents( (await fetch(cmd)).map((evt) => evt as E).concat(resultingEvents), cmd, @@ -224,18 +224,18 @@ export class EventSourcingAggregate super(decider); } - async fetch(command: C & CM): Promise { + async fetch(command: C): Promise { return this.eventRepository.fetch(command); } - async versionProvider(event: E & EM): Promise { + async versionProvider(event: E): Promise { return this.eventRepository.versionProvider(event); } async save( events: readonly E[], commandMetadata: CM, - versionProvider: (e: E & EM) => Promise + versionProvider: (e: E) => Promise ): Promise { return this.eventRepository.save(events, commandMetadata, versionProvider); } @@ -269,7 +269,7 @@ export class EventSourcingAggregate * @author Иван Дугалић / Ivan Dugalic / @idugalic */ export class EventSourcingOrchestratingAggregate - extends EventOrchestratingComputation + extends EventOrchestratingComputation implements IEventSourcingOrchestratingAggregate { constructor( @@ -280,18 +280,18 @@ export class EventSourcingOrchestratingAggregate super(decider, saga); } - async fetch(command: C & CM): Promise { + async fetch(command: C): Promise { return this.eventRepository.fetch(command); } - async versionProvider(event: E & EM): Promise { + async versionProvider(event: E): Promise { return this.eventRepository.versionProvider(event); } async save( events: readonly E[], commandMetadata: CM, - versionProvider: (e: E & EM) => Promise + versionProvider: (e: E) => Promise ): Promise { return this.eventRepository.save(events, commandMetadata, versionProvider); } @@ -302,7 +302,7 @@ export class EventSourcingOrchestratingAggregate await this.computeNewEvents( currentEvents, command, - async (cmd: C & CM) => await this.eventRepository.fetch(cmd) + async (cmd: C) => await this.eventRepository.fetch(cmd) ), command, this.versionProvider.bind(this) diff --git a/src/lib/application/materialized-view.ts b/src/lib/application/materialized-view.ts index 5a48ed4..2988fb0 100644 --- a/src/lib/application/materialized-view.ts +++ b/src/lib/application/materialized-view.ts @@ -29,11 +29,11 @@ export interface IViewStateRepository { /** * Fetch state * - * @param event - Event of type `E` with metadata of type `EM` + * @param event - Event of type `E` * * @return current state / `S` with version / `V`, or NULL */ - readonly fetch: (event: E & EM) => Promise<(S & V) | null>; + readonly fetch: (event: E) => Promise<(S & V) | null>; /** * Save state * @@ -98,7 +98,7 @@ export class MaterializedView return this.view.evolve(state, event); } - async fetch(event: E & EM): Promise<(S & V) | null> { + async fetch(event: E): Promise<(S & V) | null> { return this.viewStateRepository.fetch(event); } diff --git a/src/lib/application/statestored-aggregate.ts b/src/lib/application/statestored-aggregate.ts index 4c7ff12..735dff8 100644 --- a/src/lib/application/statestored-aggregate.ts +++ b/src/lib/application/statestored-aggregate.ts @@ -31,10 +31,10 @@ export interface IStateRepository { /** * Fetch state, version and metadata * - * @param command - Command payload of type C with metadata of type `CM` + * @param command - Command payload of type C * @return current State/[S], Version/[V] and State Metadata/[SM] */ - readonly fetch: (command: C & CM) => Promise<(S & V & SM) | null>; + readonly fetch: (command: C) => Promise<(S & V & SM) | null>; /** * Save state (with optimistic locking) @@ -176,7 +176,7 @@ export class StateStoredAggregate ) { super(decider); } - async fetch(command: C & CM): Promise<(S & V & SM) | null> { + async fetch(command: C): Promise<(S & V & SM) | null> { return this.stateRepository.fetch(command); } @@ -230,7 +230,7 @@ export class StateStoredOrchestratingAggregate super(decider, saga); } - async fetch(command: C & CM): Promise<(S & V & SM) | null> { + async fetch(command: C): Promise<(S & V & SM) | null> { return this.stateRepository.fetch(command); }