Event.Store
Event.Store is an open source library to create Event Stores
that works with multiple persistence providers and notification systems.
The Event Store is a database accompanied by a publication and subscription system.
The database stores all the events related to an event stream. The pub / sub system allows
other systems or microservices to react to changes in event streams. It is a core component in
any event sourcing + CQRS architectures.
Event.Store has client implementations for Node.js and
Java. The two clients can interact with the same
Event Store.
The Event Store support the folowing persistence providers:
And the folowing notification systems:
It is possible to create custom providers and publishers.
Installation
To install the library:
npm install --save @eventstore.net/event.store
dependencies {
compile 'br.net.eventstore:event.store:1.0.3'
}
<dependency>
<groupId>br.net.eventstore</groupId>
<artifactId>event.store</artifactId>
<version>1.0.3</version>
</dependency>
Usage Guide
Create EventStore
To Create an EventStore you must provide two implementations:
- A persistence Provider: Responsible for events persistence in the store.
- A notification Publisher (Optional): Responsible for notify any process interested in modifications
on the store streams.
If there is no publisher provided, the event store will not send any notification.
const eventStore = new EventStore(
new InMemoryProvider(), // The persistence provider. Could use different providers, like MongoDB etc
new InMemoryPublisher()); // Opcional. Support different publishers, like RabbitmqPublisher, RedisPublisher etc
EventStore eventStore = new EventStoreBuilder()
.setProvider(new InMemoryProvider()) // The persistence provider. Could use different providers, like MongoDB etc
.setPublisher(new InMemoryPublisher()) // Opcional. Support different publishers, like RabbitmqPublisher, RedisPublisher etc
.createEventStore();
Adding Events
To add Events you need to ask to EventStore a reference to an EventStream.
You can add Events passing anything you want as a payload.
const ordersStream = eventStore.getEventStream('orders', '1234567');
ordersStream.addEvent({ data: 'My Event Payload'}); // Could pass anything here
EventStream ordersStream = eventStore.getEventStream("orders", "1234567");
ordersStream.addEvent(new EventPayload("My Event Payload")); // Could pass a JSON string here
Reading to Events
To read Events you need to ask to EventStore a reference to an EventStream.
You can read a stream to receive an ordered list containing all the events in the store.
const ordersStream = eventStore.getEventStream('orders', '1234567');
const events = await ordersStream.getEvents();
const order = ordersAggregation.loadFromHistory(events)
EventStream ordersStream = eventStore.getEventStream("orders", "1234567");
List<Event> events = ordersStream.getEvents();
Order order = ordersAggregation.loadFromHistory(events)
Reacting to Events
You can add subscribers to be notified every time a new event is added in any stream contained in the given aggregation.
The following example will be notified for every new event in any stream in the "orders" aggregation.
eventStore.subscribe('orders', message => {
console.log(message.aggregation);
console.log(message.streamId);
console.log(message.event.payload);
});
eventStore.subscribe("orders", message -> {
System.out.println(message.getAggregation());
System.out.println(message.getStreamId());
System.out.println(getEvent().getPayload());
});
Removing subscriptions
It is possible to cancel subscriptions to event stream channels.
const subscription = await eventStore.subscribe('orders', message => {
console.log(message.aggregation);
console.log(message.streamId);
console.log(message.event.payload);
});
// ...
subscription.remove();
Subscription subscription = eventStore.subscribe("orders", message -> {
System.out.println(message.getAggregation());
System.out.println(message.getStreamId());
System.out.println(getEvent().getPayload());
});
// ...
subscription.remove();