![]() ![]() Between looking up the events for a stream, and inserting our event, another event might have been inserted. Note that we cannot simply obtain the maximum version number from the database, before inserting a new event. Inserting a new event is then as simple as: INSERT INTO events(stream_id, version, data) Let's assume this is available as current. This can be done by iterating over the events found in the first step, and finding the maximum version value. Here, we need to first calculate the current version number, so that we can insert an event with the next version. The second basic operation lets us insert a new event. Thanks to the version number, we know how to order events. Given a function f: (State, Event) => State, and an initial "empty" state, the current state can be obtained by repeatedly applying f from the first to the last event, using richer and richer State values this is also known as a fold. This lets us reconstruct the state of any stream. WHERE stream_id = :stream_id ORDER BY version ASC First, looking up all events for a stream with the given :stream_id: SELECT id, stream_id, version, data FROM events Using this simple structure, we can already implement the two basic operations on an event store. Here’s a couple of example events that use the above structure: (id=10, stream_id=345, version=1, data=) Each event has different data, so we're keeping this unstructured. Each new event for a given stream increments the version number, and there can be only one event with a given version in a given stream,
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |