# `Supabase.Client`

A client for interacting with Supabase. This module is responsible for
managing the connection options for your Supabase project.

## Usage

There are two ways to create a Supabase client:

### 1. Module-based Client (Recommended)

Define a client module using the macro (similar to Ecto Repo). This approach
reads configuration from your application config and builds a fresh client
struct on each call:

    # lib/my_app/supabase.ex
    defmodule MyApp.Supabase do
      use Supabase.Client, otp_app: :my_app
    end

    # config/config.exs
    config :my_app, MyApp.Supabase,
      base_url: "https://<app-name>.supabase.io",
      api_key: "<supabase-api-key>",
      db: [schema: "public"],
      auth: [flow_type: :pkce]

    # Usage
    iex> client = MyApp.Supabase.get_client!()
    iex> %Supabase.Client{}

### 2. Direct Initialization

Alternatively, create a client directly using `Supabase.init_client/3`:

    iex> base_url = "https://<app-name>.supabase.io"
    iex> api_key = "<supabase-api-key>"
    iex> Supabase.init_client(base_url, api_key, %{})
    {:ok, %Supabase.Client{}}

For more information on how to configure your Supabase Client with additional
options, please refer to the `Supabase.Client.t()` typespec.

## Client Structure

    %Supabase.Client{
      base_url: "https://<app-name>.supabase.io",
      api_key: "<supabase-api-key>",
      access_token: "<supabase-access-token>",
      db: %Supabase.Client.Db{
        schema: "public"
      },
      global: %Supabase.Client.Global{
        headers: %{}
      },
      auth: %Supabase.Client.Auth{
        auto_refresh_token: true,
        debug: false,
        detect_session_in_url: true,
        flow_type: :implicit,
        persist_session: true,
        storage_key: "sb-<host>-auth-token"
      },
      storage: %Supabase.Client.Storage{
        use_new_hostname: false
      }
    }

# `options`

```elixir
@type options() :: %{
  optional(:db) =&gt; Supabase.Client.Db.params(),
  optional(:global) =&gt; Supabase.Client.Global.params(),
  optional(:auth) =&gt; Supabase.Client.Auth.params(),
  optional(:storage) =&gt; Supabase.Client.Storage.params()
}
```

The type for the available additional options that can be passed
to `Supabase.init_client/3` to configure the Supabase client.

# `t`

```elixir
@type t() :: %Supabase.Client{
  access_token: String.t(),
  api_key: String.t(),
  auth: Supabase.Client.Auth.t(),
  auth_url: String.t(),
  base_url: String.t(),
  database_url: String.t(),
  db: Supabase.Client.Db.t(),
  functions_url: String.t(),
  global: Supabase.Client.Global.t(),
  realtime_url: String.t(),
  storage: Supabase.Client.Storage.t(),
  storage_url: String.t()
}
```

The type of the `Supabase.Client` that will be returned from `Supabase.init_client/3`.

## Source
https://supabase.com/docs/reference/javascript/initializing

# `changeset`

```elixir
@spec changeset(attrs :: map()) :: Ecto.Changeset.t()
```

# `update_access_token`

```elixir
@spec update_access_token(t(), String.t()) :: t()
```

Helper function to swap the current acccess token being used in
the Supabase client instance.

---

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