Spacetimedbex.Client behaviour (spacetimedbex v0.1.2)

Copy Markdown View Source

High-level SpacetimeDB client that ties Connection, ClientCache, and Schema together.

Usage

defmodule MyApp.SpaceClient do
  use Spacetimedbex.Client

  def config do
    %{
      host: "localhost:3000",
      database: "my_db",
      subscriptions: ["SELECT * FROM users", "SELECT * FROM messages"]
    }
  end

  def on_connect(identity, _conn_id, token, state) do
    IO.puts("Connected with identity: #{inspect(identity)}")
    {:ok, Map.put(state, :token, token)}
  end

  def on_insert("users", row, state) do
    IO.puts("New user: #{inspect(row)}")
    {:ok, state}
  end
end

# Start it
{:ok, pid} = Spacetimedbex.Client.start_link(MyApp.SpaceClient, %{})

# Call a reducer
Spacetimedbex.Client.call_reducer(pid, "create_user", %{"name" => "Alice", "age" => 30})

# Query the cache
Spacetimedbex.Client.get_all(pid, "users")

Callbacks

All callbacks are optional except config/0.

  • config() — returns connection configuration map
  • on_connect(identity, connection_id, token, state) — called on initial connection
  • on_subscribe_applied(table_name, rows, state) — called per table when subscription data arrives
  • on_insert(table_name, row, state) — called per inserted row
  • on_delete(table_name, row, state) — called per deleted row
  • on_update(table_name, old_row, new_row, state) — called when a row with the same PK is deleted then inserted (row replacement)
  • on_transaction(changes, state) — called with full transaction; return {:ok, state, :skip_row_callbacks} to suppress per-row callbacks
  • on_reducer_result(request_id, result, state) — called when a reducer completes
  • on_unsubscribe_applied(query_set_id, rows, state) — called when an unsubscribe completes
  • on_query_result(request_id, result, state) — called with one-off query results
  • on_disconnect(reason, state) — called on disconnection

Summary

Functions

Call a reducer with a map of arguments. Auto-encodes via schema.

Call a reducer with pre-encoded BSATN binary arguments.

Returns a specification to start this module under a supervisor.

Count rows in a cached table.

Find a row by primary key.

Get all rows from a cached table.

Execute a one-off SQL query via WebSocket.

Get the cached schema.

Start a Client GenServer.

Unsubscribe from a query set by ID. Options: :send_dropped_rows.

Types

changes()

@type changes() :: [%{table_name: String.t(), inserts: [map()], deletes: [map()]}]

state()

@type state() :: term()

Callbacks

config()

@callback config() :: map()

on_connect(identity, connection_id, token, state)

(optional)
@callback on_connect(
  identity :: binary(),
  connection_id :: binary(),
  token :: String.t(),
  state()
) :: {:ok, state()}

on_delete(table_name, row, state)

(optional)
@callback on_delete(table_name :: String.t(), row :: map(), state()) :: {:ok, state()}

on_disconnect(reason, state)

(optional)
@callback on_disconnect(reason :: term(), state()) :: {:ok, state()}

on_insert(table_name, row, state)

(optional)
@callback on_insert(table_name :: String.t(), row :: map(), state()) :: {:ok, state()}

on_query_result(request_id, result, state)

(optional)
@callback on_query_result(request_id :: non_neg_integer(), result :: term(), state()) ::
  {:ok, state()}

on_reducer_result(request_id, result, state)

(optional)
@callback on_reducer_result(request_id :: non_neg_integer(), result :: term(), state()) ::
  {:ok, state()}

on_subscribe_applied(table_name, rows, state)

(optional)
@callback on_subscribe_applied(table_name :: String.t(), rows :: [map()], state()) ::
  {:ok, state()}

on_transaction(changes, state)

(optional)
@callback on_transaction(changes(), state()) ::
  {:ok, state()} | {:ok, state(), :skip_row_callbacks}

on_unsubscribe_applied(query_set_id, rows, state)

(optional)
@callback on_unsubscribe_applied(
  query_set_id :: non_neg_integer(),
  rows :: [map()],
  state()
) ::
  {:ok, state()}

on_update(table_name, old_row, new_row, state)

(optional)
@callback on_update(table_name :: String.t(), old_row :: map(), new_row :: map(), state()) ::
  {:ok, state()}

Functions

call_reducer(pid, reducer_name, args_map \\ %{})

Call a reducer with a map of arguments. Auto-encodes via schema.

call_reducer_raw(pid, reducer_name, bsatn_binary)

Call a reducer with pre-encoded BSATN binary arguments.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

count(pid, table_name)

Count rows in a cached table.

find(pid, table_name, pk_value)

Find a row by primary key.

get_all(pid, table_name)

Get all rows from a cached table.

query(pid, query_string)

Execute a one-off SQL query via WebSocket.

schema(pid)

Get the cached schema.

start_link(module, init_state, opts \\ [])

Start a Client GenServer.

Parameters

  • module — callback module that use Spacetimedbex.Client
  • init_state — initial user state passed to callbacks
  • opts — GenServer options (e.g. :name)

unsubscribe(pid, query_set_id, opts \\ [])

Unsubscribe from a query set by ID. Options: :send_dropped_rows.