node-eventstore-client/samples/store-event.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

const client = require('../src/client')
// const client = require("eventstore-node")
2017-04-21 02:32:53 +00:00
const uuid = require("uuid")
const settings = {}
const endpoint = "tcp://localhost:1113"
2017-04-21 02:32:53 +00:00
const connection = client.createConnection(settings, endpoint)
connection.connect().catch(err => console.log(err))
connection.once("connected", tcpEndPoint => {
const userId = uuid.v4()
const userCreatedEvent = {
id: userId,
username: `user${uuid.v4().substring(0,6)}`,
password: Math.random().toString()
}
const event = client.createJsonEventData(
uuid.v4(),
userCreatedEvent,
null,
"UserCreated"
)
// Every user has their own stream of events:
const streamName = `user-${userId}`
console.log(`Connected to eventstore at ${tcpEndPoint.host}:${tcpEndPoint.port}`)
console.log(`Storing event. Look for it at http://localhost:2113/web/index.html#/streams/user-${userId}`)
connection.appendToStream(streamName, client.expectedVersion.any, event)
.then(result => {
console.log("Event stored.")
process.exit(0)
})
.catch(error => {
console.log(error)
process.exit(-1)
})
})
connection.on("error", error => {
console.log(`Error occurred on connection: ${error}`)
process.exit(-1)
})
connection.on("closed", reason => {
console.log(`Connection closed, reason: ${reason}`)
process.exit(-1)
})