/
cache_primary_key.ex
62 lines (51 loc) · 1.7 KB
/
cache_primary_key.ex
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
defmodule Ash.Resource.Transformers.CachePrimaryKey do
@moduledoc "Validates and caches the primary key of a resource"
use Ash.Dsl.Transformer
alias Ash.Dsl.Transformer
alias Ash.Error.Dsl.DslError
def transform(resource, dsl_state) do
primary_key_attribute =
dsl_state
|> Transformer.get_entities([:attributes])
|> Enum.filter(& &1.primary_key?)
pk_allows_nil? = Enum.any?(primary_key_attribute, & &1.allow_nil?)
primary_key =
if pk_allows_nil? == false do
Enum.map(primary_key_attribute, & &1.name)
else
:error_pk_allows_nil
end
case primary_key do
:error_pk_allows_nil ->
{:error,
DslError.exception(
module: __MODULE__,
message: "Primary keys must not be allowed to be nil"
)}
[] ->
{:error,
DslError.exception(
module: __MODULE__,
message: "Resources without a primary key are not yet supported"
)}
[field] ->
dsl_state = Transformer.persist(dsl_state, :primary_key, [field])
{:ok, dsl_state}
fields ->
if Ash.DataLayer.data_layer(resource) &&
Ash.DataLayer.data_layer_can?(resource, :composite_primary_key) do
dsl_state = Transformer.persist(dsl_state, :primary_key, fields)
{:ok, dsl_state}
else
{:error,
DslError.exception(
module: __MODULE__,
message: "Data layer does not support composite primary keys"
)}
end
end
end
def after?(Ash.Resource.Transformers.BelongsToAttribute), do: true
def after?(Ash.Resource.Transformers.DefaultPrimaryKey), do: true
def after?(_), do: false
end