add concept of postgres_table

This commit is contained in:
Andrew Callahan 2020-05-03 08:30:10 -05:00
parent 864f83cbde
commit 4c072b8736

View file

@ -14,7 +14,7 @@ defmodule AshPostgres do
] ]
) )
alias Ash.Filter.{And, Eq, Filter, In, NotEq, NotIn, Or} alias Ash.Filter.{And, Eq, In, NotEq, NotIn, Or}
@moduledoc """ @moduledoc """
A postgres data layer that levereges Ecto's postgres tools. A postgres data layer that levereges Ecto's postgres tools.
@ -101,7 +101,8 @@ defmodule AshPostgres do
end end
@impl true @impl true
def resource_to_query(resource), do: Ecto.Queryable.to_query(resource) def resource_to_query(resource),
do: Ecto.Queryable.to_query({resource.postgres_table(), resource})
@impl true @impl true
def create(resource, changeset) do def create(resource, changeset) do
@ -111,6 +112,9 @@ defmodule AshPostgres do
action -> action action -> action
end) end)
changeset =
Map.update!(changeset, :__meta__, &Map.put(&1, :source, resource.postgres_table()))
repo(resource).insert(changeset) repo(resource).insert(changeset)
rescue rescue
e -> e ->
@ -125,6 +129,14 @@ defmodule AshPostgres do
{:error, e} {:error, e}
end end
@impl true
def destroy(%resource{} = record) do
repo(resource).delete(record)
rescue
e ->
{:error, e}
end
@impl true @impl true
def sort(query, sort, _resource) do def sort(query, sort, _resource) do
{:ok, {:ok,
@ -134,6 +146,7 @@ defmodule AshPostgres do
end end
@impl true @impl true
# TODO: I have learned from experience that no single approach here # TODO: I have learned from experience that no single approach here
# will be a one-size-fits-all. We need to either use complexity metrics, # will be a one-size-fits-all. We need to either use complexity metrics,
# hints from the interface, or some other heuristic to do our best to # hints from the interface, or some other heuristic to do our best to