@@ -11,8 +11,7 @@ defmodule Supabase.PostgREST do
11
11
12
12
import Kernel , except: [ not: 1 , and: 2 , or: 2 , in: 2 ]
13
13
14
- import Supabase.Client , only: [ is_client: 1 ]
15
-
14
+ alias Supabase.Client
16
15
alias Supabase.PostgREST.Error
17
16
alias Supabase.PostgREST.FilterBuilder
18
17
alias Supabase.PostgREST.QueryBuilder
@@ -34,7 +33,7 @@ defmodule Supabase.PostgREST do
34
33
- Supabase documentation on initializing queries: https://supabase.com/docs/reference/javascript/from
35
34
"""
36
35
@ impl true
37
- def from ( client , table ) when is_client ( client ) do
36
+ def from ( % Client { } = client , table ) do
38
37
QueryBuilder . new ( table , client )
39
38
end
40
39
@@ -102,10 +101,12 @@ defmodule Supabase.PostgREST do
102
101
@ impl true
103
102
def insert ( % QueryBuilder { } = q , data , opts \\ [ ] ) do
104
103
on_conflict = Keyword . get ( opts , :on_conflict )
104
+ on_conflict = if on_conflict , do: "on_conflict=#{ on_conflict } "
105
105
upsert = if on_conflict , do: "resolution=merge-duplicates"
106
106
returning = Keyword . get ( opts , :returning , :representation )
107
107
count = Keyword . get ( opts , :count , :exact )
108
- prefer = Enum . join ( [ upsert , "return=#{ returning } " , "count=#{ count } " ] , "," )
108
+ prefer = [ "return=#{ returning } " , "count=#{ count } " , on_conflict , upsert ]
109
+ prefer = Enum . join ( Enum . reject ( prefer , & is_nil / 1 ) , "," )
109
110
110
111
case Jason . encode ( data ) do
111
112
{ :ok , body } ->
@@ -703,8 +704,7 @@ defmodule Supabase.PostgREST do
703
704
def overlaps ( % FilterBuilder { } = f , column , values )
704
705
when is_list ( values ) do
705
706
values
706
- |> Enum . map ( & "%##{ & 1 } " )
707
- |> Enum . join ( "," )
707
+ |> Enum . map_join ( "," , & "%##{ & 1 } " )
708
708
|> then ( & FilterBuilder . add_param ( f , column , "{#{ & 1 } }" ) )
709
709
end
710
710
@@ -830,7 +830,7 @@ defmodule Supabase.PostgREST do
830
830
"""
831
831
@ impl true
832
832
def single ( % FilterBuilder { } = f ) do
833
- FilterBuilder . add_header ( f , "accept" , "application/vnd.pgrst, object+json" )
833
+ FilterBuilder . add_header ( f , "accept" , "application/vnd.pgrst. object+json" )
834
834
end
835
835
836
836
@ doc """
@@ -896,19 +896,19 @@ defmodule Supabase.PostgREST do
896
896
def execute_to ( % FilterBuilder { } = f , schema ) when is_atom ( schema ) do
897
897
with { :ok , body } <- execute ( f . client , f . method , f . body , f . table , f . headers , f . params ) do
898
898
if is_list ( body ) do
899
- Enum . map ( body , & struct ( schema , & 1 ) )
899
+ { :ok , Enum . map ( body , & struct ( schema , & 1 ) ) }
900
900
else
901
- struct ( schema , body )
901
+ { :ok , struct ( schema , body ) }
902
902
end
903
903
end
904
904
end
905
905
906
906
def execute_to ( % QueryBuilder { } = q , schema ) when is_atom ( schema ) do
907
907
with { :ok , body } <- execute ( q . client , q . method , q . body , q . table , q . headers , q . params ) do
908
908
if is_list ( body ) do
909
- Enum . map ( body , & struct ( schema , & 1 ) )
909
+ { :ok , Enum . map ( body , & struct ( schema , & 1 ) ) }
910
910
else
911
- struct ( schema , body )
911
+ { :ok , struct ( schema , body ) }
912
912
end
913
913
end
914
914
end
@@ -929,35 +929,35 @@ defmodule Supabase.PostgREST do
929
929
- Supabase query execution: https://supabase.com/docs/reference/javascript/performing-queries
930
930
"""
931
931
@ impl true
932
- def execute_to_finch_request ( % mod { } = q ) when Kernel . in ( mod , [ FilterBuilder , QueryBuilder ] ) do
933
- with { :ok , % Supabase.Client { } = client } = Supabase.Client . retrieve_client ( q . client ) do
934
- base_url = Path . join ( [ client . conn . base_url , @ api_path , q . table ] )
935
- accept_profile = { "accept-profile" , client . db . schema }
936
- content_profile = { "content-profile" , client . db . schema }
937
- additional_headers = Map . to_list ( q . headers ) ++ [ accept_profile , content_profile ]
938
- headers = Supabase.Fetcher . apply_client_headers ( client , nil , additional_headers )
939
- query = URI . encode_query ( q . params )
940
- url = URI . new! ( base_url ) |> URI . append_query ( query )
941
-
942
- Supabase.Fetcher . new_connection ( q . method , url , q . body , headers )
943
- end
932
+ def execute_to_finch_request ( % mod { client: client } = q )
933
+ when Kernel . in ( mod , [ FilterBuilder , QueryBuilder ] ) do
934
+ base_url = Path . join ( [ client . conn . base_url , @ api_path , q . table ] )
935
+ headers = apply_headers ( client , q . headers )
936
+ query = URI . encode_query ( q . params )
937
+ url = URI . new! ( base_url ) |> URI . append_query ( query )
938
+
939
+ Supabase.Fetcher . new_connection ( q . method , url , q . body , headers )
944
940
end
945
941
946
942
defp execute ( client , method , body , table , headers , params ) do
947
- with { :ok , % Supabase.Client { } = client } = Supabase.Client . retrieve_client ( client ) do
948
- base_url = Path . join ( [ client . conn . base_url , @ api_path , table ] )
949
- accept_profile = { "accept-profile" , client . db . schema }
950
- content_profile = { "content-profile" , client . db . schema }
951
- additional_headers = Map . to_list ( headers ) ++ [ accept_profile , content_profile ]
952
- headers = Supabase.Fetcher . apply_client_headers ( client , nil , additional_headers )
953
- query = URI . encode_query ( params )
954
- url = URI . new! ( base_url ) |> URI . append_query ( query )
955
- request = request_fun_from_method ( method )
956
-
957
- url
958
- |> request . ( body , headers )
959
- |> parse_response ( )
960
- end
943
+ base_url = Path . join ( [ client . conn . base_url , @ api_path , table ] )
944
+ headers = apply_headers ( client , headers )
945
+ query = URI . encode_query ( params )
946
+ url = URI . new! ( base_url ) |> URI . append_query ( query )
947
+ request = request_fun_from_method ( method )
948
+
949
+ url
950
+ |> request . ( body , headers )
951
+ |> parse_response ( )
952
+ end
953
+
954
+ defp apply_headers ( client , headers ) do
955
+ accept_profile = { "accept-profile" , client . db . schema }
956
+ content_profile = { "content-profile" , client . db . schema }
957
+ content_type = { "content-type" , "application/json" }
958
+ additional_headers = Map . to_list ( headers ) ++ [ accept_profile , content_profile , content_type ]
959
+
960
+ Supabase.Fetcher . apply_client_headers ( client , nil , additional_headers )
961
961
end
962
962
963
963
defp request_fun_from_method ( :get ) , do: & Supabase.Fetcher . get / 3
@@ -984,92 +984,4 @@ defmodule Supabase.PostgREST do
984
984
defp success_resp? ( status ) do
985
985
Kernel . in ( status , 200 .. 399 )
986
986
end
987
-
988
- defmacrop wrap_postgrest_functions do
989
- quote unquote: false , bind_quoted: [ module: __MODULE__ ] do
990
- for { fun , arity } <- module . __info__ ( :functions ) do
991
- cond do
992
- fun == :from ->
993
- quote do
994
- @ doc """
995
- Check `Supabase.PostgREST.#{ unquote ( fun ) } /#{ unquote ( arity ) } `
996
- """
997
- def unquote ( fun ) ( schema ) do
998
- apply ( unquote ( module ) , unquote ( fun ) , [ @ client , schema ] )
999
- end
1000
- end
1001
-
1002
- arity == 1 ->
1003
- quote do
1004
- @ doc """
1005
- Check `Supabase.PostgREST.#{ unquote ( fun ) } /#{ unquote ( arity ) } `
1006
- """
1007
- def unquote ( fun ) ( data ) do
1008
- apply ( unquote ( module ) , unquote ( fun ) , [ data ] )
1009
- end
1010
- end
1011
-
1012
- true ->
1013
- args = for idx <- 1 .. arity , do: Macro . var ( :"arg#{ idx } " , module )
1014
-
1015
- quote do
1016
- @ doc """
1017
- Check `Supabase.PostgREST.#{ unquote ( fun ) } /#{ unquote ( arity ) } `
1018
- """
1019
- def unquote ( fun ) ( unquote_splicing ( args ) ) do
1020
- args = [ unquote_splicing ( args ) ]
1021
- apply ( unquote ( module ) , unquote ( fun ) , args )
1022
- end
1023
- end
1024
- end
1025
- end
1026
- end
1027
- end
1028
-
1029
- defmacro __using__ ( [ { :client , client } | opts ] ) do
1030
- config = Macro . escape ( Keyword . get ( opts , :config , % { } ) )
1031
-
1032
- quote location: :keep do
1033
- @ client unquote ( client )
1034
-
1035
- def child_spec ( opts ) do
1036
- % {
1037
- id: __MODULE__ ,
1038
- start: { __MODULE__ , :start_link , [ opts ] } ,
1039
- type: :supervisor
1040
- }
1041
- end
1042
-
1043
- def start_link ( opts \\ [ ] ) do
1044
- manage_clients? = Application . get_env ( :supabase_potion , :manage_clients , true )
1045
-
1046
- if manage_clients? do
1047
- Supabase . init_client ( unquote ( client ) , unquote ( config ) )
1048
- else
1049
- base_url =
1050
- Application . get_env ( :supabase_potion , :supabase_base_url ) ||
1051
- raise Supabase.MissingSupabaseConfig , :url
1052
-
1053
- api_key =
1054
- Application . get_env ( :supabase_potion , :supabase_api_key ) ||
1055
- raise Supabase.MissingSupabaseConfig , :key
1056
-
1057
- config =
1058
- unquote ( config )
1059
- |> Map . put ( :conn , % { base_url: base_url , api_key: api_key } )
1060
- |> Map . put ( :name , unquote ( client ) )
1061
-
1062
- opts = [ name: unquote ( client ) , client_info: config ]
1063
- Supabase.Client . start_link ( opts )
1064
- end
1065
- |> then ( fn
1066
- { :ok , pid } -> { :ok , pid }
1067
- { :error , { :already_started , pid } } -> { :ok , pid }
1068
- err -> err
1069
- end )
1070
- end
1071
-
1072
- unquote ( wrap_postgrest_functions ( ) )
1073
- end
1074
- end
1075
987
end
0 commit comments