Spacetimedbex.ClientCache (spacetimedbex v0.1.2)

Copy Markdown View Source

ETS-backed local cache of subscribed SpacetimeDB tables.

Maintains a mirror of server state by processing subscription and transaction update events from a Spacetimedbex.Connection.

Usage

{:ok, cache} = Spacetimedbex.ClientCache.start_link(
  host: "localhost:3000",
  database: "testmodule"
)

# After connection subscribes and events flow in:
Spacetimedbex.ClientCache.get_all(cache, "person")
Spacetimedbex.ClientCache.find(cache, "person", 1)
Spacetimedbex.ClientCache.count(cache, "person")

Summary

Functions

Returns a specification to start this module under a supervisor.

Count rows in a cached table.

Find a row by primary key value.

Get all rows from a cached table as a list of maps.

Process a SpacetimeDB event. Called by the connection handler to feed events into the cache.

Get the parsed schema.

Start the client cache.

Types

t()

@type t() :: %Spacetimedbex.ClientCache{
  ets_tables: %{required(String.t()) => :ets.table()},
  handler: pid() | nil,
  schema: Spacetimedbex.Schema.t() | nil
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

count(cache, table_name)

Count rows in a cached table.

find(cache, table_name, pk_value)

Find a row by primary key value.

get_all(cache, table_name)

Get all rows from a cached table as a list of maps.

handle_event(cache, event)

Process a SpacetimeDB event. Called by the connection handler to feed events into the cache.

schema(cache)

Get the parsed schema.

start_link(opts)

Start the client cache.

Options

  • :host - SpacetimeDB host (for schema fetch). Required unless :schema given.
  • :database - Database name. Required unless :schema given.
  • :schema - Optional pre-parsed %Schema{}. Skips HTTP fetch when provided.
  • :handler - Optional PID to forward {:cache_event, event} notifications.
  • :name - Optional process name.