This commit is contained in:
Zach Daniel 2019-12-05 13:45:42 -05:00
parent fe3d81c606
commit b33945d166
No known key found for this signature in database
GPG key ID: A57053A671EE649E
2 changed files with 45 additions and 10 deletions

View file

@ -1,18 +1,34 @@
defmodule AshPostgres do
@using_opts_schema Ashton.schema(
opts: [
repo: :atom
],
required: [:repo],
describe: [
repo:
"The repo that will be used to fetch your data. See the `Ecto.Repo` documentation for more"
],
constraints: [
repo:
{&AshPostgres.postgres_repo?/1, "must be using the postgres adapter"}
]
)
@moduledoc """
A postgres data layer that levereges Ecto's postgres tools.
To use it, add `use AshPostgres, repo: MyRepo` to your resource, after `use Ash.Resource`
#{Ashton.document(@using_opts_schema)}
"""
@behaviour Ash.DataLayer
defmacro __using__(opts) do
quote bind_quoted: [repo: opts[:repo]] do
quote bind_quoted: [opts: opts] do
opts = AshPostgres.validate_using_opts(__MODULE__, opts)
@data_layer AshPostgres
@repo repo
unless repo do
raise "You must pass the `repo` option to `use AshPostgres` for #{__MODULE__}"
end
unless repo.__adapter__() == Ecto.Adapters.Postgres do
raise "Only Ecto.Adapters.Postgres is supported with AshPostgres for now"
end
@repo opts[:repo]
def repo() do
@repo
@ -20,6 +36,24 @@ defmodule AshPostgres do
end
end
def validate_using_opts(mod, opts) do
case Ashton.validate(opts, @using_opts_schema) do
{:ok, opts} ->
opts
{:error, [{key, message} | _]} ->
raise Ash.Error.ResourceDslError,
resource: mod,
using: __MODULE__,
option: key,
message: message
end
end
def postgres_repo?(repo) do
repo.__adapter__() == Ecto.Adapters.Postgres
end
def repo(resource) do
resource.repo()
end

View file

@ -1,5 +1,6 @@
%{
"ash": {:hex, :ash, "0.1.0", "84b0e47f0f52e34bae71b682bb0689dc429b3da28ad3c02929226199a1941680", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.7.3", [hex: :ets, repo: "hexpm", optional: false]}], "hexpm"},
"ashton": {:hex, :ashton, "0.3.9", "1c089d62d35a17c1f31db4e9130fb90f8d802c8c9078fd29138be7b6b93305b5", [:mix], [], "hexpm"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
"dataloader": {:hex, :dataloader, "1.0.6", "fb724d6d3fb6acb87d27e3b32dea3a307936ad2d245faf9cf5221d1323d6a4ba", [:mix], [{:ecto, ">= 0.0.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"db_connection": {:hex, :db_connection, "2.1.1", "a51e8a2ee54ef2ae6ec41a668c85787ed40cb8944928c191280fe34c15b76ae5", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},