1
1
defmodule AshPostgres do
2
2
@ using_opts_schema Ashton . schema (
3
3
opts: [
4
- repo: :atom
4
+ repo: :atom ,
5
+ table: :string
5
6
] ,
6
7
required: [ :repo ] ,
7
8
describe: [
8
9
repo:
9
- "The repo that will be used to fetch your data. See the `Ecto.Repo` documentation for more"
10
+ "The repo that will be used to fetch your data. See the `Ecto.Repo` documentation for more" ,
11
+ table: "The name of the database table backing the resource"
10
12
] ,
11
13
constraints: [
12
14
repo:
@@ -31,10 +33,15 @@ defmodule AshPostgres do
31
33
32
34
@ data_layer AshPostgres
33
35
@ repo opts [ :repo ]
36
+ @ table opts [ :table ]
34
37
35
38
def repo ( ) do
36
39
@ repo
37
40
end
41
+
42
+ def postgres_table ( ) do
43
+ @ table || @ name
44
+ end
38
45
end
39
46
end
40
47
@@ -217,13 +224,16 @@ defmodule AshPostgres do
217
224
end
218
225
219
226
defp do_join_relationship ( query , [ % { type: :many_to_many } = relationship ] , :inner ) do
227
+ relationship_through = maybe_get_resource_query ( relationship . through )
228
+ relationship_destination = maybe_get_resource_query ( relationship . destination )
229
+
220
230
new_query =
221
231
from ( row in query ,
222
- join: through in ^ relationship . through ,
232
+ join: through in ^ relationship_through ,
223
233
on:
224
234
field ( row , ^ relationship . source_field ) ==
225
235
field ( through , ^ relationship . source_field_on_join_table ) ,
226
- join: destination in ^ relationship . destination ,
236
+ join: destination in ^ relationship_destination ,
227
237
on:
228
238
field ( destination , ^ relationship . destination_field ) ==
229
239
field ( through , ^ relationship . destination_field_on_join_table )
@@ -238,23 +248,28 @@ defmodule AshPostgres do
238
248
end
239
249
240
250
defp do_join_relationship ( query , [ relationship ] , :inner ) do
251
+ relationship_destination = maybe_get_resource_query ( relationship . destination )
252
+
241
253
new_query =
242
254
from ( row in query ,
243
- join: destination in ^ relationship . destination ,
255
+ join: destination in ^ relationship_destination ,
244
256
on: field ( row , ^ relationship . source_field ) == field ( row , ^ relationship . destination_field )
245
257
)
246
258
247
259
add_binding ( new_query , [ relationship . name ] , :inner )
248
260
end
249
261
250
262
defp do_join_relationship ( query , [ % { type: :many_to_many } = relationship ] , :left ) do
263
+ relationship_through = maybe_get_resource_query ( relationship . through )
264
+ relationship_destination = maybe_get_resource_query ( relationship . destination )
265
+
251
266
new_query =
252
267
from ( row in query ,
253
- left_join: through in ^ relationship . through ,
268
+ left_join: through in ^ relationship_through ,
254
269
on:
255
270
field ( row , ^ relationship . source_field ) ==
256
271
field ( through , ^ relationship . source_field_on_join_table ) ,
257
- left_join: destination in ^ relationship . destination ,
272
+ left_join: destination in ^ relationship_destination ,
258
273
on:
259
274
field ( destination , ^ relationship . destination_field ) ==
260
275
field ( through , ^ relationship . destination_field_on_join_table )
@@ -269,9 +284,11 @@ defmodule AshPostgres do
269
284
end
270
285
271
286
defp do_join_relationship ( query , [ relationship ] , :left ) do
287
+ relationship_destination = maybe_get_resource_query ( relationship . destination )
288
+
272
289
new_query =
273
290
from ( row in query ,
274
- left_join: destination in ^ relationship . destination ,
291
+ left_join: destination in ^ relationship_destination ,
275
292
on: field ( row , ^ relationship . source_field ) == field ( row , ^ relationship . destination_field )
276
293
)
277
294
@@ -433,4 +450,12 @@ defmodule AshPostgres do
433
450
def transaction ( resource , func ) do
434
451
repo ( resource ) . transaction ( func )
435
452
end
453
+
454
+ defp maybe_get_resource_query ( resource ) do
455
+ if Ash . resource_module? ( resource ) do
456
+ { resource . postgres_table ( ) , resource }
457
+ else
458
+ resource
459
+ end
460
+ end
436
461
end
0 commit comments