Skip to content

Commit d8cd732

Browse files
Merge pull request #2 from ash-project/generate-resources
Postgres Tables
2 parents 4c072b8 + 7ce79ef commit d8cd732

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

lib/ash_postgres.ex

+33-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
defmodule AshPostgres do
22
@using_opts_schema Ashton.schema(
33
opts: [
4-
repo: :atom
4+
repo: :atom,
5+
table: :string
56
],
67
required: [:repo],
78
describe: [
89
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"
1012
],
1113
constraints: [
1214
repo:
@@ -31,10 +33,15 @@ defmodule AshPostgres do
3133

3234
@data_layer AshPostgres
3335
@repo opts[:repo]
36+
@table opts[:table]
3437

3538
def repo() do
3639
@repo
3740
end
41+
42+
def postgres_table() do
43+
@table || @name
44+
end
3845
end
3946
end
4047

@@ -217,13 +224,16 @@ defmodule AshPostgres do
217224
end
218225

219226
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+
220230
new_query =
221231
from(row in query,
222-
join: through in ^relationship.through,
232+
join: through in ^relationship_through,
223233
on:
224234
field(row, ^relationship.source_field) ==
225235
field(through, ^relationship.source_field_on_join_table),
226-
join: destination in ^relationship.destination,
236+
join: destination in ^relationship_destination,
227237
on:
228238
field(destination, ^relationship.destination_field) ==
229239
field(through, ^relationship.destination_field_on_join_table)
@@ -238,23 +248,28 @@ defmodule AshPostgres do
238248
end
239249

240250
defp do_join_relationship(query, [relationship], :inner) do
251+
relationship_destination = maybe_get_resource_query(relationship.destination)
252+
241253
new_query =
242254
from(row in query,
243-
join: destination in ^relationship.destination,
255+
join: destination in ^relationship_destination,
244256
on: field(row, ^relationship.source_field) == field(row, ^relationship.destination_field)
245257
)
246258

247259
add_binding(new_query, [relationship.name], :inner)
248260
end
249261

250262
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+
251266
new_query =
252267
from(row in query,
253-
left_join: through in ^relationship.through,
268+
left_join: through in ^relationship_through,
254269
on:
255270
field(row, ^relationship.source_field) ==
256271
field(through, ^relationship.source_field_on_join_table),
257-
left_join: destination in ^relationship.destination,
272+
left_join: destination in ^relationship_destination,
258273
on:
259274
field(destination, ^relationship.destination_field) ==
260275
field(through, ^relationship.destination_field_on_join_table)
@@ -269,9 +284,11 @@ defmodule AshPostgres do
269284
end
270285

271286
defp do_join_relationship(query, [relationship], :left) do
287+
relationship_destination = maybe_get_resource_query(relationship.destination)
288+
272289
new_query =
273290
from(row in query,
274-
left_join: destination in ^relationship.destination,
291+
left_join: destination in ^relationship_destination,
275292
on: field(row, ^relationship.source_field) == field(row, ^relationship.destination_field)
276293
)
277294

@@ -433,4 +450,12 @@ defmodule AshPostgres do
433450
def transaction(resource, func) do
434451
repo(resource).transaction(func)
435452
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
436461
end

lib/migration.exs

Whitespace-only changes.

0 commit comments

Comments
 (0)