UNPKG

@fraktalio/fmodel-ts

Version:

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

49 lines 3.6 kB
"use strict"; /* * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MaterializedView = void 0; /** * 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 */ 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); } } exports.MaterializedView = MaterializedView; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWF0ZXJpYWxpemVkLXZpZXcuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9zcmMvbGliL2FwcGxpY2F0aW9uL21hdGVyaWFsaXplZC12aWV3LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTs7Ozs7Ozs7Ozs7R0FXRz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