# `PhoenixKit.Modules.Sync.SessionStore`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/sync/session_store.ex#L1)

ETS-based session storage for DB Sync module.

Stores sync sessions in ETS for fast, ephemeral access.
Sessions remain valid as long as the owning LiveView process is alive.
When the LiveView terminates (page closed), the session is automatically deleted.

## Architecture

This module uses a GenServer to manage an ETS table. The ETS table
provides fast reads while the GenServer handles process monitoring
and automatic cleanup when LiveView processes terminate.

## Future Migration Path

This module is designed to be easily replaced with database persistence
if audit logging or sync history is needed. The public API would remain
the same, only the storage backend would change.

## Session Structure

    %{
      code: "A7X9K2M4",
      direction: :send | :receive,
      status: :pending | :connected | :completed | :failed,
      owner_pid: #PID<0.123.0>,     # Session is deleted when this process dies
      created_at: ~U[2025-12-16 12:15:00Z],
      connected_at: nil | ~U[...],
      sender_info: nil | %{...},
      receiver_info: nil | %{...}
    }

# `child_spec`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/sync/session_store.ex#L35)

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `count_active`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/sync/session_store.ex#L138)

```elixir
@spec count_active() :: non_neg_integer()
```

Counts active sessions.

All sessions in the store are active (sessions are deleted when owner process terminates).

# `create`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/sync/session_store.ex#L67)

```elixir
@spec create(map()) :: :ok | {:error, :already_exists}
```

Creates a new session in the store and monitors the owner process.

## Parameters

- `session` - Map with at least `code` and `owner_pid` fields

## Returns

- `:ok` on success
- `{:error, :already_exists}` if code already exists

# `delete`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/sync/session_store.ex#L127)

```elixir
@spec delete(String.t()) :: :ok
```

Deletes a session by code.

# `get`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/sync/session_store.ex#L96)

```elixir
@spec get(String.t()) :: {:ok, map()} | {:error, :not_found}
```

Gets a session by its code.

## Returns

- `{:ok, session}` if found
- `{:error, :not_found}` if not found

# `list_active`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/sync/session_store.ex#L149)

```elixir
@spec list_active() :: [map()]
```

Lists all active sessions.
Useful for debugging and admin interfaces.

All sessions in the store are active (sessions are deleted when owner process terminates).

# `start_link`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/sync/session_store.ex#L50)

Starts the SessionStore GenServer.

# `update`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/sync/session_store.ex#L112)

```elixir
@spec update(String.t(), map()) :: :ok | {:error, :not_found}
```

Updates an existing session.

## Returns

- `:ok` on success
- `{:error, :not_found}` if session doesn't exist

---

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