improvement!: update to Ash 3.0

This commit is contained in:
Zach Daniel 2024-04-03 12:44:00 -04:00
parent fbaed4493e
commit 04e5bd7d1f
5 changed files with 42 additions and 33 deletions

View file

@ -1,6 +1,6 @@
import Config
if Mix.env() == :test do
config :ash, :validate_api_resource_inclusion?, false
config :ash, :validate_api_config_inclusion?, false
config :ash, :validate_domain_resource_inclusion?, false
config :ash, :validate_domain_config_inclusion?, false
end

View file

@ -14,9 +14,10 @@ defmodule AshEvents.PersistEvent do
action: opts[:action],
processed: true
},
actor: Ash.context_to_opts(context)
actor: Ash.Context.to_opts(context),
domain: input.domain
)
|> input.api.create!()
|> Ash.create!()
{:ok, :success}
@ -31,13 +32,14 @@ defmodule AshEvents.PersistEvent do
action: opts[:action],
processed: true
},
actor: Ash.context_to_opts(context)
actor: Ash.Context.to_opts(context),
domain: input.domain
)
|> input.api.create!()
|> Ash.create!()
event.resource
|> Ash.Changeset.for_create(event.action, event.input)
|> input.api.create!()
|> Ash.Changeset.for_create(event.action, event.input, domain: input.domain)
|> Ash.create!()
{:ok, :success}
end

View file

@ -21,9 +21,7 @@ defmodule AshEvents.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:ash, "~> 2.14"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
{:ash, path: "../ash"}
]
end
end

View file

