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

Built-in disk-durable `Finitomata.Persistency` adapter backed by a `:dets` file.

It behaves exactly like `Finitomata.Persistency.ETS` (one `{state, payload}` snapshot
  per FSM, keyed by the FSM name) but the table is persisted to disk, so snapshots
  survive a full node restart. Both adapters are OTP built-ins and add no dependencies.

> #### Single node {: .info}
>
> `:dets` is a single-node, single-file store with a 2GB file-size limit. For
> multi-node or large-scale persistence, back the FSM with a database via a custom
> `Finitomata.Persistency` implementation (for example through
> `Finitomata.Persistency.Persistable`).

## Usage

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

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

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

## Configuration

The `:dets` file path (and optionally the table name) are configurable. The default
  path lives under `System.tmp_dir!/0`; set a stable location for production:

```elixir
config :finitomata, Finitomata.Persistency.DETS,
  file: "/var/lib/my_app/finitomata.dets",
  table: :my_fsm_snapshots
```

# `file`

```elixir
@spec file() :: String.t()
```

The `:dets` file path in use (configurable via `config :finitomata, Finitomata.Persistency.DETS`)

# `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 file; add it to your supervision tree

# `sync`

```elixir
@spec sync() :: :ok | {:error, term()}
```

Flushes pending writes to disk

# `table`

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

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

---

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