We recently launched atomic updates, which look like this:
update :update do
change atomic_update(:score, expr(score + 1))
end
# or when done manually
changeset
|> Ash.Changeset.atomic_update(:score, expr(score + 1))
This exact same syntax is now supported on create actions. It is currently ignored if done with a non-upsert action, and is only applied in the case of an upsert. You can think of the atomics as a way to control the update that is paired with the create for a given upsert. Here is an example of how you might use it:
# on a resource called PageView, for example
create :create_or_increment do
accept [:page]
upset? true
upsert_identity :unique_page
change set_attribute(:views, 1)
change atomic_update(:views, expr(views + 1))
end
The action above would create a record with page
set to the input value.
However, if a page view already exists with that name, it would increment its views by 1.
While this change is relatively small on its own, it is part of a progression of improvements that will make for a significant performance and quality of life improvement over time. More on this in a later update.
Enjoy!