@ -1,16 +1,19 @@
%{
"ash": {:hex, :ash, "2.14.12", "bb2e3dbe82b49407f07854560ba3174a2c479508da8f33eb296764c6445477fb", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: true]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8.0", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: false]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:spark, ">= 1.1.20 and < 2.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:stream_data, "~> 0.5.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "799edb6326bd539ede0582b3e62f015a74062d3291e7b2c21ce4b0a2f9ebe88c"},
"ash": {:hex, :ash, "3.0.0-rc.8", "775e14c3ca7f20a73cf92fdfd6f4244712658f06a860aaff915bc471b2c82881", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 0.8", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.1.7 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 0.6", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f74451def7ae71f33e82fc3263312dd6521705c0db4176d1f6f02fec32b10dc0"},
"comparable": {:hex, :comparable, "1.0.0", "bb669e91cedd14ae9937053e5bcbc3c52bb2f22422611f43b6e38367d94a495f", [:mix], [{:typable, "~> 0.1", [hex: :typable, repo: "hexpm", optional: false]}], "hexpm", "277c11eeb1cd726e7cd41c6c199e7e52fa16ee6830b45ad4cdc62e51f62eb60c"},
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
"ecto": {:hex, :ecto, "3.10.3", "eb2ae2eecd210b4eb8bece1217b297ad4ff824b4384c0e3fdd28aaf96edd6135", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "44bec74e2364d491d70f7e42cd0d690922659d329f6465e89feb8a34e8cd3433"},
"ecto": {:hex, :ecto, "3.11.2", "e1d26be989db350a633667c5cda9c3d115ae779b66da567c68c80cfb26a8c9ee", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3c38bca2c6f8d8023f2145326cc8a80100c3ffe4dcbd9842ff867f7fc6156c65"},
"elixir_make": {:hex, :elixir_make, "0.7.7", "7128c60c2476019ed978210c245badf08b03dbec4f24d05790ef791da11aa17c", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bc19fff950fad52bbe5f211b12db9ec82c6b34a9647da0c2224b8b8464c7e6c"},
"ets": {:hex, :ets, "0.8.1", "8ff9bcda5682b98493f8878fc9dbd990e48d566cba8cce59f7c2a78130da29ea", [:mix], [], "hexpm", "6be41b50adb5bc5c43626f25ea2d0af1f4a242fb3fad8d53f0c67c20b78915cc"},
"ets": {:hex, :ets, "0.9.0", "79c6a6c205436780486f72d84230c6cba2f8a9920456750ddd1e47389107d5fd", [:mix], [], "hexpm", "2861fdfb04bcaeff370f1a5904eec864f0a56dcfebe5921ea9aadf2a481c822b"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"libgraph": {:hex, :libgraph, "0.16.0", "3936f3eca6ef826e08880230f806bfea13193e49bf153f93edcf0239d4fd1d07", [:mix], [], "hexpm", "41ca92240e8a4138c30a7e06466acc709b0cbb795c643e9e17174a178982d6bf"},
"nimble_options": {:hex, :nimble_options, "1.0.2", "92098a74df0072ff37d0c12ace58574d26880e522c22801437151a159392270e", [:mix], [], "hexpm", "fd12a8db2021036ce12a309f26f564ec367373265b53e25403f0ee697380f1b8"},
"picosat_elixir": {:hex, :picosat_elixir, "0.2.3", "bf326d0f179fbb3b706bb2c15fbc367dacfa2517157d090fdfc32edae004c597", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "f76c9db2dec9d2561ffaa9be35f65403d53e984e8cd99c832383b7ab78c16c66"},
"sourceror": {:hex, :sourceror, "0.13.0", "c6ecc96ee3ae0e042e9082a9550a1989ea40182492dc29024a8d9d2b136e5014", [:mix], [], "hexpm", "d0a819491061cd26bfa4450d1c84301a410c19c1782a6577ce15853fc0e7e4e1"},
"spark": {:hex, :spark, "1.1.25", "9a4836520b71a485f5dedfdab1909071c01375b0409eb4cc2e9cfa8cc28c0398", [:mix], [{:nimble_options, "~> 0.5 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:sourceror, "~> 0.1", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "506dfb6e8a851362ed5bb056644f9f852ec7513df77c65c09ef8f71518a91640"},
"stream_data": {:hex, :stream_data, "0.5.0", "b27641e58941685c75b353577dc602c9d2c12292dd84babf506c2033cd97893e", [:mix], [], "hexpm", "012bd2eec069ada4db3411f9115ccafa38540a3c78c4c0349f151fc761b9e271"},
"reactor": {:hex, :reactor, "0.8.1", "1aec71d16083901277727c8162f6dd0f07e80f5ca98911b6ef4f2c95e6e62758", [:mix], [{:libgraph, "~> 0.16", [hex: :libgraph, repo: "hexpm", optional: false]}, {:spark, "~> 2.0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.2", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ae3936d97a3e4a316744f70c77b85345b08b70da334024c26e6b5eb8ede1246b"},
"sourceror": {:hex, :sourceror, "1.0.2", "c5e86fdc14881f797749d1fe5df017ca66727a8146e7ee3e736605a3df78f3e6", [:mix], [], "hexpm", "832335e87d0913658f129d58b2a7dc0490ddd4487b02de6d85bca0169ec2bd79"},
"spark": {:hex, :spark, "2.1.11", "8093149dfd583b5ce2c06e1fea1faaf4125b50e4703138b2cbefb78c8f4aa07f", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:sourceror, "~> 1.0", [hex: :sourceror, repo: "hexpm", optional: false]}], "hexpm", "1877d92ab993b860e9d828bfd72d50367c0d3a53dd84f4de5d221baf66ae8723"},
"splode": {:hex, :splode, "0.2.1", "020079ec06c9e00f8b6586852e781b5e07aee6ba588f3f45dd993831c87b0511", [:mix], [], "hexpm", "d232a933666061fe1f659d9906042fa94b9b393bb1129a4fde6fa680033b2611"},
"stream_data": {:hex, :stream_data, "0.6.0", "e87a9a79d7ec23d10ff83eb025141ef4915eeb09d4491f79e52f2562b73e5f47", [:mix], [], "hexpm", "b92b5031b650ca480ced047578f1d57ea6dd563f5b57464ad274718c9c29501c"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"typable": {:hex, :typable, "0.3.0", "0431e121d124cd26f312123e313d2689b9a5322b15add65d424c07779eaa3ca1", [:mix], [], "hexpm", "880a0797752da1a4c508ac48f94711e04c86156f498065a83d160eef945858f8"},
}

View file

@ -3,8 +3,8 @@ defmodule AshEventsTest do
require Ash.Query
defmodule Api do
use Ash.Api
defmodule Domain do
use Ash.Domain
resources do
allow_unregistered?(true)
@ -13,9 +13,11 @@ defmodule AshEventsTest do
defmodule Event do
use Ash.Resource,
domain: Domain,
data_layer: Ash.DataLayer.Ets
actions do
default_accept :*
defaults([:read, :create, :update, :destroy])
update :process do
@ -29,12 +31,13 @@ defmodule AshEventsTest do
attributes do
uuid_primary_key(:id)
attribute(:input, :map, allow_nil?: false)
attribute(:resource, :atom, allow_nil?: false)
attribute(:action, :atom, allow_nil?: false)
attribute(:processed, :boolean, allow_nil?: false, default: false)
attribute(:input, :map, allow_nil?: false, public?: true)
attribute(:resource, :atom, allow_nil?: false, public?: true)
attribute(:action, :atom, allow_nil?: false, public?: true)
attribute(:processed, :boolean, allow_nil?: false, default: false, public?: true)
attribute :timestamp, :utc_datetime_usec do
public? true
default(&DateTime.utc_now/0)
allow_nil?(false)
writable?(false)
@ -44,6 +47,7 @@ defmodule AshEventsTest do
defmodule Profile do
use Ash.Resource,
domain: Domain,
data_layer: Ash.DataLayer.Ets,
extensions: [AshEvents]
@ -52,6 +56,7 @@ defmodule AshEventsTest do
end
actions do
default_accept :*
defaults([:create, :read, :update, :destroy])
end
@ -61,24 +66,25 @@ defmodule AshEventsTest do
attributes do
uuid_primary_key(:id)
attribute(:bio, :string)
attribute(:bio, :string, public?: true)
end
relationships do
belongs_to :user, AshEventsTest.User do
public? true
allow_nil?(false)
attribute_writable?(true)
end
end
code_interface do
define_for(Api)
define(:create)
end
end
defmodule User do
use Ash.Resource,
domain: Domain,
data_layer: Ash.DataLayer.Ets,
extensions: [AshEvents]
@ -92,17 +98,18 @@ defmodule AshEventsTest do
attributes do
uuid_primary_key(:id)
attribute(:username, :string, allow_nil?: false)
attribute(:username, :string, allow_nil?: false, public?: true)
end
actions do
default_accept :*
defaults([:read, :update, :destroy])
create :create do
primary?(true)
change(
after_action(fn _changeset, result ->
after_action(fn _changeset, result, _context ->
Profile.create!(%{user_id: result.id, bio: "Initial Bio!"})
{:ok, result}
@ -112,21 +119,20 @@ defmodule AshEventsTest do
end
code_interface do
define_for(Api)
define(:create)
end
relationships do
has_one(:profile, Profile)
has_one(:profile, Profile, public?: true)
end
end
test "test" do
assert [] = Api.read!(Event)
assert [] = Ash.read!(Event)
User.create!(%{username: "fred"})
assert [_, _] = Api.read!(Event)
assert [_] = Api.read!(User)
assert [_] = Api.read!(Profile)
assert [_, _] = Ash.read!(Event)
assert [_] = Ash.read!(User)
assert [_] = Ash.read!(Profile)
end
# defp process() do