-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
read.ex
160 lines (149 loc) · 5.42 KB
/
read.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
defmodule Ash.Resource.Actions.Read do
@moduledoc "Represents a read action on a resource."
defstruct arguments: [],
description: nil,
filter: nil,
get_by: nil,
get?: nil,
manual: nil,
metadata: [],
modify_query: nil,
name: nil,
pagination: nil,
preparations: [],
primary?: nil,
touches_resources: [],
timeout: nil,
transaction?: false,
type: :read
@type t :: %__MODULE__{
arguments: [Ash.Resource.Actions.Argument.t()],
description: String.t() | nil,
filter: any,
get_by: nil | atom | [atom],
get?: nil | boolean,
manual: atom | {atom, Keyword.t()} | nil,
metadata: [Ash.Resource.Actions.Metadata.t()],
modify_query: nil | mfa,
name: atom,
pagination: any,
primary?: boolean,
touches_resources: [atom],
timeout: pos_integer() | nil,
transaction?: boolean,
type: :read
}
import Ash.Resource.Actions.SharedOptions
@global_opts shared_options()
@opt_schema Spark.OptionsHelpers.merge_schemas(
[
filter: [
type: :any,
doc:
"A filter template that will be applied whenever the action is used. See `Ash.Filter` for more on templates"
],
manual: [
type:
{:spark_function_behaviour, Ash.Resource.ManualRead,
{Ash.Resource.ManualRead.Function, 3}},
doc: """
Delegates running of the query to the provided module. Accepts a module or module and opts, or a function that takes the ash query, the data layer query, and context. See the [manual actions guide](/documentation/topics/manual-actions.md) for more.
"""
],
get?: [
type: :boolean,
default: false,
doc: """
Expresses that this action innately only returns a single result. Used by extensions to validate and/or modify behavior. Causes code interfaces to return a single value instead of a list. See the [code interface guide](/documentation/topics/code-interface.md) for more.
"""
],
modify_query: [
type: {:or, [:mfa, {:fun, 2}]},
doc: """
Allows direct manipulation of the data layer query via an MFA. The ash query and the data layer query will be provided as additional arguments. The result must be `{:ok, new_data_layer_query} | {:error, error}`.
"""
],
get_by: [
type: {:wrap_list, :atom},
default: nil,
doc: """
A helper to automatically generate a "get by X" action. Sets `get?` to true, add args for each of the specified fields, and adds a filter for each of the arguments.
"""
],
timeout: [
type: :pos_integer,
doc: """
The maximum amount of time, in milliseconds, that the action is allowed to run for. Ignored if the data layer doesn't support transactions *and* async is disabled.
"""
]
],
@global_opts,
"Action Options"
)
@pagination_schema [
keyset?: [
type: :boolean,
doc: "Whether or not keyset based pagination is supported",
default: false
],
offset?: [
type: :boolean,
doc: "Whether or not offset based pagination is supported",
default: false
],
default_limit: [
type: :pos_integer,
doc: "The default page size to apply, if one is not supplied"
],
countable: [
type: {:in, [true, false, :by_default]},
doc:
"Whether not a returned page will have a full count of all records. Use `:by_default` to do it automatically.",
default: false
],
max_page_size: [
type: :pos_integer,
doc: "The maximum amount of records that can be requested in a single page",
default: 250
],
required?: [
type: :boolean,
doc:
"Whether or not pagination can be disabled. Only relevant if some pagination configuration is supplied.",
default: true
]
]
defmodule Pagination do
@moduledoc "Represents the pagination configuration of a read action"
defstruct [
:default_limit,
:max_page_size,
countable: false,
required?: false,
keyset?: false,
offset?: false
]
@type t :: %__MODULE__{}
def transform(pagination) do
if pagination.keyset? or pagination.offset? do
{:ok, pagination}
else
{:error, "Must enable `keyset?` or `offset?`"}
end
end
end
def transform(read) do
if read.pagination do
if is_list(read.pagination) do
{:ok, %{read | pagination: List.last(read.pagination) || false}}
else
{:ok, %{read | pagination: read.pagination}}
end
else
{:ok, %{read | pagination: false}}
end
end
@doc false
def opt_schema, do: @opt_schema
def pagination_schema, do: @pagination_schema
end