@@ -11,7 +11,7 @@ defmodule Supabase.PostgREST do
11
11
12
12
alias Supabase.Client
13
13
alias Supabase.Fetcher
14
- alias Supabase.PostgREST.Builder
14
+ alias Supabase.Fetcher.Request
15
15
alias Supabase.PostgREST.Error
16
16
17
17
alias Supabase.PostgREST.FilterBuilder
@@ -78,7 +78,8 @@ defmodule Supabase.PostgREST do
78
78
@ impl true
79
79
def from ( % Client { } = client , table ) do
80
80
client
81
- |> Builder . new ( relation: table )
81
+ |> Request . new ( )
82
+ |> Request . with_database_url ( table )
82
83
|> with_custom_media_type ( :default )
83
84
end
84
85
@@ -94,8 +95,8 @@ defmodule Supabase.PostgREST do
94
95
iex> Supabase.PostgREST.schema(builder, private)
95
96
"""
96
97
@ impl true
97
- def schema ( % Builder { } = b , schema ) when is_binary ( schema ) do
98
- % { b | schema: schema }
98
+ def schema ( % Request { } = b , schema ) when is_binary ( schema ) do
99
+ put_in ( b . client . db . schema , schema )
99
100
end
100
101
101
102
@ doc """
@@ -112,10 +113,10 @@ defmodule Supabase.PostgREST do
112
113
- [PostgREST resource represation docs](https://docs.postgrest.org/en/v12/references/api/resource_representation.html)
113
114
"""
114
115
@ impl true
115
- def with_custom_media_type ( % Builder { } = b , media_type )
116
+ def with_custom_media_type ( % Request { } = b , media_type )
116
117
when is_atom ( media_type ) do
117
118
header = @ accept_headers [ media_type ] || @ accept_headers [ :default ]
118
- Builder . add_request_header ( b , "accept" , header )
119
+ Request . with_headers ( b , % { "accept" => header } )
119
120
end
120
121
121
122
@ doc """
@@ -131,26 +132,7 @@ defmodule Supabase.PostgREST do
131
132
- Supabase query execution: https://supabase.com/docs/reference/javascript/performing-queries
132
133
"""
133
134
@ impl true
134
- def execute ( % Builder { } = b ) , do: do_execute ( b )
135
-
136
- @ doc """
137
- Executes the query and returns the result as a JSON-encoded string.
138
-
139
- ## Parameters
140
- - `builder`: The Builder or Builder instance to execute.
141
-
142
- ## Examples
143
- iex> PostgREST.execute_string(builder)
144
-
145
- ## See also
146
- - Supabase query execution and response handling: https://supabase.com/docs/reference/javascript/performing-queries
147
- """
148
- @ impl true
149
- def execute_string ( % Builder { } = b ) do
150
- with { :ok , body } <- do_execute ( b ) do
151
- Jason . encode ( body )
152
- end
153
- end
135
+ def execute ( % Request { } = b ) , do: do_execute ( b )
154
136
155
137
@ doc """
156
138
Executes the query and maps the resulting data to a specified schema struct, useful for casting the results to Elixir structs.
@@ -166,88 +148,44 @@ defmodule Supabase.PostgREST do
166
148
- Supabase query execution and schema casting: https://supabase.com/docs/reference/javascript/performing-queries
167
149
"""
168
150
@ impl true
169
- def execute_to ( % Builder { } = b , schema ) when is_atom ( schema ) do
170
- with { :ok , body } <- do_execute ( b ) do
171
- if is_list ( body ) do
172
- { :ok , Enum . map ( body , & struct ( schema , & 1 ) ) }
173
- else
174
- { :ok , struct ( schema , body ) }
175
- end
176
- end
151
+ def execute_to ( % Request { } = b , schema ) when is_atom ( schema ) do
152
+ alias Supabase.PostgREST.SchemaDecoder
153
+
154
+ Request . with_body_decoder ( b , SchemaDecoder , schema: schema )
155
+ |> do_execute ( )
177
156
end
178
157
179
158
@ doc """
180
- Executes a query using the Finch HTTP client, formatting the request appropriately. Returns the HTTP request without executing it.
159
+ Builds a query using the Finch HTTP client, formatting the request appropriately. Returns the HTTP request without executing it.
181
160
182
161
## Parameters
183
162
- `builder`: The Builder or Builder instance to execute.
184
- - `schema`: Optional schema module to map the results.
185
163
186
164
## Examples
187
- iex> PostgREST.execute_to_finch_request(builder, User )
165
+ iex> PostgREST.execute_to_finch_request(builder)
188
166
189
167
## See also
190
168
- Supabase query execution: https://supabase.com/docs/reference/javascript/performing-queries
191
169
"""
192
170
@ impl true
193
- def execute_to_finch_request ( % Builder { client: client } = b ) do
194
- headers = Fetcher . apply_client_headers ( client , nil , Map . to_list ( b . headers ) )
195
- query = URI . encode_query ( b . params )
196
- url = URI . new! ( b . url ) |> URI . append_query ( query )
171
+ def execute_to_finch_request ( % Request { } = b ) do
172
+ query = URI . encode_query ( b . query )
173
+ url = URI . parse ( b . url ) |> URI . append_query ( query )
197
174
198
- Supabase.Fetcher . new_connection ( b . method , url , b . body , headers )
175
+ Finch . build ( b . method , url , b . headers , b . body )
199
176
end
200
177
201
- defp do_execute ( % Builder { client: client } = b ) do
202
- headers = Fetcher . apply_client_headers ( client , nil , Map . to_list ( b . headers ) )
203
- query = URI . encode_query ( b . params )
204
- url = URI . new! ( b . url ) |> URI . append_query ( query )
205
- request = request_fun_from_method ( b . method )
178
+ defp do_execute ( % Request { client: client } = b ) do
179
+ schema = client . db . schema
206
180
207
- url
208
- |> request . ( b . body , headers )
209
- |> parse_response ( )
210
- end
211
-
212
- defp request_fun_from_method ( :get ) , do: & Supabase.Fetcher . get / 3
213
- defp request_fun_from_method ( :head ) , do: & Supabase.Fetcher . head / 3
214
- defp request_fun_from_method ( :post ) , do: & Supabase.Fetcher . post / 3
215
- defp request_fun_from_method ( :delete ) , do: & Supabase.Fetcher . delete / 3
216
- defp request_fun_from_method ( :patch ) , do: & Supabase.Fetcher . patch / 3
217
-
218
- defp parse_response ( { :error , reason } ) , do: { :error , reason }
219
-
220
- defp parse_response ( { :ok , % { status: _ , body: "" } } ) do
221
- { :ok , nil }
222
- end
223
-
224
- defp parse_response ( { :ok , % { status: status , body: raw , headers: headers } } ) do
225
- if json_content? ( headers ) do
226
- with { :ok , body } <- Jason . decode ( raw , keys: :atoms ) do
227
- cond do
228
- error_resp? ( status ) -> { :error , Error . from_raw_body ( body ) }
229
- success_resp? ( status ) -> { :ok , body }
230
- end
231
- end
232
- else
233
- { :ok , raw }
234
- end
235
- end
236
-
237
- defp json_content? ( headers ) when is_list ( headers ) do
238
- headers
239
- |> Enum . find_value ( fn
240
- { "content-type" , type } -> type
241
- _ -> false
242
- end )
243
- |> String . match? ( ~r/ json/ )
244
- end
245
-
246
- defp error_resp? ( status ) do
247
- Kernel . in ( status , 400 .. 599 )
248
- end
181
+ schema_header =
182
+ if b . method in [ :get , :head ] ,
183
+ do: % { "accept-profile" => schema } ,
184
+ else: % { "content-profile" => schema }
249
185
250
- defp success_resp? ( status ) do
251
- Kernel . in ( status , 200 .. 399 )
186
+ b
187
+ |> Request . with_error_parser ( Error )
188
+ |> Request . with_headers ( schema_header )
189
+ |> Fetcher . request ( )
252
190
end
253
191
end
0 commit comments