# `TypeDB.Connection`
[🔗](https://github.com/NoeticEcho/TypedbEx/blob/v0.11.0/typedb/lib/typedb/connection.ex#L1)

A supervised connection to a TypeDB server.

A connection process owns exactly two things: the configuration and the current
access token. It does **not** proxy requests — HTTP calls run in the caller's
process, reading configuration from a
`:read_concurrency` ETS table owned by this process. A slow query therefore
never blocks anything else.

The process is consulted only when the token has to be minted or renewed, and
it collapses concurrent renewals into a single sign-in.

## Starting

Put it in your supervision tree:

    children = [
      {TypeDB,
       url: "http://localhost:8000",
       username: "admin",
       password: System.fetch_env!("TYPEDB_PASSWORD")}
    ]

Or start several, each under its own name:

    {TypeDB, name: :analytics, url: "http://analytics:8000", ...}
    {TypeDB, name: :ingest,    url: "http://ingest:8000",    ...}

Every API function takes that name as its first argument.

## Token lifecycle

TypeDB issues expiring JWTs. This driver:

  * acquires one lazily, on the first request that needs it;
  * reads the token's own lifetime from its JWT claims and renews it
    *before* it expires, so ordinary traffic never spends a round trip
    discovering a `401`;
  * still renews reactively when a `401` arrives anyway — a clock skew, a
    revoked token, a restarted server — and retries the failed request;
  * collapses concurrent renewals: whoever reaches the connection first signs
    in, and everyone queued behind takes that token.

`:max_auth_renewals` bounds how many times a single request will go around the
renew-and-retry loop before surfacing the error.

When a connection is configured with a pre-issued `:token` there is nothing to
renew, and expiry surfaces as `%TypeDB.Error{kind: :unauthenticated}`.

# `request_opts`

```elixir
@type request_opts() :: keyword()
```

Options for `request/4`:

  * `:body` — term to encode as the JSON request body
  * `:expect` — `:json` (default), `:text` or `:empty`
  * `:versioned` — prefix the path with the API version. Defaults to `true`
  * `:authenticated` — send a bearer token. Defaults to `true`
  * `:idempotent` — allow retries. Defaults to `true` for `:get` and
    `:delete`, but the method is a poor proxy for the question and every
    caller in this driver answers it for itself: a read query is safe to
    re-send and a commit is not, though both are `POST`
  * `:timeout` — overrides the connection timeout for this request
  * `:deadline` — overrides the connection's wall-clock budget for the whole
    call, retries included
  * `:metadata` — a map merged into the telemetry metadata of this call, for
    what the path cannot say: the database of a `/query` is in its body

# `t`

```elixir
@type t() :: atom()
```

A connection: the registered name of a `TypeDB.Connection` process.

# `config`

```elixir
@spec config(t()) :: TypeDB.Config.t()
```

Returns the validated configuration of a running connection.

# `request`

```elixir
@spec request(t(), TypeDB.HTTP.method(), String.t(), request_opts()) ::
  {:ok, term()} | {:error, TypeDB.Error.t()}
```

Performs an authenticated request against the TypeDB HTTP API.

This is the escape hatch used by every other module in this library. Reach for
it directly only to call an endpoint the driver does not wrap yet.

    TypeDB.Connection.request(conn, :get, "/databases")

# `running?`

```elixir
@spec running?(t()) :: boolean()
```

Returns `true` when `conn` can serve a request.

Every other function in this driver raises `%TypeDB.Error{kind: :config}`
against a connection that is not running — deliberately, since the name is
nearly always a typo or a missing child spec. This is for the callers who
cannot let that happen: a health endpoint, a supervisor deciding whether to
start work, a module that maps driver failures onto its own error type and
has nowhere to put an exception.

**`Process.whereis/1` is not the same question.** `GenServer.start_link/3`
registers the name *before* `init/1` runs, so a pid exists under the name
before the connection can do anything with it — and a call in that window
raises. This checks what a request actually needs.

    if TypeDB.running?(conn), do: TypeDB.query(conn, "social", query)

It answers about *this node's connection process*, not about TypeDB: a
connection whose server is unreachable is still running. `TypeDB.Server.health/2`
is the question about the server, and it needs a running connection to ask.

Inherently racy, as any such predicate is — the connection can stop between
this call and the next one. It narrows the window; it does not remove it.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts a connection. See `TypeDB.Config` for the supported options.

# `stop`

```elixir
@spec stop(t() | pid(), term(), timeout()) :: :ok
```

Stops a connection.

Accepts the registered name or the pid — unlike every other function here,
which needs the name because it reads the connection's ETS table.

# `token`

```elixir
@spec token(t()) :: {:ok, String.t()} | {:error, TypeDB.Error.t()}
```

Returns a token that is not about to expire, minting one if needed.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
