TypeDB.Config (TypeDB v0.1.0)

Copy Markdown View Source

Validated connection configuration.

You normally never build this yourself — pass the options below to TypeDB.start_link/1 and they are validated into a %TypeDB.Config{}.

Options

  • :url — server URL, e.g. "http://localhost:8000". A bare "host:port" or "host" is accepted and defaults to http. Defaults to "http://localhost:8000".
  • :username / :password — credentials used against POST /v1/signin. Required unless :token is given.
  • :token — a pre-issued bearer token, used instead of signing in. When the token expires it cannot be renewed, so prefer credentials for long-lived connections.
  • :name — registered name of the connection process. Defaults to TypeDB.
  • :timeout — per-request receive timeout in ms. Defaults to 60_000.
  • :connect_timeout — TCP/TLS connect timeout in ms. Defaults to 10_000.
  • :http{adapter_module, adapter_opts}. Defaults to {TypeDB.HTTP.Finch, []}. See TypeDB.HTTP for the alternatives and why this is the default.
  • :max_retries — how many times to retry idempotent requests after a transport failure. Defaults to 1.
  • :max_auth_renewals — how many times a single request will renew its token and retry after a 401. Defaults to 2; more than one matters only when a burst of requests is wide enough for the freshly minted token to expire before every one of them has used it.
  • :answer_count_limit — a default cap on answers per query, applied unless the query passes its own. Unset by default. The HTTP API is not streaming and TypeDB does not cap results itself, so an unbounded match really does materialise the whole match set on the server and ship it; setting this once per connection is the cheap guard against that.
  • :retry_backoff — either {:exponential, base_ms} or a (attempt -> ms) function. Defaults to {:exponential, 100}.

Reading configuration from the environment

TypeDB.start_link(
  url: System.fetch_env!("TYPEDB_URL"),
  username: System.fetch_env!("TYPEDB_USERNAME"),
  password: System.fetch_env!("TYPEDB_PASSWORD")
)

Summary

Functions

The HTTP API version this driver speaks.

Computes the backoff delay, in milliseconds, before retry attempt (1-based).

The option keys new/1 accepts. Anything else is rejected.

Validates connection options.

Same as new/1 but raises TypeDB.Error on invalid options.

Builds the absolute URL for an unversioned API path, such as /health.

Builds the absolute URL for a versioned API path.

Types

t()

@type t() :: %TypeDB.Config{
  answer_count_limit: pos_integer() | nil,
  base_url: String.t(),
  connect_timeout: timeout(),
  http_adapter: module(),
  http_opts: keyword(),
  max_auth_renewals: non_neg_integer(),
  max_retries: non_neg_integer(),
  name: atom(),
  password: String.t() | nil,
  retry_backoff:
    {:exponential, pos_integer()} | (pos_integer() -> non_neg_integer()),
  static_token: String.t() | nil,
  timeout: timeout(),
  username: String.t() | nil
}

Functions

api_version()

@spec api_version() :: String.t()

The HTTP API version this driver speaks.

backoff(config, attempt)

@spec backoff(t(), pos_integer()) :: non_neg_integer()

Computes the backoff delay, in milliseconds, before retry attempt (1-based).

known_options()

@spec known_options() :: [atom()]

The option keys new/1 accepts. Anything else is rejected.

new(opts)

@spec new(keyword()) :: {:ok, t()} | {:error, TypeDB.Error.t()}

Validates connection options.

Returns {:ok, config} or {:error, %TypeDB.Error{kind: :config}}.

new!(opts)

@spec new!(keyword()) :: t()

Same as new/1 but raises TypeDB.Error on invalid options.

raw_url(config, path)

@spec raw_url(t(), String.t()) :: String.t()

Builds the absolute URL for an unversioned API path, such as /health.

url(config, path)

@spec url(t(), String.t()) :: String.t()

Builds the absolute URL for a versioned API path.

iex> config = TypeDB.Config.new!(url: "http://localhost:8000", token: "t")
iex> TypeDB.Config.url(config, "/databases/social")
"http://localhost:8000/v1/databases/social"