2017-05-17 19:46:27 +00:00
# node-eventstore-client
2016-03-09 20:46:15 +00:00
A port of the EventStore .Net ClientAPI to Node.js
2020-09-29 00:29:02 +00:00
## Learning
If you want to learn more about EventSourcing/CQRS/EventModeling, you can join the virtual workshop offered by my employer Adaptech Group, see info at [https://www.adaptechgroup.com/virtual-workshop/ ](https://www.adaptechgroup.com/virtual-workshop/ ).
2016-03-09 23:37:08 +00:00
## Status
2016-03-14 22:37:35 +00:00
### Missing features:
- Set system settings
2017-01-29 02:28:36 +00:00
### Areas to improve
- Errors
- Use codes or types to differentiate between errors
- Performance
- Performance hasn't been tested yet
- Tests
- Can always do with more tests
## Getting started
2016-03-09 20:46:15 +00:00
2017-05-17 19:46:27 +00:00
Install using `npm install node-eventstore-client`
2016-03-17 06:15:25 +00:00
2017-01-29 02:28:36 +00:00
### Dependencies
2017-10-18 21:39:25 +00:00
- Node.js >= 4.0
2019-05-16 09:59:56 +00:00
- Modules: [long ](https://www.npmjs.org/package/long ), [protobufjs ](https://www.npmjs.org/package/protobufjs ), [uuid ](https://www.npmjs.org/package/uuid ), [strict-event-emitter-types ](https://www.npmjs.com/package/strict-event-emitter-types ) (installed via `npm install` )
2017-01-29 02:28:36 +00:00
2018-03-11 22:25:44 +00:00
### Install and run an Eventstore on localhost
2017-11-08 21:06:16 +00:00
2018-03-11 22:25:44 +00:00
See https://eventstore.org/docs/introduction/4.1.0/
2017-11-08 21:06:16 +00:00
*Note: If you are using a version of EventStore prior to 3.9.4, you need to use version 0.1.x of this package `npm install node-eventstore-client@^0.1` .*
2017-01-29 02:28:36 +00:00
### API Documentation
2016-11-08 06:09:57 +00:00
#### Offline
2017-05-17 19:46:27 +00:00
The offline documentation can be found in the module folder `./node_modules/node-eventstore-client/docs` .
2016-11-08 06:09:57 +00:00
#### Online
2017-05-17 19:46:27 +00:00
The online documentation can be found at [https://dev.nicdex.com/node-eventstore-client/docs/ ](https://dev.nicdex.com/node-eventstore-client/docs/ )
2017-11-08 21:06:16 +00:00
2016-10-29 21:30:39 +00:00
### Example: Storing an event
2016-10-29 21:34:29 +00:00
Save to ```app.js:```
2016-10-29 21:30:39 +00:00
```javascript
2017-05-17 19:46:27 +00:00
var esClient = require('node-eventstore-client');
2016-10-29 21:30:39 +00:00
var uuid = require('uuid');
var streamName = "testStream";
2017-01-29 19:09:09 +00:00
/*
Connecting to a single node using "tcp://localhost:1113"
- to connect to a cluster via dns discovery use "discover://my.host:2113"
- to connect to a cluster via gossip seeds use
[
new esClient.GossipSeed({host: '192.168.1.10', port: 2113}),
new esClient.GossipSeed({host: '192.168.1.11', port: 2113}),
new esClient.GossipSeed({host: '192.168.1.12', port: 2113})
]
*/
var connSettings = {}; // Use defaults
var esConnection = esClient.createConnection(connSettings, "tcp://localhost:1113");
2016-10-29 21:30:39 +00:00
esConnection.connect();
esConnection.once('connected', function (tcpEndPoint) {
2017-01-29 02:04:58 +00:00
console.log('Connected to eventstore at ' + tcpEndPoint.host + ":" + tcpEndPoint.port);
2016-10-29 21:30:39 +00:00
});
var eventId = uuid.v4();
var eventData = {
a : Math.random(),
b: uuid.v4()
};
var event = esClient.createJsonEventData(eventId, eventData, null, 'testEvent');
console.log("Appending...");
esConnection.appendToStream(streamName, esClient.expectedVersion.any, event)
.then(function(result) {
console.log("Stored event:", eventId);
console.log("Look for it at: http://localhost:2113/web/index.html#/streams/testStream");
esConnection.close();
})
.catch(function(err) {
console.log(err);
});
```
2016-10-29 23:41:08 +00:00
Run:
2016-10-29 21:30:39 +00:00
```json
2016-10-29 23:41:08 +00:00
npm install uuid
2017-05-17 19:46:27 +00:00
npm install node-eventstore-client
2016-10-29 23:41:08 +00:00
node app.js
2016-10-29 21:30:39 +00:00
```
2016-11-01 14:26:27 +00:00
### Example: Subscribing to events
2016-03-10 01:08:03 +00:00
2016-11-01 14:26:27 +00:00
```cd samples```
To subscribe to all events from now on (includes example of a filter which ignores events which we aren't interested in):
```node subscribe-all-events.js```
To catch up on all events ever and subscribe to all new ones from now on:
```node subscribe-catchup-all-events.js```
To generate a test event, open a separate console and run:
```node store-event.js```
2016-10-15 22:41:25 +00:00
2016-03-09 23:37:08 +00:00
## Running the tests
2016-03-09 20:46:15 +00:00
2020-09-16 15:23:46 +00:00
### Local testing
2016-10-30 16:44:29 +00:00
To run the tests it is recommended that you use an in-memory instance of the eventstore so you don't pollute your dev instance.
2016-03-17 06:15:25 +00:00
2018-03-11 22:25:44 +00:00
EventStore.ClusterNode.exe --run-projections=all --memdb – certificate-file=yourcert.pfx
or
./run-node.sh --run-projections=all --memdb – certificate-file=yourcert.p12
2020-09-16 15:23:46 +00:00
You can also use docker-compose :
```bash
# start the single node cluster
npm run compose:single:start
# if you want to wait for the cluster to be available
npm run compose:wait
# run the tests
npm run test
# to cleanup (stop containres, delete volumes)
npm run compose:single:stop
```
2018-03-11 22:25:44 +00:00
For SSL setup see:
https://eventstore.org/docs/server/setting_up_ssl/
or
https://eventstore.org/docs/server/setting_up_ssl_linux/
2016-03-09 20:46:15 +00:00
2016-10-30 16:44:29 +00:00
To execute the tests suites simply run
2016-03-09 20:46:15 +00:00
2016-03-10 01:08:03 +00:00
npm test
2016-03-09 23:37:08 +00:00
2020-09-16 15:23:46 +00:00
### Isolated environment
To be able to run the tests for different connection types (tcp, gossip, cluster) docker-compose files are available to setup the environment and run the tests.
#### Prerequisites
* docker
* docker-compose
#### Run
To execute the tests suites for single node cluster (tcp connection) simply run
npm run test:single
To execute the tests suites for multiple nodes cluster (gossip connection) simply run
npm run test:gossip
To execute the tests suites for multiple nodes cluster (dns discovery connection) simply run
npm run test:cluster
2016-11-01 14:26:27 +00:00
## Porting .Net Task to Node.js
2017-01-29 02:28:36 +00:00
Any async commands returns a [Promise ](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise ) object in replacement of .Net Task.
2016-11-01 14:26:27 +00:00
2016-03-09 23:37:08 +00:00
## License
2017-05-17 19:46:27 +00:00
Ported code is released under the MIT license, see [LICENSE ](https://github.com/nicdex/node-eventstore-client/blob/master/LICENSE ).
2016-03-17 06:15:25 +00:00
Original code is released under the EventStore license and can be found at https://github.com/eventstore/eventstore.