{"id":13776694,"url":"https://github.com/pedroassumpcao/incident","last_synced_at":"2026-02-20T00:31:52.079Z","repository":{"id":34936349,"uuid":"186066919","full_name":"pedroassumpcao/incident","owner":"pedroassumpcao","description":"Event Sourcing and CQRS library in Elixir","archived":false,"fork":false,"pushed_at":"2023-04-17T11:59:21.000Z","size":762,"stargazers_count":158,"open_issues_count":4,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-12-02T11:39:23.791Z","etag":null,"topics":["cqrs","elixir","event-sourcing"],"latest_commit_sha":null,"homepage":null,"language":"Elixir","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/pedroassumpcao.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-10T23:39:05.000Z","updated_at":"2025-08-18T17:34:34.000Z","dependencies_parsed_at":"2024-08-03T18:10:38.262Z","dependency_job_id":"e561a227-6022-4535-a3d9-5daa0325a2a9","html_url":"https://github.com/pedroassumpcao/incident","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/pedroassumpcao/incident","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedroassumpcao%2Fincident","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedroassumpcao%2Fincident/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedroassumpcao%2Fincident/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedroassumpcao%2Fincident/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pedroassumpcao","download_url":"https://codeload.github.com/pedroassumpcao/incident/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedroassumpcao%2Fincident/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29637409,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T22:32:43.237Z","status":"ssl_error","status_checked_at":"2026-02-19T22:32:38.330Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["cqrs","elixir","event-sourcing"],"created_at":"2024-08-03T18:00:31.687Z","updated_at":"2026-02-20T00:31:52.061Z","avatar_url":"https://github.com/pedroassumpcao.png","language":"Elixir","funding_links":[],"categories":["Elixir","Libraries"],"sub_categories":[],"readme":"# Incident\n\nEvent Sourcing and CQRS building blocks.\n\nSpecial thanks to my friend [Paulo Gonzalez](https://github.com/pdgonzalez872) for the name suggestion\nfor this library.\n\n*This library is in constant development phase and evaluation, that means core changes still can be made.*\n*While I am already using Incident in production, please be advised about the risks, once the library\nreaches its maturity, a release 1.x.x will be created.*\n\n## Goals\n\n* incentivize the usage of **Event Sourcing** and **CQRS** as good choice for domains that can leverage\nthe main benefits of this design pattern;\n* offer the essential building blocks for using Event Sourcing in your system with proper contracts, but\nallowing specific needs to leverage what Elixir already brings to the table, for example, concurrency;\n* leverage functions and reducers for executing commands and applying events, facilitating stateless tests;\n* allow customization for fine-grained needs without compromising the principles;\n\n## Getting Started\n\n### Example Application\n\nThere is an [example application](https://github.com/pedroassumpcao/incident/tree/master/examples/bank)\nthat implements a **Bank** application for reference with great documentation and including all the\ndetails and usage in **IEx** as well.\n\nIt also contains projections specific to the application domain with migration and schemas defined.\n\nThis example application will give you all the details in how to use **Incident**, including integration\ntests for both `InMemory` and `Postgres` adapters for both Event Store and Projection Store.\n\n### Blog Posts\n\n* [Using Event Sourcing and CQRS with Incident - Part 1](https://pedroassumpcao.ghost.io/event-sourcing-and-cqrs-using-incident-part-1/)\n\n## Event Sourcing and CQRS\n\nIn a nutshell, Event Sourcing ensures that all changes to application state are stored as a sequence of\nevents. But if you are new to **Event Sourcing** and **CQRS** I highly recommend watch [Greg Young's\npresentation at Code on the Beach 2014](https://www.youtube.com/watch?v=JHGkaShoyNs) before moving forward.\n\n## Main Components\n\n### Command\n\nIt is a data structure used to define the command attributes with some basic validation.\n\n### Command Handler\n\nIt is the entry point of a command in the **write side**. It performs basic command validation and\nexecutes the command through the **Aggregate** logic.\n\n### Aggregate\n\nDefines how a specific entity (_User_, for example) in your domain will execute each of its commands and\napply each of its events. The aggregate itself only defines the logic but not its state.\n\n### Aggregate State\n\nDefines the initial state of an **Aggregate** and it is able to calculate the new state by replaying all\nthe events through the aggregate logic.\n\n### Event\n\nIt is a data structure that defines the **event data** attributes.\n\n### Event Handler\n\nReceives a persisted event and perform actions in the **read side** such as to update the projection for\nan individual aggregate. Another usage is to build a new command and send to the Command Hanlder when\nspecific events should trigger a new cycle.\n\n### Projection\n\nIt represents the current state of an individual aggregate (_User ID: 123456_, for example) after\nreplaying all events. Your application reads/queries data from the projection, that is similar to a\npersisted cache.\n\n### Event Store\n\nAll events from _all aggregates_ are stored in the Event Store. The Event Store uses the Port/Adapter\ndesign pattern so through configuration you can define which adapter your application will use to store\nthe events. Currently, Incident comes with two adapters, an `InMemory` to be used as playground and a\n`Postgres` one.\n\n### Projection Store\n\nVery similar to the Event Store, the Projection Store uses the Port/Adapter design pattern so through\nconfiguration you can define which adapter your application will use to store the aggregate projections.\nCurrently, Incident comes with two adapters, an `InMemory` to be used as playground and a `Postgres` one.\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `incident` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:incident, \"~\u003e 0.6.2\"}\n  ]\nend\n```\n\n## Usage\n\n`Incident` will be added as part of the application supervision tree, configuring the the adapters for the\n**Event Store** and the **Projection Store**.\n\n### With Postgres Adapters\n\nThe Postgres adapter uses `Ecto` behind the scenes so a lot of its configuration it is simply how you should\nconfigure a Postgres database for any application using Ecto.\n\n**There will be two Ecto Repos**, one for the events and another one for the projections.\n\n_Optionally, you can even define two different databases, so the events database will contain only one\ntable (events) and the projections database will contain one table fore each projection type._\n\nAdd the Ecto Repo modules for both databases:\n\n```elixir\ndefmodule AppName.EventStoreRepo do\n  use Ecto.Repo,\n    otp_app: :app_name,\n    adapter: Ecto.Adapters.Postgres\nend\n\ndefmodule AppName.ProjectionStoreRepo do\n  use Ecto.Repo,\n    otp_app: :app_name,\n    adapter: Ecto.Adapters.Postgres\nend\n\n```\n\nIn your application `config.exs` specify the repositories:\n\n```elixir\nconfig :app_name, ecto_repos: [AppName.EventStoreRepo, AppName.ProjectionStoreRepo]\n```\n\nIn your application `dev|test|prod.exs` (the example below defines two databases but it could be only one):\n\n```elixir\nconfig :app_name, AppName.EventStoreRepo,\n  username: \"postgres\",\n  password: \"postgres\",\n  hostname: \"localhost\",\n  database: \"app_name_event_store_dev\"\n\nconfig :app_name, AppName.ProjectionStoreRepo,\n  username: \"postgres\",\n  password: \"postgres\",\n  hostname: \"localhost\",\n  database: \"app_name_projection_store_dev\"\n```\n\nCreate the application databases running the Ecto mix task:\n\n```bash\nmix ecto.create\n```\n\nUse the Incident Mix Task below to generate the `events` and `aggregate_locks` table migrations, after\nthat, run the migration task:\n\n```bash\nmix incident.postgres.init -r AppName.EventStoreRepo\nmix ecto.migrate\n```\n\n_The migrations and schemas for the projections will depend on your application domains and it follows\nthe same process for any Ecto Migration._\n\nAdd `Incident` in the `application.ex`, adding into the application supervision tree:\n\n```elixir\ndefmodule AppName.Application do\n  @moduledoc false\n\n  use Application\n\n  def start(_type, _args) do\n    config = %{\n      event_store: %{\n        adapter: :postgres,\n        options: [repo: AppName.EventStoreRepo]\n      },\n      projection_store: %{\n        adapter: :postgres,\n        options: [repo: AppName.ProjectionStoreRepo]\n      }\n    }\n\n    children = [\n      AppName.EventStoreRepo,\n      AppName.ProjectionStoreRepo,\n      {Incident, config}\n    ]\n\n    opts = [strategy: :one_for_one, name: AppName.Supervisor]\n    Supervisor.start_link(children, opts)\n  end\nend\n```\n\n### With InMemory Adapters\n\nIn case of `InMemory` adapters that use `Agent` there is no need for any `Ecto` configuration so it is simply added`Incident` to the application supervision tree:\n\n```elixir\ndefmodule AppName.Application do\n  @moduledoc false\n\n  use Application\n\n  def start(_type, _args) do\n    config = %{\n      event_store: %{\n        adapter: :in_memory,\n        options: []\n      },\n      projection_store: %{\n        adapter: :in_memory,\n        options: [\n          initial_state: %{AppName.Projections.ProjectionName =\u003e []}\n        ]\n      }\n    }\n\n    children = [\n      {Incident, config}\n    ]\n\n    opts = [strategy: :one_for_one, name: AppName.Supervisor]\n    Supervisor.start_link(children, opts)\n  end\nend\n```\n\n## Planned Next Steps\n\nThe list below is the upcoming enhacements or fixes, it will grow as the library is being developed.\n\n- [ ] allow Incident to be used by more than one application within the umbrella, if needed;\n- [ ] add Telemetry module and trigger telemetry events;\n- [ ] add Event Snapshots to improve performance for aggregates with long event history;\n- [ ] run migrations when using `mix incident.postgres.init` for `Postgres` adapter;\n- [ ] allow custom error modules to be used and incorporate as part of the contract in some components;\n\n## Contributing\n\nWe appreciate any contribution to Incident. Please see the [Code of Conduct](https://github.com/pedroassumpcao/incident/blob/master/CODE_OF_CONDUCT.md) and [Contributing](https://github.com/pedroassumpcao/incident/blob/master/CONTRIBUTING.md) guides.\n\n## Documentation\n\nThe full documentation can be found at [https://hexdocs.pm/incident](https://hexdocs.pm/incident).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedroassumpcao%2Fincident","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpedroassumpcao%2Fincident","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedroassumpcao%2Fincident/lists"}