Supabase.Client (supabase_potion v0.6.1)

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

Usage

Generally, you can start a client by calling 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{}}

That way of initialisation is useful when you want to manage the client state by yourself or create one off clients.

However, starting a client directly means you have to manage the client state by yourself. To make it easier, you can use the Supabase.Client module to manage the connection options for you, which we call a "self managed client".

To achieve this you can use the Supabase.Client module in your module:

defmodule MyApp.Supabase.Client do
  use Supabase.Client, otp_app: :my_app
end

This will automatically start an Agent process to manage the state for you. But for that to work, you need to configure your Supabase client options in your application configuration, either in compile-time (config.exs) or runtime (runtime.exs):

# config/runtime.exs or config/config.exs

config :my_app, MyApp.Supabase.Client,
  base_url: "https://<app-name>.supabase.co",
  api_key: "<supabase-api-key>",
  # any additional options
  access_token: "<supabase-access-token>",
  db: [schema: "another"],
  auth: [debug: true] # optional

Another alternative would be to configure your Supabase Client in code, while starting your application:

defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    children = [
      {MyApp.Supabase.Client, [
        base_url: "https://<app-name>.supabase.co",
        api_key: "<supabase-api-key>"
      ]}
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supabase.Client.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

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

Examples

%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"
  }
}

Summary

Types

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

t()

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

Functions

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

Types

options()

@type options() :: %{
  optional(:db) => Supabase.Client.Db.params(),
  optional(:global) => Supabase.Client.Global.params(),
  optional(:auth) => Supabase.Client.Auth.params()
}

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

Note that these options can be passed to Supabase.init_client/3 as Enumerable, which means it can be either a Keyword.t() or a Map.t(), but internally it will be passed as a map.

t()

@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_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

Functions

changeset(attrs)

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

update_access_token(client, access_token)

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

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

Note that this functions shoudln't be used directly if you are using a self managed client (aka started it into your supervision tree as the Supabase.Client moduledoc says), since it will return the updated client but it won't update the inner client in the Agent process.

To update the access token for a self managed client, you can use the set_auth/2 function that is generated when you configure your client module.

If you're managing your own Supabase client state (aka one off clients) you can use this helper function.