# `Finitomata.Persistency.ETS`
[🔗](https://github.com/am-kantox/finitomata/blob/v0.41.0/lib/finitomata/persistency/ets.ex#L1)

Built-in in-memory `Finitomata.Persistency` adapter backed by a named ETS table.

It stores one `{state, payload}` snapshot per FSM (keyed by the FSM name) and reloads
  it when an FSM with the same name starts again. Because the table is owned by a
  long-lived process rather than by the FSM itself, snapshots survive the transient
  restart of an individual FSM (for example after a crash), which makes this adapter a
  zero-boilerplate fit for development, tests, and single-node deployments that only
  need durability for the lifetime of the node.

> #### In-memory only {: .warning}
>
> Snapshots are kept in ETS and are lost when the node stops. Use
> `Finitomata.Persistency.DETS` for durability across node restarts, or a custom
> `Finitomata.Persistency` implementation (for example via
> `Finitomata.Persistency.Persistable`) to target an external database.

## Usage

Add the table owner to your supervision tree and point `use Finitomata` at this module:

```elixir
children = [
  Finitomata.Persistency.ETS,
  {Finitomata.Supervisor, id: MyApp.Fsm}
]

defmodule MyApp.Order do
  use Finitomata, fsm: @fsm, persistency: Finitomata.Persistency.ETS
end
```

Start FSMs whose name equals the entity id so that the `store` and `load` keys line up:

```elixir
Finitomata.start_fsm(MyApp.Fsm, MyApp.Order, order_id, %MyApp.Order{id: order_id})
```

## Configuration

The ETS table name defaults to `Finitomata.Persistency.ETS` and can be overridden:

```elixir
config :finitomata, Finitomata.Persistency.ETS, table: :my_fsm_snapshots
```

# `get`

```elixir
@spec get(Finitomata.fsm_name()) :: Finitomata.Persistency.snapshot() | nil
```

Reads the snapshot stored for `name`, or `nil`

# `last_error`

```elixir
@spec last_error(Finitomata.fsm_name()) :: Finitomata.Persistency.error() | nil
```

Reads the last persisted failure for `name`, or `nil`

# `purge`

```elixir
@spec purge(Finitomata.fsm_name()) :: :ok
```

Removes the snapshot and the last error stored for `name`

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts the process owning the snapshot table; add it to your supervision tree

# `table`

```elixir
@spec table() :: atom()
```

The ETS table name in use (configurable via `config :finitomata, Finitomata.Persistency.ETS`)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
