Installation
Installation with Bundler
If your application dependencies happen to be managed by Bundler, please add the following line to your Gemfile
:
gem "rails_event_store"
After running bundle install
, Rails Event Store should be ready to be used.
Kickstarting new Rails application with RailsEventStore
If you're setting up a new Rails app, there is even a faster way to begin with RailsEventStore. The template will install required gems, perform initial database migration, pre-configure event browser and more — rails new -m https://railseventstore.org/new APP_NAME
Make sure to check generated config/initializers/rails_event_store.rb
for initial configuration.
Installation using RubyGems
You can also install this library using the gem
command:
gem install rails_event_store
After requiring rubygems
in your project you should be ready to use Rails Event Store.
Setup data model
Use provided task to generate a table to store events in your database.
spring stop # if you use spring
rails generate rails_event_store_active_record:migration
rake db:migrate
Rails 5.0–5.1 with SQLite
If you're setting up a Rails 5.0 or Rails 5.1 app with sqlite database (i.e. for development)., you may encounter an issue when creating event_store_events_table — ArgumentError: Index name 'sqlite_autoindex_event_store_events_1' on table 'event_store_events' already exists
.
This is a known issue fixed in Rails 5.2. If you're going to stick with this configuration make sure to remove t.index ["id"], name: "sqlite_autoindex_event_store_events_1", unique: true
line from db/schema.rb
file.
Instantiate a client
# config/environments/*.rb
Rails.application.configure do
config.to_prepare do
Rails.configuration.event_store = RailsEventStore::Client.new
# add subscribers here
end
end
or
# config/application.rb
module YourAppName
class Application < Rails::Application
config.to_prepare do
Rails.configuration.event_store = RailsEventStore::Client.new
# add subscribers here
end
end
end
or
# config/initializers/rails_event_store.rb
Rails.configuration.to_prepare do
Rails.configuration.event_store = RailsEventStore::Client.new
# add subscribers here
end
Then in your application code you can reference it as:
Rails.configuration.event_store
In Rails development mode when you change a registered class, it is reloaded, and a new class with same name is constructed. To keep RailsEventStore
aware of changes in event classes, handler classes, and handler subscriptions use to_prepare
callback. It is executed before every code reload in development, and once in production.