Skip to content

Commit

Permalink
add comments in the server
Browse files Browse the repository at this point in the history
  • Loading branch information
barnesoir committed Oct 30, 2024
1 parent 023b622 commit 7ea904f
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { blue } from "std/fmt/colors.ts";
import { blue, red } from "std/fmt/colors.ts";
import {
type Order,
orderDecider,
Expand All @@ -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);
}
});

Expand All @@ -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<Command, (Order & Restaurant) | null, Event> =
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(
Expand All @@ -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,
});
Expand Down

1 comment on commit 7ea904f

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on 7ea904f Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

UNCAUGHT_EXCEPTION

TypeError: Non-default databases are not supported
    at Object.openKv (ext:deno_kv/01_db.ts:10:21)
    at file:///src/server.ts:28:23

Please sign in to comment.