{"id":15064644,"url":"https://github.com/blocknotes/rails-event-sourcing","last_synced_at":"2026-01-02T08:15:47.384Z","repository":{"id":45978283,"uuid":"429760696","full_name":"blocknotes/rails-event-sourcing","owner":"blocknotes","description":"Layer to setup an event sourcing Rails application","archived":false,"fork":false,"pushed_at":"2021-11-23T11:16:58.000Z","size":53,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-23T12:40:40.032Z","etag":null,"topics":["rails","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blocknotes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-19T10:42:16.000Z","updated_at":"2021-11-20T13:44:37.000Z","dependencies_parsed_at":"2022-07-20T04:02:25.910Z","dependency_job_id":null,"html_url":"https://github.com/blocknotes/rails-event-sourcing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blocknotes%2Frails-event-sourcing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blocknotes%2Frails-event-sourcing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blocknotes%2Frails-event-sourcing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blocknotes%2Frails-event-sourcing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blocknotes","download_url":"https://codeload.github.com/blocknotes/rails-event-sourcing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243790999,"owners_count":20348385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rails","ruby","ruby-on-rails"],"created_at":"2024-09-25T00:23:23.870Z","updated_at":"2026-01-02T08:15:47.330Z","avatar_url":"https://github.com/blocknotes.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rails Event Sourcing\n[![gem version](https://badge.fury.io/rb/rails-event-sourcing.svg)](https://rubygems.org/gems/rails-event-sourcing)\n[![linters](https://github.com/blocknotes/rails-event-sourcing/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/rails-event-sourcing/actions/workflows/linters.yml)\n[![specs](https://github.com/blocknotes/rails-event-sourcing/actions/workflows/specs.yml/badge.svg)](https://github.com/blocknotes/rails-event-sourcing/actions/workflows/specs.yml)\n\nThis gem provides features to setup an event sourcing application using ActiveRecord.\nActiveJob is necessary only to use async callbacks.\n\n\u003e DISCLAIMER: this project is in alpha stage\n\nThe main components are:\n- **event**: track state changes for an application model;\n- **command**: wrap events creation;\n- **dispatcher**: events' callbacks (sync and async).\n\nThis gem adds a layer to handle events for the underlying application models. In short:\n- setup: an event model is created for each \"event-ed\" application model;\n- usage: creating/updating/deleting application entities is applied via events;\n- every change to an application model (named _aggregate_ in the event perspective) is stored in an event record;\n- querying application models is the same as usual.\n\n:star: if you like it, please.\n\nA sample usage workflow:\n\n```rb\n# Load a plain Post model:\npost = Post.find(1)\n# Update that post's description:\nPosts::ChangedDescriptionEvent.create!(post: post, description: 'My beautiful post content')\n# Create a new post:\nPosts::CreatedEvent.create!(title: 'New post!', description: 'Another beautiful post')\n# List events for an aggregated entity (in this case Posts::Event is a STI base class for the events):\nevents = Posts::Event.events_for(post)\n# Rollback the post to a specific version:\nevents[2].rollback!\n# The aggregated entity is restored to the specific state, the events above that point are removed\n```\n\n:information_source: this project is based on the [event-sourcing-rails-todo-app-demo](https://github.com/pcreux/event-sourcing-rails-todo-app-demo) proposed by [Philippe Creux](https://github.com/pcreux) and his [video presentation](https://www.youtube.com/watch?v=ulF6lEFvrKo) for the Rails Conf 2019 :rocket:\n\n## Usage\n\n- Add to your Gemfile: `gem 'rails-event-sourcing'` (and execute `bundle`)\n- Create a migration per model to store the related events, example for a User model:\n`bin/rails generate migration CreateUserEvents type:string user:reference data:text metadata:text`\n- Create the required events, example to create a User:\n```rb\nmodule Users\n  class CreatedEvent \u003c RailsEventSourcing::BaseEvent\n    self.table_name = 'user_events' # usually this fits better in a base class using STI\n\n    belongs_to :user, autosave: false\n\n    data_attributes :name\n\n    def apply(user)\n      # this method will be applied when the event is created\n      user.name = name\n      # the aggregated entity must be returned\n      user\n    end\n  end\nend\n```\n- Create an event (which applies the User creation) with: `Users::CreatedEvent.create!(name: 'Some user')`\n- Optionally define a create Command, for example:\n```rb\nmodule Users\n  class CreateCommand\n    include RailsEventSourcing::Command\n\n    attributes :user, :name\n\n    def build_event\n      # this method will prepare the event when the command is executed\n      Users::CreatedEvent.new(user_id: user.id, name: name)\n    end\n  end\nend\n```\n- Invoke it with: `Users::CreateCommand.call(name: 'Some name')`\n\nPlease take a look at the [dummy app](spec/dummy/app) for a complete example.\nIn this case I preferred to store events models in _app/events_, commands in _app/commands_ and dispatchers in _app/dispatchers_ - but this is not mandatory. Another option could be to have an `Events` namespace and a single event could be: `Events::TodoItem::CreatedEvent`.\n\n## Examples\n\nEvents:\n```rb\nTodoLists::Created.create!(name: 'My TODO 1')\nTodoLists::NameUpdated.create!(name: 'My TODO!', todo_list: TodoList.first)\nTodoItems::Created.create!(todo_list_id: TodoList.first.id, name: 'First item')\nTodoItems::Completed.create!(todo_item: TodoItem.last)\n```\n\nCommands:\n```rb\nTodoLists::Create.call(name: 'My todo')\nTodoItems::Create.call(todo_list: TodoList.first, name: 'Some task')\n```\n\nDispatchers:\n```rb\nclass TodoItemsDispatcher \u003c RailsEventSourcing::EventDispatcher\n  on TodoItems::Created, trigger: -\u003e(todo_item) { puts \"\u003e\u003e\u003e TodoItems::Created [##{todo_item.id}]\" }\n  on TodoItems::Completed, async: Notifications::TodoItems::Completed\nend\n# When the event TodoItems::Created is created the trigger callback is executed\n# When the event TodoItems::Completed is created a job to create a Notifications::TodoItems::Completed event is scheduled\n```\n\n## To do\n\n- [ ] Generators for events, commands and dispatchers\n- [ ] Database specific optimizations\n- [ ] Add more usage examples\n\n## Do you like it? Star it!\n\nIf you use this component just star it. A developer is more motivated to improve a project when there is some interest.\n\nOr consider offering me a coffee, it's a small thing but it is greatly appreciated: [about me](https://www.blocknot.es/about-me).\n\n## Contributors\n\n- [Mattia Roccoberton](https://www.blocknot.es): author\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblocknotes%2Frails-event-sourcing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblocknotes%2Frails-event-sourcing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblocknotes%2Frails-event-sourcing/lists"}