Event Store Rest API Basics (Node.js)

Event Store Node.js client

I recently finished my first project written in Node.js with EventStore as data-store. I published the some of the code used to access the EventStore to github, feel free to play with it. The official documentation can be found the the EventStore web-site.

The code to load/save the events is coded in a repository and contains 3 methods:

Have a look at the implementation on github. And at jasmine test file for the full examples.

Loading the events of an aggregate

The load method loads all events of a stream to be able to replay them on an aggregate. The events are loaded in batch. (In this example per 5)

The load method returns the current version of the stream and all stored events:

When the stream doesn’t exists, the load methods returns an empty event array with -1 as version. -1 should be used as version to save events to a new streams.

ALSO READ  Event Store Projection in C#

Load the last event of a stream

To get the state of an projection the last event of a stream is loaded. This is done by requesting the head of the stream.

The loadLast method returns the version of the stream, and the name and the body of the last event.

Saving the events of an aggregate

The save methods appends to specified event to the stream. It also performs a concurrency check based on the version. When the version is not correct the callback returns with an error.

What’s next

In the next posts I describe how I managed projections and applied event-sourcing on an aggregate in JavaScript.

Source code

A working project for these examples can be found on github: https://github.com/tim-cools/EventStore-Node-Examples

Event Store Projections by Example

This post is part of a series:

  1. EventStore Client API Basics (C#)
  2. Counting events of a specific type
  3. Partition events based on data found in previous events
  4. Calculating an average per day
  5. The irresponsible gambler
  6. Distribute events to other streams
  7. Temporal Projection to generate alarms
  8. Projection in C#

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *