-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: simplify builder\n\nfix some documentations, remove unecessary functions * split in submodules * format
- Loading branch information
Showing
11 changed files
with
1,131 additions
and
1,067 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.