-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathapp.js
More file actions
45 lines (35 loc) · 1.04 KB
/
app.js
File metadata and controls
45 lines (35 loc) · 1.04 KB
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
45
const express = require('express')
const app = express()
const port = 8080
const {
EventStoreDBClient,
jsonEvent,
FORWARDS,
START,
} = require("@eventstore/db-client");
const eventStore = new EventStoreDBClient(
{ endpoint: 'esdb-local:2113' },
{ insecure: true }
)
const visitorsStream = 'visitors-stream'
app.get('/hello-world', async (req, res) => {
const visitor = req.query.visitor ?? 'Visitor'
const event = jsonEvent({
type: 'VisitorGreeted',
data: {
visitor,
},
})
await eventStore.appendToStream(visitorsStream, [event])
const eventStream = eventStore.readStream(visitorsStream, {
fromRevision: START,
direction: FORWARDS,
})
let visitorsGreeted = []
for await (const { event } of eventStream)
visitorsGreeted.push(event.data.visitor)
res.send(`${visitorsGreeted.length} visitors have been greeted, they are: [${visitorsGreeted.join(',')}]`)
})
app.listen(port, () => {
console.log(`Sample app listening on port ${port}`)
})