From 7ea904f9f2a8fefd51add187d2572a0cf401c1ef Mon Sep 17 00:00:00 2001 From: J Date: Wed, 30 Oct 2024 13:31:40 +0000 Subject: [PATCH] add comments in the server --- server.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/server.ts b/server.ts index e448770..82748f6 100644 --- a/server.ts +++ b/server.ts @@ -1,4 +1,4 @@ -import { blue } from "std/fmt/colors.ts"; +import { blue, red } from "std/fmt/colors.ts"; import { type Order, orderDecider, @@ -24,21 +24,31 @@ import { } from "./lib/api_schema.ts"; import type { Command, Event } from "./lib/api.ts"; +// Open the key-value store: running in-memory const kv = await Deno.openKv(":memory:"); + +// Listen to events from kv queue and apply them to the materialized view +// retry policy can specified on kv.enqueue method (optionally enabled in the DenoEventRepository) kv.listenQueue(async (raw) => { try { + // Parse the event and metadata from the raw data / Zod validation/parsing const event: Event & EventMetadata = eventAndMetadataSchema.parse(raw); console.log(blue("Handling event: "), event); - const readRepository = new DenoViewStateRepository(kv); + // Combine views to create a new view that can handle both restaurant and order events const view = restaurantView.combine(orderView); + // Create a repository for the view state / a Deno implementation of the IViewStateRepository + const readRepository = new DenoViewStateRepository(kv); + // Create a materialized view to handle the events of all types / MaterializedView is composed of a view and a read repository const materializedView: ApplicationMaterializedView = new MaterializedView( view, readRepository, ); + // Handle the events of all types const result = await materializedView.handle(event); console.log(blue("Result of event handling: "), result); } catch (error) { - console.error("Error of event handling: ", error); + // Catch & no throw to prevent queue retries + console.log(red("Error of event handling: "), error); } }); @@ -52,11 +62,10 @@ Deno.serve(async (request: Request) => { console.log(blue("Handling command: "), command); - // Open the key-value store // Combine deciders to create a new decider that can handle both restaurant and order commands const decider: Decider = restaurantDecider.combine(orderDecider); - // Create a repository for the events / a Deno implementation of the IEventRepository + // Create a repository for the events / a Deno implementation of the IEventRepository and optionally enable event enqueueing const eventRepository = new DenoEventRepository(kv, true); // Create an aggregate to handle the commands of all types / Aggregate is composed of a decider and an event repository const aggregate: ApplicationAggregate = new EventSourcingAggregate( @@ -72,7 +81,7 @@ Deno.serve(async (request: Request) => { }); } catch (error) { console.error("Error of command handling: ", error); - return new Response(error?.message ?? error, { + return new Response((error as Error)?.message ?? error, { headers: { "Content-Type": "application/json" }, status: 500, });