telega/storage

Unified key-value storage contract shared by sessions and flows.

KeyValueStorage is the single low-level contract that every backend implements (ETS in core; Postgres/SQLite/Redis as separate packages). Values are opaque Strings — callers serialize to/from JSON themselves.

The two bridges below derive the higher-level SessionSettings and FlowStorage contracts from a single KeyValueStorage, so a bot only needs to wire up one backend for both sessions and flows.

Types

Backend-agnostic key-value store.

  • get returns None for a missing key.
  • set stores a value with no expiration.
  • set_with_ttl stores a value that expires after ttl_ms milliseconds. Backends without native TTL emulate it with lazy expiration on access.
  • scan returns every key beginning with the given prefix (live keys only).
pub type KeyValueStorage(error) {
  KeyValueStorage(
    get: fn(String) -> Result(option.Option(String), error),
    set: fn(String, String) -> Result(Nil, error),
    set_with_ttl: fn(String, String, Int) -> Result(Nil, error),
    delete: fn(String) -> Result(Nil, error),
    scan: fn(String) -> Result(List(String), error),
  )
}

Constructors

  • KeyValueStorage(
      get: fn(String) -> Result(option.Option(String), error),
      set: fn(String, String) -> Result(Nil, error),
      set_with_ttl: fn(String, String, Int) -> Result(Nil, error),
      delete: fn(String) -> Result(Nil, error),
      scan: fn(String) -> Result(List(String), error),
    )

Values

pub fn flow_storage_from_storage(
  storage storage: KeyValueStorage(error),
) -> types.FlowStorage(error)

Derive FlowStorage from a KeyValueStorage.

Flow instances are stored under the flow: key namespace as complete JSON (see instance.to_json), so subflows and parallel state survive restarts. list_by_user is served by scan over the namespace, replacing the secondary index used by the legacy ETS-only implementation.

pub fn session_settings_from_storage(
  storage storage: KeyValueStorage(error),
  encode encode: fn(session) -> json.Json,
  decode decoder: decode.Decoder(session),
  default default: fn() -> session,
) -> bot.SessionSettings(session, error)

Derive SessionSettings from a KeyValueStorage.

Sessions are stored under the session: key namespace as JSON produced by encode. A decode failure on load is treated as “no session” so the bot falls back to default instead of crashing on a corrupt or migrated value.

Search Document