Samples: Storing events and subscribing.
This commit is contained in:
parent
4f066bf00e
commit
39d5188f14
40
samples/storeevent.js
Normal file
40
samples/storeevent.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
var esClient = require('../src/client'); // When running in 'eventstore-node/samples' folder.
|
||||
// var esClient = require('eventstore-node'); // Otherwise
|
||||
var uuid = require('uuid');
|
||||
|
||||
var esConnection = esClient.createConnection({}, {"hostname": "localhost", "port": 1113});
|
||||
esConnection.connect();
|
||||
esConnection.once('connected', function (tcpEndPoint) {
|
||||
console.log('Connected to eventstore at ' + tcpEndPoint.hostname + ":" + tcpEndPoint.port);
|
||||
var userId = uuid.v4();
|
||||
// This event could happen as a result of (e.g.) a 'CreateUser(id, username, password)' command.
|
||||
var userCreatedEvent = {
|
||||
id: userId,
|
||||
username: "user" + uuid.v4().substring(0,6), // Hard-to-spell exotic username.
|
||||
password: Math.random().toString() // Hard-to-guess password.
|
||||
};
|
||||
var eventId = uuid.v4();
|
||||
var event = esClient.createJsonEventData(eventId, userCreatedEvent, null, "UserCreated");
|
||||
// Every user has her/his own stream of events:
|
||||
var streamName = "user-" + userId;
|
||||
console.log("Storing event. Look for it at http://localhost:2113/web/index.html#/streams/user-" + userId);
|
||||
esConnection.appendToStream(streamName, esClient.expectedVersion.any, event)
|
||||
.then(function(result) {
|
||||
console.log("Event stored.");
|
||||
process.exit(0);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.log(err);
|
||||
process.exit(-1);
|
||||
});
|
||||
});
|
||||
|
||||
esConnection.on('error', function (err) {
|
||||
console.log('Error occurred on connection:', err);
|
||||
process.exit(-1);
|
||||
});
|
||||
|
||||
esConnection.on('closed', function (reason) {
|
||||
console.log('Connection closed, reason:', reason);
|
||||
process.exit(-1);
|
||||
});
|
47
samples/subscribe.js
Normal file
47
samples/subscribe.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
var esClient = require('../src/client'); // When running in 'eventstore-node/samples' folder.
|
||||
// var esClient = require('eventstore-node'); // Otherwise
|
||||
|
||||
const credentialsForAllEventsStream = new esClient.UserCredentials("admin", "changeit");
|
||||
const resolveLinkTos = false;
|
||||
|
||||
var esConnection = esClient.createConnection({}, {"hostname": "localhost", "port": 1113});
|
||||
esConnection.connect();
|
||||
esConnection.once('connected', function (tcpEndPoint) {
|
||||
console.log('Connected to eventstore at ' + tcpEndPoint.hostname + ":" + tcpEndPoint.port);
|
||||
});
|
||||
|
||||
function belongsToAUserAggregate(event) {
|
||||
return event.originalEvent.eventStreamId.startsWith("user-")
|
||||
}
|
||||
|
||||
function eventAppeared(subscription, event) {
|
||||
// Ignore all events which aren't about users:
|
||||
if(belongsToAUserAggregate(event)) {
|
||||
var aggregateId = event.originalEvent.eventStreamId;
|
||||
var eventId = event.originalEvent.eventId;
|
||||
var eventType = event.originalEvent.eventType;
|
||||
var eventCreated = event.originalEvent.created;
|
||||
console.log(aggregateId, eventType, eventId, eventCreated);
|
||||
console.log(event.originalEvent.data.toString() + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
function subscriptionDropped(subscription, reason, error) {
|
||||
if (error) return test.done(error);
|
||||
console.log('Subscription dropped.');
|
||||
}
|
||||
|
||||
esConnection.subscribeToAll(resolveLinkTos, eventAppeared, subscriptionDropped, credentialsForAllEventsStream)
|
||||
.then(function(subscription) {
|
||||
console.log("subscription.isSubscribedToAll: " + subscription.isSubscribedToAll);
|
||||
})
|
||||
.catch(console.log("Caught."));
|
||||
|
||||
esConnection.on('error', function (err) {
|
||||
console.log('Error occurred on connection:', err);
|
||||
});
|
||||
|
||||
esConnection.on('closed', function (reason) {
|
||||
console.log('Connection closed, reason:', reason);
|
||||
process.exit(-1);
|
||||
});
|
Loading…
Reference in New Issue
Block a user