UNPKG

@fraktalio/fmodel-ts

Version:

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

45 lines 3.43 kB
/* * Copyright 2023 Fraktalio D.O.O. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an " * AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific * language governing permissions and limitations under the License. */ /** * 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 class MaterializedView { constructor(view, viewStateRepository) { this.view = view; this.viewStateRepository = viewStateRepository; this.initialState = this.view.initialState; } evolve(state, event) { return this.view.evolve(state, event); } async fetch(event) { return this.viewStateRepository.fetch(event); } async save(state, eventMetadata, version) { return this.viewStateRepository.save(state, eventMetadata, version); } async handle(event) { const currentStateAndVersion = await this.viewStateRepository.fetch(event); const newState = this.view.evolve(currentStateAndVersion ? currentStateAndVersion : this.view.initialState, event); return this.viewStateRepository.save(newState, event, currentStateAndVersion); } } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWF0ZXJpYWxpemVkLXZpZXcuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvbGliL2FwcGxpY2F0aW9uL21hdGVyaWFsaXplZC12aWV3LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7Ozs7OztHQVdHO0FBK0RIOzs7Ozs7Ozs7O0dBVUc7QUFDSCxNQUFNLE9BQU8sZ0JBQWdCO0lBR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