# `HephaestusEcto.Serializer`
[🔗](https://github.com/lucas-stellet/hephaestus_ecto/blob/v0.3.0/lib/hephaestus_ecto/serializer.ex#L1)

Converts between `Hephaestus.Core.Instance` structs and database-safe values.

Handles the type conversions required to persist Elixir-specific types
(atoms, MapSets, module references, DateTimes) into PostgreSQL JSONB.

All deserialization uses `String.to_existing_atom/1` to prevent
arbitrary atom creation from database values.

The serialized form is a 5-tuple so the database layer can persist `workflow_version`
separately from the JSONB `state` payload:

    {id, workflow_string, status_string, workflow_version, state_map}

# `from_db`

```elixir
@spec from_db(String.t(), String.t(), String.t(), pos_integer(), map()) ::
  Hephaestus.Core.Instance.t()
```

Reconstructs an `Instance` struct from database values.

Converts string module names back to atoms (via `String.to_existing_atom/1`),
sorted lists back to MapSets, and ISO 8601 timestamps back to `DateTime`.

This is the inverse of `to_db/1` and expects the same 5 arguments returned there:

    HephaestusEcto.Serializer.from_db(id, workflow, status, workflow_version, state)

# `to_db`

```elixir
@spec to_db(Hephaestus.Core.Instance.t()) ::
  {String.t(), String.t(), String.t(), pos_integer(), map()}
```

Serializes an `Instance` struct into a tuple of database-safe values.

Returns `{id, workflow_string, status_string, workflow_version, state_map}` where
`state_map` contains the serialized context, step configs, active/completed steps,
and execution history.

This tuple is intended for `HephaestusEcto.Storage.put/2`, which stores
`workflow_version` in its own column and the remaining state in JSONB.

---

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