-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
builtins.ex
409 lines (307 loc) · 13.4 KB
/
builtins.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
defmodule Ash.Resource.Change.Builtins do
@moduledoc """
Built in changes that are available to all resources
The functions in this module are imported by default in the actions section.
"""
@doc """
Applies a filter to the changeset. Has no effect for create actions.
This ensures that only things matching the provided filter are updated or destroyed.
"""
@spec filter(expr :: Ash.Expr.t()) :: Ash.Resource.Change.ref()
def filter(filter) do
{Ash.Resource.Change.Filter, filter: filter}
end
@doc """
Relates the actor to the data being changed, as the provided relationship.
## Options
#{Spark.Options.docs(Ash.Resource.Change.RelateActor.opt_schema())}
## Examples
change relate_actor(:owner, allow_nil?: true)
"""
@spec relate_actor(relationship :: atom, opts :: Keyword.t()) :: Ash.Resource.Change.ref()
def relate_actor(relationship, opts \\ []) do
opts =
opts
|> Keyword.put(:relationship, relationship)
|> Keyword.put_new(:allow_nil?, false)
{Ash.Resource.Change.RelateActor, opts}
end
@spec debug_log(label :: String.t() | nil) :: Ash.Resource.Change.ref()
def debug_log(label \\ nil) do
{Ash.Resource.Change.DebugLog, label: label}
end
@doc """
Apply an "optimistic lock" on a record being updated or destroyed.
This is modeled after ecto's implementation of optimistic locking, so to
read more, see their documentation: https://hexdocs.pm/ecto/Ecto.Changeset.html#optimistic_lock/3
The primary difference is that we leave it to you to increment the field being used for optimistic locking
yourself. So in ecto you might do `Changeset.optimistic_lock(changeset, :foo)` and that would add 1 to the `:foo` attribute
automatically. In Ash, you would combine this with the `increment/1` change.
```elixir
change optimistic_lock(:foo)
change increment(:foo)
```
"""
def optimistic_lock(attribute) do
{Ash.Resource.Change.OptimisticLock, attribute: attribute}
end
@doc """
Re-fetches the record being updated and locks it for update.
Only usable with data layers that support locking `:for_update`.
This happens in a `before_action` hook (so that it is done as part of the transaction).
If your resource has global validations (in the top level `validations` block), you may
want to add `delay_global_validations? true` to your action to ensure they happen on the
locked record.
"""
@spec get_and_lock_for_update :: Ash.Resource.Change.ref()
def get_and_lock_for_update do
{Ash.Resource.Change.GetAndLockForUpdate, []}
end
@doc """
Re-fetches the record being updated and locks it with the given type.
This happens in a `before_action` hook (so that it is done as part of the transaction).
If your resource has global validations (in the top level `validations` block), you may
want to add `delay_global_validations? true` to your action to ensure they happen on the
locked record.
"""
@spec get_and_lock(lock :: Ash.DataLayer.lock_type()) :: Ash.Resource.Change.ref()
def get_and_lock(lock) do
{Ash.Resource.Change.GetAndLock, [lock: lock]}
end
@doc """
Updates an existing attribute change by applying a function to it.
The update function gets called with the value already cast to the correct type, and only gets called
on valid changesets, so the value is guaranteed to have passed validations and constraints.
"""
defmacro update_change(attribute, function) do
{value, function} =
Spark.CodeHelpers.lift_functions(function, :change_update_change, __CALLER__)
quote generated: true do
unquote(function)
{Ash.Resource.Change.UpdateChange, attribute: unquote(attribute), function: unquote(value)}
end
end
@doc """
Increments an attribute's value by the amount specified, which defaults to 1.
Options:
* `:amount` - Defaults to 1
* `:overflow_limit` - Defaults to `nil`. If the value is over the overflow limit it will roll-over to the amount being incremented by (for common database limit support)
"""
@spec increment(attribute :: atom, opts :: Keyword.t()) :: Ash.Resource.Change.ref()
def increment(attribute, opts \\ []) do
opts =
opts
|> Keyword.put_new(:amount, 1)
|> Keyword.put_new(:overflow_limit, nil)
|> Keyword.put(:attribute, attribute)
{Ash.Resource.Change.Increment, opts}
end
@doc """
Sets the attribute to the value provided.
If a zero argument function is provided, it is called to determine the value.
Use `arg(:argument_name)` to use the value of the given argument. If the argument is not supplied then nothing happens.
## Options
#{Spark.Options.docs(Keyword.drop(Ash.Resource.Change.SetAttribute.opt_schema(), [:attribute, :value]))}
## Examples
change set_attribute(:active, false)
change set_attribute(:opened_at, &DateTime.utc_now/0)
change set_attribute(:status, arg(:status))
change set_attribute(:encrypted_data, arg(:data), set_when_nil?: false)
"""
@spec set_attribute(
attribute :: atom,
(-> term) | {:_arg, :status} | term(),
opts :: Keyword.t()
) ::
Ash.Resource.Change.ref()
def set_attribute(attribute, value, opts \\ []) do
opts =
opts
|> Keyword.put(:attribute, attribute)
|> Keyword.put(:value, value)
{Ash.Resource.Change.SetAttribute, opts}
end
@doc """
Updates an attribute using an expression. See `Ash.Changeset.atomic_update/3` for more.
Options:
* `:cast_atomic?` - set to `false` to ignore atomic type casting logic. Defaults to `true`.
"""
@spec atomic_update(attribute :: atom, expr :: Ash.Expr.t(), opts :: Keyword.t()) ::
Ash.Resource.Change.ref()
def atomic_update(attribute, expr, opts \\ []) do
{Ash.Resource.Change.Atomic,
attribute: attribute, expr: expr, cast_atomic?: Keyword.get(opts, :cast_atomic?, true)}
end
@doc """
Sets the attribute to the value provided if the attribute is not already being changed.
If a zero argument function is provided, it is called to determine the value.
Use `arg(:argument_name)` to use the value of the given argument. If the argument is not supplied then nothing happens.
## Examples
change set_new_attribute(:active, false)
change set_new_attribute(:opened_at, &DateTime.utc_now/0)
change set_new_attribute(:status, arg(:status))
"""
@spec set_new_attribute(relationship :: atom, (-> term) | {:_arg, :status} | term()) ::
Ash.Resource.Change.ref()
def set_new_attribute(attribute, value) do
{Ash.Resource.Change.SetAttribute, attribute: attribute, value: value, new?: true}
end
@doc """
Clears a change off of the changeset before the action runs.
Does not fail if it is being changed, but ensures it is cleared just before the action.
Can be useful if a change is only used in validations but shouldn't ultimately be written to the data layer.
## Examples
change prevent_change(:email)
"""
@spec prevent_change(attribute :: atom) :: Ash.Resource.Change.ref()
def prevent_change(attribute) do
{Ash.Resource.Change.PreventChange, field: attribute}
end
@doc """
Calls `Ash.Changeset.manage_relationship/4` with the changeset and relationship provided, using the value provided for the named argument.
If relationship_name is not specified, it is assumed to be the same as the argument.
For information on the available options, see `Ash.Changeset.manage_relationship/4`.
## Examples
change manage_relationship(:comments, type: :append)
change manage_relationship(:remove_comments, :comments, type: :remove)
"""
@spec manage_relationship(
argument :: atom,
relationship_name :: atom | nil,
opts :: Keyword.t()
) ::
Ash.Resource.Change.ref()
def manage_relationship(argument, relationship_name \\ nil, opts) do
relationship_name = relationship_name || argument
{Ash.Resource.Change.ManageRelationship,
[argument: argument, relationship: relationship_name, opts: opts]}
end
@doc """
Merges the given query context.
If an MFA is provided, it will be called with the changeset.
The MFA should return `{:ok, context_to_be_merged}` or `{:error, term}`
## Examples
change set_context(%{something_used_internally: true})
change set_context({MyApp.Context, :set_context, []})
"""
@spec set_context(context :: map | mfa) ::
Ash.Resource.Change.ref()
def set_context(context) do
{Ash.Resource.Change.SetContext, context: context}
end
@doc """
Passes the provided value into `Ash.load` after the action has completed.
## Example
change load(:comments)
change load([:friend_count, :friends])
"""
@spec load(load :: term()) :: Ash.Resource.Change.ref()
def load(value) do
{Ash.Resource.Change.Load, target: value}
end
@doc """
Passes the provided value into `Ash.Changeset.select/3`
Keep in mind, this will *limit* the fields that are selected. You may want `ensure_selected/1` if you
want to make sure that something is selected, without deselecting anything else.
Selecting in changesets does not actually do a select in the data layer. It nils out any
fields that were not selected after completing the action. This can be useful if you are writing
policies that have to do with specific fields being selected.
## Example
change select([:name])
"""
@spec select(select :: atom | list(atom)) :: Ash.Resource.Change.ref()
def select(value) do
{Ash.Resource.Change.Select, target: value}
end
@doc """
Passes the provided value into `Ash.Changeset.ensure_selected/2`
If the value is not already selected, this makes sure it is. Does not deselect anything else.
## Example
change ensure_selected([:necessary_field])
"""
@spec ensure_selected(select :: atom | list(atom)) :: Ash.Resource.Change.ref()
def ensure_selected(value) do
{Ash.Resource.Change.Select, target: value, ensure?: true}
end
@doc ~S"""
Directly attach an `after_action` function to the current change.
See `Ash.Changeset.after_action/3` for more information.
Provide the option `prepend?: true` to place the hook before all other hooks instead of after.
## Example
change after_action(fn changeset, record, _context ->
Logger.debug("Successfully executed action #{changeset.action.name} on #{inspect(changeset.resource)}")
{:ok, record}
end)
"""
# @dialyzer {:nowarn_function, "MACRO-after_action": 3}
defmacro after_action(callback, opts \\ []) do
{value, function} =
Spark.CodeHelpers.lift_functions(callback, :change_after_action, __CALLER__)
quote generated: true do
unquote(function)
{Ash.Resource.Change.AfterAction,
callback: unquote(value), prepend?: unquote(Keyword.get(opts, :prepend?, false))}
end
end
@doc ~S"""
Directly attach an `after_transaction` function to the current change.
See `Ash.Changeset.after_transaction/3` for more information.
Provide the option `prepend?: true` to place the hook before all other hooks instead of after.
## Example
change after_transaction(fn
changeset, {:ok, record}, _context ->
Logger.debug("Successfully executed transaction for action #{changeset.action.name} on #{inspect(changeset.resource)}")
{:ok, record}
changeset, {:error, reason}, _context ->
Logger.debug("Failed to execute transaction for action #{changeset.action.name} on #{inspect(changeset.resource)}, reason: #{inspect(reason)}")
{:error, reason}
end)
"""
defmacro after_transaction(callback, opts \\ []) do
{value, function} =
Spark.CodeHelpers.lift_functions(callback, :change_after_transaction, __CALLER__)
quote generated: true do
unquote(function)
{Ash.Resource.Change.AfterTransaction,
callback: unquote(value), prepend?: unquote(Keyword.get(opts, :prepend?, false))}
end
end
@doc ~S"""
Directly attach a `before_action` function to the current change.
See `Ash.Changeset.before_action/3` for more information.
Provide the option `append?: true` to place the hook after all other hooks instead of before.
## Example
change before_action(fn changeset, _context ->
Logger.debug("About to execute #{changeset.action.name} on #{inspect(changeset.resource)})
changeset
end)
"""
defmacro before_action(callback, opts \\ []) do
{value, function} =
Spark.CodeHelpers.lift_functions(callback, :change_before_action, __CALLER__)
quote generated: true do
unquote(function)
{Ash.Resource.Change.BeforeAction,
callback: unquote(value), append?: unquote(Keyword.get(opts, :append?, false))}
end
end
@doc ~S"""
Directly attach a `before_transaction` function to the current change.
See `Ash.Changeset.before_transaction/3` for more information.
Provide the option `append?: true` to place the hook after all other hooks instead of before.
## Example
change before_transaction(fn changeset, _context ->
Logger.debug("About to execute transaction for #{changeset.action.name} on #{inspect(changeset.resource)})
changeset
end)
"""
defmacro before_transaction(callback, opts \\ []) do
{value, function} =
Spark.CodeHelpers.lift_functions(callback, :change_before_transaction, __CALLER__)
quote generated: true do
unquote(function)
{Ash.Resource.Change.BeforeTransaction,
callback: unquote(value), append?: unquote(Keyword.get(opts, :append?, false))}
end
end
end