The RailsEventStore gem can be easily used outside of Rails. To accomplish this you need to use the RubyEventStore gem, wchich is a part of RailsEventStore. It comes without any additional dependencies.
RubyEventStore uses InMemoryRepository as a storage backend - this means that published events are not persisted.
event_store = RubyEventStore::Client.new(
repository: RubyEventStore::InMemoryRepository.new
)
It's fairly easy to create a simple backend repository for storing published events. The simplest use case requires creating an object responding to create
method, accepting two arguments: event
object and stream_name
value.
class DynamodbRepository
def create(event, stream_name)
# DynamoDB persistance logic
end
end
Follow the instructions at http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html to install the databse locally.
Start the database server: (adjust the path accordingly)
$ java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
$ git clone [email protected]:siemakuba/res.git
gem install bundler
bundle install
Execute the code in dynamodb_create_table.rb
:
$ ruby dynamodb_create_table.rb
You should see the following output:
Created table. Status: ACTIVE
Execute the code in events.rb
:
$ ruby events.rb
You should see the following output, indicating that event has been published and consumed:
* Publishing FooEvent
- FooEventHandler received FooEvent with data: {:sample=>"Foo Event Data"}
- GeneralEventHandler received FooEvent with data: {:sample=>"Foo Event Data"}
* Publishing BarEvent
- BarEventHandler received BarEvent with data: {:sample=>"Bar Event Data"}
- GeneralEventHandler received BarEvent with data: {:sample=>"Bar Event Data"}
* Publishing OtherEvent
- GeneralEventHandler received OtherEvent with data: {:sample=>"Other Event Data"}
Execute the code in dynamodb_read_table.rb
:
$ ruby dynamodb_read_table.rb
You should see the following output, showing the stored events:
All items in table: 3
Stored items:
{"id"=>"e32926cf-1bc7-4a56-b3ba-1bf52597a957", "timestamp"=>"2017-11-07T09:54:54.334293+01:00", "info"=>{"metadata"=>{"timestamp"=>"2017-11-07T09:54:54+01:00"}, "event_type"=>"BarEvent", "stream_name"=>"all", "data"=>{"sample"=>"Bar Event Data"}}}
{"id"=>"dabd1724-a3bc-4279-9aee-f8eb73da5c90", "timestamp"=>"2017-11-07T09:54:54.322915+01:00", "info"=>{"metadata"=>{"timestamp"=>"2017-11-07T09:54:54+01:00"}, "event_type"=>"FooEvent", "stream_name"=>"all", "data"=>{"sample"=>"Foo Event Data"}}}
{"id"=>"463a3b6d-cf76-4634-80ed-5a7986158d52", "timestamp"=>"2017-11-07T09:54:54.342175+01:00", "info"=>{"metadata"=>{"timestamp"=>"2017-11-07T09:54:54+01:00"}, "event_type"=>"OtherEvent", "stream_name"=>"all", "data"=>{"sample"=>"Other Event Data"}}}