Created
March 7, 2024 15:15
-
-
Save daveremy/a2ede08cca703e81ae137ad40d962eac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
EventStoreDBClient, | |
jsonEvent, | |
FORWARDS, | |
START, | |
JSONEventType, | |
} from "@eventstore/db-client"; | |
interface Reservation { | |
reservationId: string; | |
movieId: string; | |
userId: string; | |
seatId: string; | |
} | |
type SeatReservedEvent = JSONEventType< | |
"seat-reserved", | |
{ | |
reservationId: string; | |
movieId: string; | |
userId: string; | |
seatId: string; | |
} | |
>; | |
type SeatChangedEvent = JSONEventType< | |
"seat-changed", | |
{ | |
reservationId: string; | |
newSeatId: string; | |
} | |
>; | |
type ReservationEvents = SeatReservedEvent | SeatChangedEvent; | |
async function main(): Promise<void> { | |
try { | |
const client = EventStoreDBClient.connectionString("esdb://localhost:2113?tls=false"); | |
const streamName = "booking-abc123"; | |
console.log("Creating and appending events..."); | |
await createAndAppendEvents(client, streamName); | |
console.log("Reading and processing events..."); | |
const reservation = await readAndProcessEvents(client, streamName); | |
console.log("Final reservation state:", JSON.stringify(reservation, null, 2)); | |
} catch (error) { | |
console.error('An error occurred:', error); | |
} | |
} | |
async function createAndAppendEvents(client: EventStoreDBClient, streamName: string): Promise<void> { | |
const seatReservedEvent = jsonEvent<SeatReservedEvent>({ | |
type: "seat-reserved", | |
data: { | |
reservationId: "abc123", | |
movieId: "tt0368226", | |
userId: "nm0802995", | |
seatId: "4b", | |
}, | |
}); | |
const seatChangedEvent = jsonEvent<SeatChangedEvent>({ | |
type: "seat-changed", | |
data: { | |
reservationId: "abc123", | |
newSeatId: "5c", | |
}, | |
}); | |
await client.appendToStream(streamName, [seatReservedEvent, seatChangedEvent]); | |
console.log("Events appended to stream."); | |
} | |
async function readAndProcessEvents(client: EventStoreDBClient, streamName: string): Promise<Partial<Reservation>> { | |
const events = client.readStream<ReservationEvents>(streamName, { | |
fromRevision: START, | |
direction: FORWARDS, | |
maxCount: 10, | |
}); | |
const reservation: Partial<Reservation> = {}; | |
for await (const { event } of events) { | |
if (!event) continue; | |
switch (event.type) { | |
case "seat-reserved": { | |
console.log("Applying 'seat-reserved' event to reservation state."); | |
reservation.reservationId = event.data.reservationId; | |
reservation.movieId = event.data.movieId; | |
reservation.seatId = event.data.seatId; | |
reservation.userId = event.data.userId; | |
break; | |
} | |
case "seat-changed": { | |
console.log("Applying 'seat-changed' event to reservation state."); | |
reservation.seatId = event.data.newSeatId; | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
return reservation; | |
} | |
main().then(() => { | |
console.log('Execution completed successfully.'); | |
process.exit(0); | |
}).catch((error) => { | |
console.error('An error occurred:', error); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment