Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Textalk/event-store-promise
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.0.1
Choose a base ref
...
head repository: Textalk/event-store-promise
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Mar 30, 2016

  1. Updating README with API docs.

    Fredrik Liljegren committed Mar 30, 2016
    Copy the full SHA
    03fd349 View commit details
Showing with 96 additions and 9 deletions.
  1. +96 −9 README.md
105 changes: 96 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
Event Store Promises
====================

An interface to [Event Store](https://geteventstore.com/) using
[Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
and [Streams](https://nodejs.org/api/stream.html).

Programming for event sourcing fits perfectly with javascripts asynchronous handling of Promises
and Streams. This lib puts a layer on top of the protocol implementation
[event-store-client](https://github.com/x-cubed/event-store-client).


Install
-------

@@ -17,10 +29,10 @@ times out after 2 seconds.
```javascript
'use strict'

const esp = require('../event-store-promise/esp.js')
const async = require('asyncawait/async')
const await = require('asyncawait/await')
const _ = require('highland')
const EventStorePromise = require('event-store-promise')
const async = require('asyncawait/async')
const await = require('asyncawait/await')
const _ = require('highland')

// A convenient wrapper to await the result of a highland stream. Always gives an array though.
const await_ = stream => await(new Promise((res, rej) => stream.errors(rej).toArray(res)))
@@ -33,7 +45,7 @@ const streamId = 'testStream'

async(() => {
// Connect to EventStore.
const es = await(esp({
const esp = await(EventStorePromise({
host: '0.0.0.0',
port: 1113,
credentials: {username: 'admin', password: 'changeit'},
@@ -43,14 +55,14 @@ async(() => {
setInterval(async(() => {
const event = {foo: 'bar', date: Date()}
console.log('Adding event in stream', streamId)
await(es.writeEvents(streamId, es.ExpectedVersion.Any, false,
[{eventId: es.createGuid(), eventType: 'test', data: event}]))
await(esp.writeEvents(streamId, esp.ExpectedVersion.Any, false,
[{eventId: esp.createGuid(), eventType: 'test', data: event}]))
console.log('Event is now added asynchronously and awaited.')
}), 400)

try {
// Read all events up until event 10. This will wait for events to come in.
const result = await_(_(es.readStreamEventsUntil(streamId, 0, 10)).reduce(0, applyEvent))
const result = await_(_(esp.readStreamEventsUntil(streamId, 0, 10)).reduce(0, applyEvent))
console.log('Got result', result)
} catch (err) {
// Catching errors, probably timeout for too few events.
@@ -64,4 +76,79 @@ async(() => {
API Documentation
-----------------

Coming soon…
Generic parameters:

* `streamId`: The string identifier of an eventstore stream.
* `params`: Object with `timeout` (seconds), `resolveLinkTos` (boolean), `requireMaster` (boolean),
`maxCount` (integer)
* `from`, `to`, and `expectedVersion`: Refers to eventNumber, order nr in event stream.


## const esp = EventStorePromise(options)

Returns a promise of an esp object.

Options are passed to EventStore.Connection.

* `host`
* `port`
* `credentials` - an object with `username` and `password`.

## esp.close() → void

Closes the connection.


## esp.createGuid() → string (uuid)

## esp.sendPing() → Promise (of a pong)

## esp.subscribeToStream(streamId, params) → stream

The returned `stream` emits `open`, `data`, `close`, and `error`.

Call `stream.close()` to stop subscription.


## esp.readStreamEventsForward(streamId, from, params) → stream

The returned `stream` emits `open`, `data`, `close`, and `error`.


## esp.readStreamEventsUntil(streamId, from, to, params) → stream

Returns a stream of all events from `from` to `to`. If the eventstore isn't yet up to `to`, it
will subscribe and continue emitting events until `to` is reached, or `params.timeout` is reached.

The returned `stream` emits `open`, `data`, `close`, and `error`.


## esp.writeEvents(streamId, expectedVersion, requireMaster, events) → Promise (of written)

* `events`: An array of event objects. Each event object needs `eventId` (should be a uuid),
`eventType` (string), and `data` (free object).

Return: A Promise of `written`, an object with:

* `result`: integer, corresponding to esp.OperationResult values. Should be 0.
* `message`
* `firstEventNumber`
* `lastEventNumber`: Last written event number.
* `preparePosition`
* `commitPosition`


## esp.OperationResult / esp.OperationResult.getName(result)

Object with values of `written` result. Use `esp.OperationResult.getName(result)` to get the name
of the result.


## esp.readAllEventsBackward(…)

Not yet implemented.


## esp.readAllEventsForward(…)

Not yet implemented.