Skip to content

Instantly share code, notes, and snippets.

@graste
Last active May 3, 2021 16:49
Show Gist options
  • Save graste/7278494fd5d2f6b3da4678722308d13f to your computer and use it in GitHub Desktop.
Save graste/7278494fd5d2f6b3da4678722308d13f to your computer and use it in GitHub Desktop.
EventStore installation and usage http://geteventstore.com
  • Download/Installation:
  • on Ubuntu: sudo service eventstore start
  • Admin GUI: http://127.0.0.1:2113 (admin:changeit)
  • When eventstore is installed on host instead of in the virtualbox, use port forwarding:
    • vagrant ssh -- -R 2113:localhost:2113
  • Introduction: http://docs.geteventstore.com/introduction/
  • Store events to a stream:
    • streams are automatically created by posting data for the stream name
    • general format: [ { "eventId": "…", "eventType": "…", "data": {…}, "metadata": {…} } ] (plus UUID if PUTing is used)
    • POSTing single events is assumed when content type is application/json or application/xml
    • POSTing multiple events: curl -i -d @testevents.txt "http://127.0.0.1:2113/streams/newstream" -H "Content-Type:application/vnd.eventstore.events+json"
    • Single event POSTing allows to set eventType and eventId via headers ES-EventType and ES-EventId: curl -i -d @singleevent.txt "http://127.0.0.1:2113/streams/shop" -H "Content-Type: application/json" -H "ES-EventType: HelloCreated" -H "ES-EventId: 8f5ff3e6-0e26-4510-96c4-7e61a270e6f6"
  • Read streams:
    • as Atom XML feed: curl -i -H "Accept:application/atom+xml" "http://127.0.0.1:2113/streams/newstream"
    • as Atom JSON feed: curl -i -H "Accept: application/vnd.eventstore.atom+json" "http://127.0.0.1:2113/streams/newstream"
    • single event payload as json: curl -i -H "Accept: application/json" http://127.0.0.1:2113/streams/newstream/0
  • Setup ACL for a stream via the streams' metadata:
  • Media types:
    • application/vnd.eventstore.events+json and application/vnd.eventstore.events+xml
    • application/json and application/xml and text/xml
  • Projections
    • used to query streams and create new persistent ones if needed
    • when creating a projection one can specify whether to run it for history and stop or to run for the history and continue
    • there are two standard projections built-in:
      • $by_category for all events that have the same prefix (separator is -)
      • by_event_type for all events that have the same type
      • e.g.: fromCategory('foo') gets all events from all streams starting with name foo-
    • general format: fromStream("stream").when({ $init: f0, Event1: f1, Event2: f2, $any: f3 });
    • use $init function to set initial state for your projection
    • $any will match all events to your function (useful when not caring for the actual event type as e.g. general metadata is interesting)
    • emit events to streams: emit("stream", "eventtype", eventdata)
    • emit w/o copy: linkTo("stream", event) doesn't actually write the event to the stream, but emits a pointer to the event to the stream (which doesn't make a difference when fetching the stream)
    • List existing projections: curl -i 'http://localhost:2113/projections/any'
    • Show state of specific projection: curl -i 'http://localhost:2113/projection/$foo/state'
    • Show query of specific projection: curl -i 'http://localhost:2113/projection/$foo/query'
    • Change query of projection: curl -i -X PUT -d @proj.js 'http://localhost:2113/projection/$foo/query'
    • create transient projection: curl -i -d @projection-foo.js http://127.0.0.1:2113/projections/transient?enabled=yes -u admin:changeit -H "Content-Type: application/json" – response contains projection name to query curl -i http://127.0.0.1:2113/projection/uuid
    • create projection: curl -i -d @projection-foo.js 'http://127.0.0.1:2113/projections/continuous?name=$foo&enabled=yes&emit=yes&checkpoints=yes' -u admin:changeit -H "Content-Type: application/json" (alternative to continuous is onetime)
  • Subscriptions
    • Live-only subscriptions
    • Catch-up subscriptions
  • Configuration read order
    • Command Line Parameter (Most important)
    • Environment Variable
    • Configuration File
    • Default (Least important)

Links/Sources:

Forks of https://github.com/FriendsOfOuro/geteventstore-php-core:

fromCategory('honeybee.system_account.user')
.when({
$init: function(s,e) {
return {
count: 0
}
},
$any: function(s,e) {
return {
count: s.count+1
}
}
})

Create a projection named $countstuff via admin gui or cli:

fromStreams(["shop", "newstream"])
  .when({
    $init: function() {
      return { createsCount: 0, updatesCount: 0, deletesCount: 0 }
    },
    "OrderCreated": function(state, event) {
      state.createsCount += 1
    },
    "OrderUpdated": function(state, event) {
      state.updatesCount += 1
    },
    "OrderDeleted": function(state, event) {
      state.deletedCount += 1
    },
    "event-type": function(state, event) {
      state.updatesCount += 1
    }
})
  • get status: curl -i 'http://localhost:2113/projection/$countstuff'
  • get state: curl -i "http://127.0.0.1:2113/projection/$countstuff/state' -H "Accept: application/json"

Another example:

fromStream('$stats-127.0.0.1:2113').when({
  "$init" : function(s,e) {
    return { "count": 0 }
  },
  "$statsCollected" : function(s,e) {
    var currentCpu = e.body["sys-cpu"];
    if (currentCpu > 40) {
      s.count += 1;
      if (s.count >= 3)
        emit("heavycpu", "heavyCpuFound", {
          "level" : currentCpu,
          "count" : s.count
        });
    } else
      s.count = 0;
    }
  });
fromStream('$stats-127.0.0.1:2113')
  .when({
    "$statsCollected" : function(s,e) {
      var currentCpu = e.body["sys-cpu"];
      if (currentCpu > 40) {
        emit("heavycpu", "heavyCpuFound", {"level" : currentCpu})
      }
    }
  });
fromAll().
    when({
       $init : function(s,e) {return {count : 0}},
       $any  : function(s,e) {return {count : s.count +1}}
    })

Create personal stream of users (fetch via: curl -i "http://localhost:2113/streams/username):

fromAll().when({$any : function(s,e) { linkTo(e.metadata.username, e); });

Create stream for marked events:

fromAll().when({$any : function(s,e) { linkTo(e.metadata.correlationId, e); }})
[{
"eventId": "ebc744bb-c50d-451f-b1d7-b385c49b1087",
"eventType": "OrderCreated",
"data": {
"description": "Order has been created"
}
},
{
"eventId": "adaa388c-18c1-4be6-9670-6064bfd9f3dd",
"eventType": "OrderUpdated",
"data": {
"description": "Order has been updated"
}
},
{
"eventId": "4674d7df-4d3e-49eb-80fc-e5494d89a1bd",
"eventType": "OrderUpdated",
"data": {
"description": "Order has been updated"
}
}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment