-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample_read.js
44 lines (37 loc) · 1.9 KB
/
sample_read.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { BACKWARDS, EventStoreDBClient, FORWARDS, START } from "@eventstore/db-client";
////////////////////////////////////////////////////////
//
// Step 1. Create client and connect it to EventStoreDB
//
////////////////////////////////////////////////////////
// Create an instance of EventStoreDBClient, connecting to the EventStoreDB at localhost without TLS
const client = EventStoreDBClient.connectionString("esdb://localhost:2113?tls=false");
///////////////////////////////////////////
//
// Step 2. Read all events from the stream
//
///////////////////////////////////////////
// Read events from the SampleStream
const stream_name = "SampleStream"; // Define the name of the stream to read from
let events = client.readStream( // Read events from stream
stream_name, // Specify the stream name
{ //
fromRevision: START, // Read from the start of the stream
direction: FORWARDS, // Read events forward in time
maxCount: 20 // Read at most 20 events
}
);
///////////////////////////////////////
//
// Step 3. Print each event to console
//
///////////////////////////////////////
for await (const resolvedEvent of events) { // For each event found in SampleStream
console.log("************************"); //
console.log("You have read an event!"); //
console.log("Stream: " + resolvedEvent.event?.streamId); // Print the stream name of the event
console.log("Event Type: " + resolvedEvent.event?.type); // Print the type of the event
console.log("Event Body: " + JSON.stringify(resolvedEvent.event?.data)); // Print the body of the event as a string
console.log("************************");
}
client.dispose(); // Close the client connection