ash v0.11.0 Ash.Changeset View Source
Changesets are used to create and update data in Ash.
Create a changeset with create/2
or update/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 of list of records to a relationship. Stacks with previous removals/additions.
Returns the original data with attribute changes merged.
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
Gets the new value for an attribute, or :error
if it is not being changed
Changes an attribute even if it isn't writable
Calls force_change_attribute/3
for each key/value pair provided
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
Removes a record of list of records to a relationship. Stacks with previous removals/additions.
Replaces the value of a relationship. Any previous additions/removals are cleared.
Wraps a function in the before/after action hooks of a changeset.
Link to this section Types
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()} | {: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 of 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()) :: Ash.record()
Returns the original data with attribute changes merged.
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
Gets the new value for an attribute, or :error
if it is not being changed
Specs
Changes an attribute even if it isn't writable
Specs
Calls force_change_attribute/3
for each key/value pair provided
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(), map()) :: t()
Return a changeset over a resource or a record
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 of 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()]) :: 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.
Specs
with_hooks(t(), (t() -> {:ok, Ash.record()} | {: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.