Skip to content

Commit

Permalink
Simplify the builder structure (#7)
Browse files Browse the repository at this point in the history
* refactor: simplify builder\n\nfix some documentations, remove unecessary functions

* split in submodules

* format
  • Loading branch information
zoedsoupe authored Dec 30, 2024
1 parent f048c64 commit 646fab7
Show file tree
Hide file tree
Showing 11 changed files with 1,131 additions and 1,067 deletions.
936 changes: 80 additions & 856 deletions lib/supabase/postgrest.ex

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions lib/supabase/postgrest/behaviour.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Supabase.PostgREST.Behaviour do
@moduledoc "Defines the interface for the main module Supabase.PostgREST"

alias Supabase.Client
alias Supabase.PostgREST.Builder
alias Supabase.PostgREST.Error

@type media_type ::
:json | :csv | :openapi | :geojson | :pgrst_plan | :pgrst_object | :pgrst_array

@callback with_custom_media_type(builder, media_type) :: builder
when builder: Builder.t() | Builder.t()
@callback from(Client.t(), relation :: String.t()) :: Builder.t()
@callback schema(Builder.t(), schema :: String.t()) :: Builder.t()

@callback execute(Builder.t() | Builder.t()) :: {:ok, term} | {:error, Error.t()}
@callback execute_string(Builder.t() | Builder.t()) ::
{:ok, binary} | {:error, Error.t() | atom}
@callback execute_to(Builder.t() | Builder.t(), atom) ::
{:ok, term} | {:error, Error.t() | atom}
@callback execute_to_finch_request(Builder.t() | Builder.t()) ::
Finch.Request.t()
end
79 changes: 79 additions & 0 deletions lib/supabase/postgrest/builder.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
defmodule Supabase.PostgREST.Builder do
@moduledoc """
Defines a struct to centralize and accumulate data to
be sent to the PostgREST server, parsed.
"""

alias Supabase.Client

defstruct ~w[method body url headers params client schema]a

@type t :: %__MODULE__{
url: String.t(),
method: :get | :post | :put | :patch | :delete,
params: %{String.t() => String.t()},
headers: %{String.t() => String.t()},
body: map,
client: Supabase.Client.t(),
schema: String.t()
}

defp get_version do
:supabase_postgrest
|> :application.get_key(:vsn)
|> elem(1)
|> List.to_string()
end

@doc "Creates a new `#{__MODULE__}` instance"
def new(%Client{} = client, relation: relation) do
%__MODULE__{
schema: client.db.schema,
method: :get,
params: %{},
url:
URI.parse(client.conn.base_url)
|> URI.append_path("/rest/v1")
|> URI.append_path("/" <> relation),
headers: %{
"x-client-info" => "postgrest-ex/#{get_version()}",
"accept-profile" => client.db.schema,
"content-profile" => client.db.schema,
"content-type" => "application/json"
}
}
end

@doc """
Updates the key `#{__MODULE__}.params` and adds a new query params
"""
def add_query_param(%__MODULE__{} = b, _, nil), do: b

def add_query_param(%__MODULE__{} = b, key, value) do
%{b | params: Map.put(b.params, key, value)}
end

@doc """
Updates the key `#{__MODULE__}.headers` and adds a new request header
"""
def add_request_header(%__MODULE__{} = b, _, nil), do: b

def add_request_header(%__MODULE__{} = b, key, value) do
%{b | headers: Map.put(b.headers, key, value)}
end

@doc "Removes a request header"
def del_request_header(%__MODULE__{} = b, key) do
%{b | headers: Map.delete(b.headers, key)}
end

@doc "Changes the HTTP method that'll be used to execute the query"
def change_method(%__MODULE__{} = q, method) do
%{q | method: method}
end

@doc "Changes the request body that will be sent to the PostgREST server"
def change_body(%__MODULE__{} = q, body) do
%{q | body: body}
end
end
Loading

0 comments on commit 646fab7

Please sign in to comment.