{"id":13513861,"url":"https://github.com/8thlight/ex_state","last_synced_at":"2025-10-21T14:57:31.761Z","repository":{"id":62429438,"uuid":"246622294","full_name":"8thlight/ex_state","owner":"8thlight","description":"Database-backed state machines and statecharts for Elixir","archived":false,"fork":false,"pushed_at":"2020-05-27T16:12:51.000Z","size":131,"stargazers_count":116,"open_issues_count":0,"forks_count":4,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-10-21T14:57:16.211Z","etag":null,"topics":["elixir"],"latest_commit_sha":null,"homepage":"","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/8thlight.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-11T16:25:05.000Z","updated_at":"2025-06-20T20:05:21.000Z","dependencies_parsed_at":"2022-11-01T20:05:32.388Z","dependency_job_id":null,"html_url":"https://github.com/8thlight/ex_state","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/8thlight/ex_state","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8thlight%2Fex_state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8thlight%2Fex_state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8thlight%2Fex_state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8thlight%2Fex_state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/8thlight","download_url":"https://codeload.github.com/8thlight/ex_state/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/8thlight%2Fex_state/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280281400,"owners_count":26303709,"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","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["elixir"],"created_at":"2024-08-01T05:00:38.968Z","updated_at":"2025-10-21T14:57:31.740Z","avatar_url":"https://github.com/8thlight.png","language":"Elixir","funding_links":[],"categories":["Elixir"],"sub_categories":[],"readme":"# ExState\n\n[![Hex.pm](https://img.shields.io/hexpm/v/ex_state_ecto.svg)](https://hex.pm/packages/ex_state_ecto)\n[![Hex Docs](https://img.shields.io/badge/hexdocs-release-blue.svg)](https://hexdocs.pm/ex_state_ecto/ExState.html)\n\nElixir state machines, statecharts, and workflows for Ecto models.\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `ex_state` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:ex_state_ecto, \"~\u003e 0.3\"}\n  ]\nend\n```\n\nDocumentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)\nand published on [HexDocs](https://hexdocs.pm). Once published, the docs can\nbe found at [https://hexdocs.pm/ex_state](https://hexdocs.pm/ex_state).\n\n## Usage\n\n### Without Ecto\n\n- [Example](test/ex_state/examples/vending_machine_test.exs)\n\n### Ecto Setup\n\n```elixir\ndefmodule MyApp.Repo.Migrations.AddWorkflows do\n  def up do\n    # Ensure Ecto.UUID support is enabled:\n    execute(\"CREATE EXTENSION IF NOT EXISTS pgcrypto\")\n\n    ExState.Ecto.Migration.up()\n  end\n\n  def down do\n  end\nend\n```\n\n```elixir\nconfig :ex_state, repo: MyApp.Repo\n```\n\n### Defining States\n\nDefine the workflow:\n\n```elixir\ndefmodule SaleWorkflow do\n  use ExState.Definition\n\n  alias MyApp.Repo\n\n  workflow \"sale\" do\n    subject :sale, Sale\n\n    participant :seller\n    participant :buyer\n\n    initial_state :pending\n\n    state :pending do\n      on :send, :sent\n      on :cancel, :cancelled\n    end\n\n    state :sent do\n      parallel do\n        step :acknowledge_receipt, participant: :buyer\n        step :close, participant: :seller\n      end\n\n      on :cancelled, :cancelled\n      on_completed :acknowledge_receipt, :receipt_acknowledged\n      on_completed :close, :closed\n    end\n\n    state :receipt_acknowledged do\n      step :close, participant: :seller\n      on_completed :close, :closed\n    end\n\n    state :closed\n\n    state :cancelled do\n      on_entry :update_cancelled_at\n    end\n  end\n\n  def guard_transition(:pending, :sent, %{sale: %{address: nil}}) do\n    {:error, \"missing address\"}\n  end\n\n  def guard_transition(_from, _to, _context), do: :ok\n\n  def update_cancelled_at(%{sale: sale}) do\n    sale\n    |\u003e Sale.changeset(%{cancelled_at: DateTime.utc_now()})\n    |\u003e Repo.update()\n  end\nend\n```\n\nAdd the workflow association to the subject:\n\n```elixir\ndefmodule Sale do\n  use Ecto.Schema\n  use ExState.Ecto.Subject\n\n  import Ecto.Changeset\n\n  schema \"sales\" do\n    has_workflow SaleWorkflow\n    field :product_id, :string\n    field :cancelled_at, :utc_datetime\n  end\nend\n```\n\nAdd a `workflow_id` column to the subject table:\n\n```\nalter table(:sales) do\n  add :workflow_id, references(:workflows, type: :uuid)\nend\n```\n\n### Transitioning States\n\nUsing `ExState.transition/3`:\n\n```elixir\ndef create_sale(params) do\n  Multi.new()\n  |\u003e Multi.insert(:sale, Sale.new(params))\n  |\u003e ExState.Ecto.Multi.create(:sale)\n  |\u003e Repo.transaction()\nend\n\ndef cancel_sale(id, user_id: user_id) do\n  sale = Repo.get(Sale, id)\n\n  ExState.transition(sale, :cancel, user_id: user_id)\nend\n```\n\nUsing `ExState.Execution.transition_maybe/2`:\n\n```elixir\nsale\n|\u003e ExState.create()\n|\u003e ExState.Execution.transition_maybe(:send)\n|\u003e ExState.persist()\n```\n\nUsing `ExState.Execution.transition/2`:\n\n```elixir\n{:ok, execution} =\n  sale\n  |\u003e ExState.load()\n  |\u003e ExState.Execution.transition(:cancelled)\n\nExState.persist(execution)\n```\n\nUsing `ExState.Execution.transition!/2`:\n\n```elixir\nsale\n|\u003e ExState.load()\n|\u003e ExState.Execution.transition!(:cancelled)\n|\u003e ExState.persist()\n```\n\n### Completing Steps\n\n```elixir\ndef acknowledge_receipt(id, user_id: user_id) do\n  sale = Repo.get(Sale, id)\n\n  ExState.complete(sale, :acknowledge_receipt, user_id: user_id)\nend\n```\n\n### Running Tests\n\nSetup test database\n\n```bash\nMIX_ENV=test mix ecto.create\nmix test\n```\n\n## TODO\n\n- Extract `ex_state_core`, and other backend / db packages.\n- Multiple workflows per subject.\n- Allow configurable primary key / UUID type for usage across different\n  databases.\n- Tracking event history with metadata.\n- Add SCXML support\n- Define schema for serialization / json API usage / client consumption.\n- [Parallel states](https://xstate.js.org/docs/guides/parallel.html#parallel-state-nodes)\n- [History states](https://xstate.js.org/docs/guides/history.html#history-state-configuration)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F8thlight%2Fex_state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F8thlight%2Fex_state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F8thlight%2Fex_state/lists"}