Skip to content

Commit

Permalink
more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Apr 21, 2024
1 parent cc0ce25 commit 5f26627
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 19 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ result
# Nix files
result

/.elixir_ls/
/.elixir-tools/
.elixir_ls/
.elixir-tools/
.lexical/
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,90 @@ config :supabase,

Make sure to set the environment variables `SUPABASE_BASE_URL` and `SUPABASE_API_KEY`.

## Manually starting Clients

If you want to manually manage Clients, firstly you need to disable the config:

```elixir
import Config

config :supabase_potion, manage_clients: false
```

Then you need to start the Clients managements processes, that consists on:
- `Supabase.ClientSupervisor`: a DynamicSupervisor to manage multiple Supabase clients
- `Supabase.ClientRegistry`: a Registry to easily find a Supabase client

You can start them on your application startup, often on your `MyApp.Application`:

```elixir
defmodule MyApp.Application do
use Application

@impl true
def start(_type, _args) do
children = [
# rest of your Supervisor's children...
# is recommended to start registry **before**
# the Supervsior
Supabase.ClientRegistry
Supabase.ClientSupervisor
]
end

# rest of module...
end
```

Now you can start to initialize Clients! as pointed on [Staring a Client](#starting-a-client) section.

However, maybe you can full control of your Supabase Clients, then, you can start Clients individually on your application startup, like:

```elixir
defmodule MyApp.Application do
use Application

@impl true
def start(_type, _args) do
supabase_config = supabase_client_config()
supabase_opts = [name: MyApp.SupabaseClient, client_info: supabase_config]

children = [
# rest of your Supervisor's children...
{Supabase.Client, supabase_opts}
]
end

defp supabase_client_config do
%{
# you can skip the `conn` config if you already set on config.exs
conn: %{
api_key: "my-super-secret-api-key",
base_url: "https://my-project-id.supabase.co",
access_token: "my-scoped-access-token",
},
db: %{schema: "my-schema"} # default to "public",
# global headers to be used on Supabase API requests
global: %{headers: %{"content-type": "application/json"}},
auth: %{
# below are the defaults values
auto_refresh_token: true,
debug: false,
detect_session_in_url: true,
flow_type: :implicit,
persist_session: true,
storage: "my-storage",
storage_key: "my-storage-key"
}
}
end

# rest of module...
end
```

#### TODO

### How to find my Supabase base URL?

You can find your Supabase base URL in the Settings page of your project.
Expand Down
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Config

if config_env() == :dev do
config :supabase_potion,
manage_clients: true,
supabase_base_url: System.get_env("SUPABASE_URL"),
supabase_api_key: System.get_env("SUPABASE_KEY")
end
12 changes: 6 additions & 6 deletions lib/supabase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Supabase do
def deps do
[
{:supabase_potion, "~> 0.1"}
{:supabase_potion, "~> 0.3"}
]
end
Expand Down Expand Up @@ -110,13 +110,13 @@ defmodule Supabase do

@typep changeset :: Ecto.Changeset.t()

@spec init_client(params) :: {:ok, pid} | {:error, changeset}
@spec init_client(name :: atom, params) :: {:ok, pid} | {:error, changeset}
when params: Client.params()
def init_client(%{} = opts) do
def init_client(name, opts \\ %{}) do
conn = Map.get(opts, :conn, %{})
opts = maybe_merge_config_from_application(conn, opts)

with {:ok, opts} <- Client.parse(opts) do
with {:ok, opts} <- Client.parse(Map.put(opts, :name, name)) do
name = ClientRegistry.named(opts.name)
client_opts = [name: name, client_info: opts]
ClientSupervisor.start_child({Client, client_opts})
Expand All @@ -125,11 +125,11 @@ defmodule Supabase do
_ -> Client.parse(opts)
end

def init_client!(%{} = opts) do
def init_client!(name, %{} = opts \\ %{}) do
conn = Map.get(opts, :conn, %{})
opts = maybe_merge_config_from_application(conn, opts)

case init_client(opts) do
case init_client(name, opts) do
{:ok, pid} -> pid
{:error, changeset} -> raise Ecto.InvalidChangesetError, changeset: changeset, action: :init
end
Expand Down
9 changes: 2 additions & 7 deletions lib/supabase/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ defmodule Supabase.Application do

@impl true
def start(_start_type, _args) do
children = [
{Finch, @finch_opts},
if(manage_clients?(), do: Supabase.ClientSupervisor),
if(manage_clients?(), do: Supabase.ClientRegistry)
]

children = [{Finch, @finch_opts}, (if manage_clients?(), do: Supabase.Supervisor)]
opts = [strategy: :one_for_one, name: Supabase.Supervisor]

children
Expand All @@ -21,6 +16,6 @@ defmodule Supabase.Application do
end

defp manage_clients? do
Application.get_env(:supabase, :manage_clients, true)
Application.get_env(:supabase_potion, :manage_clients, true)
end
end
8 changes: 5 additions & 3 deletions lib/supabase/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ defmodule Supabase.Client do
}

@type params :: %{
name: atom,
conn: Conn.params(),
db: Db.params(),
global: Global.params(),
Expand Down Expand Up @@ -165,8 +164,11 @@ defmodule Supabase.Client do
when name: atom | pid
def retrieve_client(source) do
if is_atom(source) do
pid = ClientRegistry.lookup(source)
{:ok, Agent.get(pid, & &1)}
if pid = ClientRegistry.lookup(source) do
{:ok, Agent.get(pid, & &1)}
else
{:ok, Agent.get(source, & &1)}
end
else
{:ok, Agent.get(source, & &1)}
end
Expand Down
18 changes: 18 additions & 0 deletions lib/supabase/supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Supabase.Supervisor do
use Supervisor

def start_link(opts \\ []) do
Supervisor.start_link(__MODULE__, opts)
end

@impl true
def init(opts) do
name = Keyword.get(opts, :name, Supabase.Supervisor)
strategy = Keyword.get(opts, :strategy, :one_for_one)
opts = [name: name, strategy: strategy]
children = [Supabase.ClientRegistry, Supabase.ClientSupervisor]

Supervisor.init(children, opts)
end

end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Supabase.Potion.MixProject do
use Mix.Project

@version "0.3.2"
@version "0.3.4"
@source_url "https://github.com/zoedsoupe/supabase"

def project do
Expand Down

0 comments on commit 5f26627

Please sign in to comment.