BaileysEx.Signal.Store behaviour (baileys_ex v0.1.0-alpha.7)

Copy Markdown View Source

Runtime-backed Signal key store boundary aligned with Baileys' keys contract.

The store exposes three core operations:

  • get/3 for keyed reads by logical family
  • set/2 for batched updates and deletions (nil removes an entry)
  • transaction/3 for per-key serialized work through an explicit transaction-scoped store handle

Custom store modules must pass that transaction-scoped handle into the closure:

transaction(ref, "session:alice", fn tx_ref ->
  existing = get(tx_ref, :session, ["alice.0"])
  :ok = set(tx_ref, %{session: %{"alice.0" => updated}})
  existing
end)

Concrete persistence remains swappable. The default in-memory runtime implementation can be replaced with file/ETS/DB-backed variants without changing repository consumers.

Summary

Types

Logical key families matching Baileys' Signal store.

One value written under a logical key family.

t()

Store handle passed to repository and helper modules.

Functions

Flushes the data store completely.

Extracts values from the store sequentially resolving cache items.

Examines if identical transaction processes cover the ongoing context stack.

Persists an explicitly typed data set definition back to the store.

Initializes and starts the underlying data store process/pool.

Executes work inside of a logically consistent mutex-isolated transaction.

Resolve a running store process or {module, server} tuple into a wrapped BaileysEx.Signal.Store handle.

Types

data_entries()

@type data_entries() :: %{optional(String.t()) => data_value()}

data_set()

@type data_set() :: %{
  optional(data_type()) => %{optional(String.t()) => data_value() | nil}
}

data_type()

@type data_type() ::
  :session
  | :"pre-key"
  | :"sender-key"
  | :"sender-key-memory"
  | :"app-state-sync-key"
  | :"app-state-sync-version"
  | :"lid-mapping"
  | :"device-list"
  | :tctoken
  | :"identity-key"

Logical key families matching Baileys' Signal store.

data_value()

@type data_value() ::
  binary()
  | [String.t()]
  | %{optional(String.t()) => boolean()}
  | %{:token => binary(), optional(:timestamp) => String.t()}
  | %{public: binary(), private: binary()}
  | term()

One value written under a logical key family.

start_result()

@type start_result() :: {:ok, pid()} | :ignore | {:error, term()}

t()

@type t() :: %BaileysEx.Signal.Store{module: module(), ref: term()}

Store handle passed to repository and helper modules.

Callbacks

clear(term)

@callback clear(term()) :: :ok

get(term, data_type, list)

@callback get(term(), data_type(), [String.t()]) :: data_entries()

in_transaction?(term)

@callback in_transaction?(term()) :: boolean()

set(term, data_set)

@callback set(term(), data_set()) :: :ok

start_link(keyword)

@callback start_link(keyword()) :: start_result()

transaction(term, t, function)

@callback transaction(term(), String.t(), (term() -> result)) :: result when result: var

wrap(term)

@callback wrap(term()) :: term()

Functions

clear(store)

@spec clear(t()) :: :ok

Flushes the data store completely.

get(store, type, ids)

@spec get(t(), data_type(), [String.t()]) :: data_entries()

Extracts values from the store sequentially resolving cache items.

in_transaction?(store)

@spec in_transaction?(t()) :: boolean()

Examines if identical transaction processes cover the ongoing context stack.

set(store, data)

@spec set(t(), data_set()) :: :ok

Persists an explicitly typed data set definition back to the store.

start_link(opts \\ [])

@spec start_link(keyword()) :: {:ok, t()} | :ignore | {:error, term()}

Initializes and starts the underlying data store process/pool.

transaction(store, key, fun)

@spec transaction(t(), String.t(), (t() -> result)) :: result when result: var

Executes work inside of a logically consistent mutex-isolated transaction.

The callback receives a transaction-scoped store handle. Reads and writes that should participate in the transaction must use that handle, not the outer non-transactional store handle.

wrap_running(store)

@spec wrap_running(t() | {module(), term()} | term() | nil) :: t() | nil

Resolve a running store process or {module, server} tuple into a wrapped BaileysEx.Signal.Store handle.