Skip to content

Commit

Permalink
allow to use custom client
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Apr 21, 2024
1 parent 4ab3b06 commit 12ec1a1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export SUPABASE_URL=http://127.0.0.1:54321
export SUPABASE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU
7 changes: 7 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Config

if config_env() == :dev do
config :supabase_potion,
supabase_base_url: System.get_env("SUPABASE_URL"),
supabase_api_key: System.get_env("SUPABASE_KEY")
end
88 changes: 88 additions & 0 deletions lib/supabase/postgrest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -942,4 +942,92 @@ defmodule Supabase.PostgREST do
defp success_resp?(status) do
Kernel.in(status, 200..399)
end

defmacrop wrap_postgrest_functions do
quote unquote: false, bind_quoted: [module: __MODULE__] do
for {fun, arity} <- module.__info__(:functions) do
cond do
fun == :from ->
quote do
@doc """
Check `Supabase.PostgREST.#{unquote(fun)}/#{unquote(arity)}`
"""
def unquote(fun)(schema) do
apply(unquote(module), unquote(fun), [@client, schema])
end
end

arity == 1 ->
quote do
@doc """
Check `Supabase.PostgREST.#{unquote(fun)}/#{unquote(arity)}`
"""
def unquote(fun)(data) do
apply(unquote(module), unquote(fun), [data])
end
end

true ->
args = for idx <- 1..arity, do: Macro.var(:"arg#{idx}", module)

quote do
@doc """
Check `Supabase.PostgREST.#{unquote(fun)}/#{unquote(arity)}`
"""
def unquote(fun)(unquote_splicing(args)) do
args = [unquote_splicing(args)]
apply(unquote(module), unquote(fun), args)
end
end
end
end
end
end

defmacro __using__([{:client, client} | opts]) do
config = Macro.escape(Keyword.get(opts, :config, %{}))

quote location: :keep do
@client unquote(client)

def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]},
type: :supervisor
}
end

def start_link(opts \\ []) do
manage_clients? = Application.get_env(:supabase_potion, :manage_clients, true)

if manage_clients? do
Supabase.init_client(unquote(client), unquote(config))
else
base_url =
Application.get_env(:supabase_potion, :supabase_base_url) ||
raise Supabase.MissingSupabaseConfig, :url

api_key =
Application.get_env(:supabase_potion, :supabase_api_key) ||
raise Supabase.MissingSupabaseConfig, :key

config =
unquote(config)
|> Map.put(:conn, %{base_url: base_url, api_key: api_key})
|> Map.put(:name, unquote(client))

opts = [name: unquote(client), client_info: config]
Supabase.Client.start_link(opts)
end
|> then(fn
{:ok, pid} -> {:ok, pid}
{:error, {:already_started, pid}} -> {:ok, pid}
err -> err
end)
end

unquote(wrap_postgrest_functions())
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule PostgREST.MixProject do
def project do
[
app: :supabase_postgrest,
version: "0.1.0",
version: "0.1.1",
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"castore": {:hex, :castore, "1.0.6", "ffc42f110ebfdafab0ea159cd43d31365fa0af0ce4a02ecebf1707ae619ee727", [:mix], [], "hexpm", "374c6e7ca752296be3d6780a6d5b922854ffcc74123da90f2f328996b962d33a"},
"castore": {:hex, :castore, "1.0.7", "b651241514e5f6956028147fe6637f7ac13802537e895a724f90bf3e36ddd1dd", [:mix], [], "hexpm", "da7785a4b0d2a021cd1292a60875a784b6caef71e76bf4917bdee1f390455cf5"},
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
"ecto": {:hex, :ecto, "3.11.2", "e1d26be989db350a633667c5cda9c3d115ae779b66da567c68c80cfb26a8c9ee", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3c38bca2c6f8d8023f2145326cc8a80100c3ffe4dcbd9842ff867f7fc6156c65"},
Expand All @@ -15,6 +15,6 @@
"nimble_options": {:hex, :nimble_options, "1.1.0", "3b31a57ede9cb1502071fade751ab0c7b8dbe75a9a4c2b5bbb0943a690b63172", [:mix], [], "hexpm", "8bbbb3941af3ca9acc7835f5655ea062111c9c27bcac53e004460dfd19008a99"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
"supabase_potion": {:hex, :supabase_potion, "0.3.1", "2cf8561eeeaddb1bbfe4d9d9b7cad28c9162efefa4cdfb4ec6ab633030d47bc1", [:mix], [{:ecto, "~> 3.10", [hex: :ecto, repo: "hexpm", optional: false]}, {:ex_doc, ">= 0.0.0", [hex: :ex_doc, repo: "hexpm", optional: false]}, {:finch, "~> 0.16", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "a3342793167a18b8987ce1313d52ad96a2d803c24d6e9470da8688d35f78bae0"},
"supabase_potion": {:hex, :supabase_potion, "0.3.4", "0e9292bd27cc475b7209ba6f01b80b02d7876bb9af271c9c84444ab9078576c5", [:mix], [{:ecto, "~> 3.10", [hex: :ecto, repo: "hexpm", optional: false]}, {:ex_doc, ">= 0.0.0", [hex: :ex_doc, repo: "hexpm", optional: false]}, {:finch, "~> 0.16", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "669c9f443be3129b2eafcafcf7a62408cb3bdb4e5a1b645bb5827f2a5de1a57e"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
}

0 comments on commit 12ec1a1

Please sign in to comment.