UNPKG

@fraktalio/fmodel-ts

Version:

Functional domain modeling with TypeScript. Optimized for event sourcing and CQRS

72 lines (71 loc) 2.91 kB
import { IView } from '../domain/view'; /** * View State repository interface * * @typeParam E - Event * @typeParam S - State * @typeParam V - Version of the state * @typeParam EM - Event Metadata * * @author Иван Дугалић / Ivan Dugalic / @idugalic */ export interface IViewStateRepository<E, S, V, EM> { /** * Fetch state * * @param event - Event of type `E` with metadata of type `EM` * * @return current state / `S` with version / `V`, or NULL */ readonly fetch: (event: E & EM) => Promise<(S & V) | null>; /** * 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, eventMetadata: EM, version: V | null) => Promise<S & V>; } /** * Materialized view interface is using/delegating a `IView` to handle events of type `E` and to maintain a state of projection(s) via `IViewStateRepository` as a result. * Essentially, it represents the query/view side of the CQRS pattern. * * @typeParam S - Materialized View state of type `S` * @typeParam E - Events of type `E` that are handled by this Materialized View * @typeParam V - Version of the state * @typeParam EM - Event Metadata * * @author Иван Дугалић / Ivan Dugalic / @idugalic */ export interface IMaterializedView<S, E, V, EM> extends IView<S, E>, IViewStateRepository<E, S, V, EM> { /** * Handles the event of type `E`, and returns new persisted state of type `S` * * @param event Event of type `E & EM` to be handled / event with event metadata * @return State of type `S` & `V` */ readonly handle: (event: E & EM) => Promise<S & V>; } /** * Materialized view is using/delegating a `View` to handle events of type `E` and to maintain a state of projection(s) via `IViewStateRepository` as a result. * Essentially, it represents the query/view side of the CQRS pattern. * * @typeParam S - Materialized View state of type `S` * @typeParam E - Events of type `E` that are handled by this Materialized View * @typeParam V - Version of the state * @typeParam EM - Event Metadata * * @author Иван Дугалић / Ivan Dugalic / @idugalic */ export declare class MaterializedView<S, E, V, EM> implements IMaterializedView<S, E, V, EM> { protected readonly view: IView<S, E>; protected readonly viewStateRepository: IViewStateRepository<E, S, V, EM>; constructor(view: IView<S, E>, viewStateRepository: IViewStateRepository<E, S, V, EM>); readonly initialState: S; evolve(state: S, event: E): S; fetch(event: E & EM): Promise<(S & V) | null>; save(state: S, eventMetadata: EM, version: V | null): Promise<S & V>; handle(event: E & EM): Promise<S & V>; }