forked from wojtekmach/mix_install_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ash.exs
69 lines (55 loc) · 1.58 KB
/
ash.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Mix.install(
[
{:ash, "~> 3.0"}
],
consolidate_protocols: false
)
defmodule Accounts.Profile do
use Ash.Resource,
domain: Accounts,
data_layer: Ash.DataLayer.Ets
actions do
defaults [:read, :destroy, create: [:name], update: [:name]]
end
attributes do
uuid_primary_key :id
attribute :name, :string
end
end
defmodule Accounts do
use Ash.Domain, validate_config_inclusion?: false
resources do
resource Accounts.Profile do
define :all_profiles, action: :read
define :profile_by_id, action: :read, get_by: [:id]
define :create_profile, args: [:name], action: :create
define :update_profile, args: [:name], action: :update
define :delete_profile, action: :destroy
end
end
end
IO.puts("#{IO.ANSI.yellow()}\nAsh: With the code interface we defined on the Accounts domain (preferred)")
Accounts.create_profile!("Joe Armstrong")
[profile] = Accounts.all_profiles!() |> IO.inspect()
Accounts.update_profile!(profile, "José Valim")
Accounts.profile_by_id!(profile.id) |> IO.inspect()
Accounts.destroy!(profile)
Accounts.all_profiles!() |> IO.inspect()
IO.puts("#{IO.ANSI.yellow()}\nAsh: Interacting with resource actions directly (equivalent to the above)")
Accounts.Profile
|> Ash.Changeset.for_create(:create, %{name: "Joe Armstrong"})
|> Ash.create!()
[profile] =
Accounts.Profile
|> Ash.read!()
|> IO.inspect()
profile
|> Ash.Changeset.for_update(:update, %{name: "José Valim"})
|> Ash.update!()
Accounts.Profile
|> Ash.read!()
|> IO.inspect()
Ash.destroy!(profile)
Accounts.Profile
|> Ash.read!()
|> IO.inspect()