-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
aggregate.ex
114 lines (110 loc) · 2.98 KB
/
aggregate.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
defmodule Ash.Resource.Aggregate do
@moduledoc "Represents a named aggregate on the resource that can be loaded"
defstruct [
:name,
:relationship_path,
:filter,
:kind,
:implementation,
:read_action,
:constraints,
:type,
:description,
:private?,
:field,
:sort,
:default,
:uniq?,
join_filters: [],
authorize?: true,
filterable?: true
]
defmodule JoinFilter do
@moduledoc "Represents a join filter on a resource aggregate"
defstruct [:relationship_path, :filter]
end
@schema [
name: [
type: :atom,
doc: "The field to place the aggregate in",
required: true
],
read_action: [
type: :atom,
doc: """
The read action to use when building the aggregate. Defaults to the primary read action. Keep in mind this action must not have any required arguments.
"""
],
relationship_path: [
type: {:wrap_list, :atom},
doc: "The relationship or relationship path to use for the aggregate",
required: true
],
kind: [
type:
{:or,
[
{:in, [:count, :first, :sum, :list, :avg, :max, :min, :exists, :custom]},
{:tuple, [{:in, [:custom]}, Ash.OptionsHelpers.ash_type()]}
]},
doc: "The kind of the aggregate",
required: true
],
field: [
type: :atom,
doc:
"The field to aggregate. Defaults to the first field in the primary key of the resource",
required: false
],
filter: [
type: :any,
doc: "A filter to apply to the aggregate",
default: []
],
sort: [
type: :any,
doc: "A sort to be applied to the aggregate"
],
description: [
type: :string,
doc: "An optional description for the aggregate"
],
default: [
type: :any,
doc: "A default value to use in cases where nil would be used. Count defaults to `0`."
],
private?: [
type: :boolean,
default: false,
doc:
"Whether or not the aggregate will appear in any interfaces created off of this resource, e.g AshJsonApi and AshGraphql"
],
filterable?: [
type: {:or, [:boolean, {:in, [:simple_equality]}]},
default: true,
doc: "Whether or not the aggregate should be usable in filters."
],
authorize?: [
type: :boolean,
default: true,
doc: """
Whether or not the aggregate query should authorize based on the target action, if the parent query is authorized. Requires filter checks on the target action.
"""
]
]
@type t :: %__MODULE__{
name: atom(),
relationship_path: list(atom()),
filter: Keyword.t(),
field: atom,
kind: Ash.Query.Aggregate.kind(),
description: String.t() | nil,
private?: boolean,
authorize?: boolean,
read_action: atom | nil,
default: term,
join_filters: %{list(atom) => term()}
}
@doc false
def schema, do: @schema
end