ash v1.29.0-rc1 Ash.Changeset View Source
Changesets are used to create and update data in Ash.
Create a changeset with new/1
or new/2
, and alter the attributes
and relationships using the functions provided in this module. Nothing in this module
actually incurs changes in a data layer. To commit a changeset, see Ash.Api.create/2
and Ash.Api.update/2
.
Primary Keys
For relationship manipulation using append_to_relationship/3
, remove_from_relationship/3
and replace_relationship/3
there are three types that can be used for primary keys:
1.) An instance of the resource in question.
2.) If the primary key is just a single field, i.e :id
, then a single value, i.e 1
3.) A map of keys to values representing the primary key, i.e %{id: 1}
or %{id: 1, org_id: 2}
Join Attributes
For many to many relationships, the attributes on a join relationship may be set while relating items by passing a tuple of the primary key and the changes to be applied. This is done via upserts, so update validations on the join resource are not applied, but create validations are.
For example:
Ash.Changeset.replace_relationship(changeset, :linked_tickets, [
{1, %{link_type: "blocking"}},
{a_ticket, %{link_type: "caused_by"}},
{%{id: 2}, %{link_type: "related_to"}}
])
Link to this section Summary
Functions
Adds an error to the changesets errors list, and marks the change as valid?: false
Adds an after_action hook to the changeset.
Appends a record or a list of records to a relationship. Stacks with previous removals/additions.
Returns the original data with attribute changes merged, if the changeset is valid.
Adds a before_action hook to the changeset.
Adds a change to the changeset, unless the value matches the existing value
Calls change_attribute/3
for each key/value pair provided
Change an attribute only if is not currently being changed
Change an attribute if is not currently being changed, by calling the provided function
Returns true if an attribute exists in the changes
Returns true if a relationship exists in the changes
Clears an attribute or relationship change off of the changeset
Remove an argument from the changeset
Gets the new value for an attribute, or :error
if it is not being changed
Constructs a changeset for a given create action, and validates it.
Constructs a changeset for a given destroy action, and validates it.
Constructs a changeset for a given update action, and validates it.
Changes an attribute even if it isn't writable
Calls force_change_attribute/3
for each key/value pair provided
Force change an attribute if is not currently being changed, by calling the provided function
Gets the value of an argument provided to the changeset
Gets the changing value or the original value of an attribute
Gets the original value for an attribute
Return a changeset over a resource or a record. params
can be either attributes, relationship values or arguments.
Removes a record or a list of records to a relationship. Stacks with previous removals/additions.
Replaces the value of a relationship. Any previous additions/removals are cleared.
Add an argument to the changeset, which will be provided to the action
Merge a map of arguments to the arguments list
Wraps a function in the before/after action hooks of a changeset.
Link to this section Types
Specs
t() :: %Ash.Changeset{ __validated_for_action__: term(), action: term(), action_failed?: term(), action_type: term(), after_action: term(), api: term(), arguments: term(), attributes: term(), before_action: term(), change_dependencies: term(), context: term(), data: term(), errors: term(), relationships: term(), requests: term(), resource: term(), tenant: term(), valid?: term() }
Link to this section Functions
Specs
Adds an error to the changesets errors list, and marks the change as valid?: false
Specs
after_action( t(), (t(), Ash.record() -> {:ok, Ash.record()} | {:ok, Ash.record(), [Ash.notification()]} | {:error, term()}) ) :: t()
Adds an after_action hook to the changeset.
append_to_relationship(changeset, relationship, record_or_records)
View SourceSpecs
append_to_relationship(t(), atom(), Ash.primary_key() | [Ash.primary_key()]) :: t()
Appends a record or a list of records to a relationship. Stacks with previous removals/additions.
Accepts a primary key or a list of primary keys. See the section on "Primary Keys" in the module documentation for more.
For many to many relationships, accepts changes for any join_attributes
configured on
the resource. See the section on "Join Attributes" in the module documentation for more.
Cannot be used with belongs_to
or has_one
relationships.
See replace_relationship/3
for manipulating belongs_to
and has_one
relationships.
Specs
apply_attributes(t()) :: {:ok, Ash.record()} | {:error, t()}
Returns the original data with attribute changes merged, if the changeset is valid.
Specs
Adds a before_action hook to the changeset.
Adds a change to the changeset, unless the value matches the existing value
Specs
Calls change_attribute/3
for each key/value pair provided
Specs
Change an attribute only if is not currently being changed
Specs
Change an attribute if is not currently being changed, by calling the provided function
Use this if you want to only perform some expensive calculation for an attribute value only if there isn't already a change for that attribute
Specs
Returns true if an attribute exists in the changes
Specs
Returns true if a relationship exists in the changes
Clears an attribute or relationship change off of the changeset
Remove an argument from the changeset
Specs
Gets the new value for an attribute, or :error
if it is not being changed
Constructs a changeset for a given create action, and validates it.
Params
params
may be attributes, relationships, or arguments. You can safely pass user/form input directly into this function.
Only public attributes and relationships are supported. If you want to change private attributes as well, see the
Customization section below.
Opts
:relationships
- customize relationship behavior. See the Relationships section below.:actor
- set the actor, which can be used in anyAsh.Resource.Change
s configured on the action. (in thecontext
argument)
Relationships
By default, any relationships are replaced via replace_relationship
. To change this behavior, provide the
relationships
option.
For example:
Ash.Changeset.for_create(MyResource, :create, params, relationships: [relationship: :append, other_relationship: :remove])
Customization
A changeset can be provided as the first argument, instead of a resource, to allow setting specific attributes ahead of time.
For example:
MyResource
|> Changeset.change_attribute(:foo, 1)
|> Changeset.for_create(:create, ...opts)
Constructs a changeset for a given destroy action, and validates it.
Pass an actor
option to specify the actor
Constructs a changeset for a given update action, and validates it.
See for_create/4
for more information
Specs
Changes an attribute even if it isn't writable
Specs
Calls force_change_attribute/3
for each key/value pair provided
Specs
Force change an attribute if is not currently being changed, by calling the provided function
See change_new_attribute_lazy/3
for more.
Specs
Gets the value of an argument provided to the changeset
Specs
Gets the changing value or the original value of an attribute
Specs
Gets the original value for an attribute
Specs
new(Ash.resource() | Ash.record(), params :: map()) :: t()
Return a changeset over a resource or a record. params
can be either attributes, relationship values or arguments.
This changeset does not consider an action, and so allows you to change things with minimal validation. Values are
validated when changed, and the existence of attributes and relationships are validated. If you want to essentially
"run an action", and get back a changeset with any errors that would be generated by that action (with the exception
of errors that can only be generated by the data layer), use for_action/2
.
Additionally, this format only supports supplying attributes in the params. This is because we don't know what the
behavior should be for relationship changes, nor what arguments are available. You can manage them yourself with
the functions that allow managing arguments/relationships that are provided in this module, e.g set_argument/3
and
replace_relationship/3
Specs
remove_from_relationship(changeset, relationship, record_or_records)
View SourceSpecs
remove_from_relationship(t(), atom(), Ash.primary_key() | [Ash.primary_key()]) :: t()
Removes a record or a list of records to a relationship. Stacks with previous removals/additions.
Accepts a primary key or a list of primary keys. See the section on "Primary Keys" in the module documentation for more.
Cannot be used with belongs_to
or has_one
relationships.
See replace_relationship/3
for manipulating those relationships.
Specs
replace_relationship(t(), atom(), Ash.primary_key() | [Ash.primary_key()] | nil) :: t()
Replaces the value of a relationship. Any previous additions/removals are cleared.
Accepts a primary key or a list of primary keys. See the section on "Primary Keys" in the module documentation for more.
For many to many relationships, accepts changes for any join_attributes
configured on
the resource. See the section on "Join Attributes" in the module documentation for more.
For a has_many
or many_to_many
relationship, this means removing any currently related
records that are not present in the replacement list, and creating any that do not exist
in the data layer.
For a belongs_to
or has_one
, replace with a nil
value to unset a relationship.
Add an argument to the changeset, which will be provided to the action
Merge a map of arguments to the arguments list
Specs
Specs
Specs
with_hooks( t(), (t() -> {:ok, Ash.record(), %{notifications: [Ash.notification()]}} | {:error, term()}) ) :: {:ok, term()} | {:error, term()}
Wraps a function in the before/after action hooks of a changeset.
The function takes a changeset and if it returns
{:ok, result}
, the result will be passed through the after
action hooks.