Search documentation of ash

# Ash (ash v3.4.71)

View Source

The primary interface to call actions and interact with resources.

# Summary

## Types

actor()
aggregate()
load_statement()
page_request()
record_or_records()
record_with_args()
resource_with_args()

## Functions

aggregate(query, aggregate_or_aggregates, opts \\ [])

Runs an aggregate or aggregates over a resource query

aggregate!(query, aggregate_or_aggregates, opts \\ [])

Runs an aggregate or aggregates over a resource query. See `aggregate/3` for more.

avg(query, field, opts \\ [])

Fetches the average of all values of a given field.

avg!(query, field, opts \\ [])

Fetches the average of all values of a given field or raises an error.

bulk_create(inputs, resource, action, opts \\ [])

Creates many records.

bulk_create!(inputs, resource, action, opts \\ [])

Creates many records, raising any errors that are returned. See `bulk_create/4` for more.

bulk_destroy(query_or_stream, action, input, opts \\ [])

Destroys all items in the provided enumerable or query with the provided input.

bulk_destroy!(stream_or_query, action, input, opts \\ [])

Destroys all items in the provided enumerable or query with the provided input.

bulk_update(query_or_stream, action, input, opts \\ [])

Updates all items in the provided enumerable or query with the provided input.

bulk_update!(stream_or_query, action, input, opts \\ [])

Updates all items in the provided enumerable or query with the provided input.

calculate(resource_or_record, calculation, opts \\ [])

Evaluates the calculation on the resource.

calculate!(resource_or_record, calculation, opts \\ [])

Evaluates the calculation on the resource or raises an error. See `calculate/3` for more.

calculate_opts()
can(action_or_query_or_changeset, actor, opts \\ [])

Returns whether or not the user can perform the action, or `:maybe`, returning any errors.

can?(action_or_query_or_changeset, actor, opts \\ [])

Returns whether or not the user can perform the action, or raises on errors.

context_to_opts(map, add_to \\ []) deprecated

See `Ash.Context.to_opts/2`.

count(query, opts \\ [])

Fetches the count of results that would be returned from a given query.

count!(query, opts \\ [])

Fetches the count of results that would be returned from a given query, or raises an error.

create(changeset_or_resource, params_or_opts \\ %{}, opts \\ [])

Create a record.

create!(changeset_or_resource, params \\ %{}, opts \\ [])

Create a record or raises an error. See `create/2` for more information.

destroy(changeset_or_record, opts \\ [])

Destroy a record.

destroy!(changeset_or_record, opts \\ [])

Destroy a record. See `destroy/2` for more information.

exists(query, opts \\ [])

Returns whether or not the query would return any results.

exists?(query, opts \\ [])

Returns whether or not the query would return any results, or raises an error.

first(query, field, opts \\ [])

Fetches the first value for a given field, or raises an error.

first!(query, field, opts \\ [])

Fetches the first value for a given field.

get(resource, id, opts \\ [])

Get a record by an identifier.

get!(resource, id, opts \\ [])

Get a record by an identifier, or raises an error. See `get/3` for more.

list(query, field, opts \\ [])

Fetches a list of all values of a given field.

list!(query, field, opts \\ [])

Fetches a list of all values of a given field or raises an error.

load(data, query, opts \\ [])

Load fields or relationships on already fetched records.

load!(data, query, opts \\ [])

Load fields or relationships on already fetched records. See `load/3` for more information.

max(query, field, opts \\ [])

Fetches the greatest of all values of a given field.

max!(query, field, opts \\ [])

Fetches the greatest of all values of a given field or raises an error.

min(query, field, opts \\ [])

Fetches the least of all values of a given field.

min!(query, field, opts \\ [])

Fetches the least of all values of a given field or raises an error.

page(page, n)

Fetch a page relative to the provided page.

page!(page, request)

Fetch a page relative to the provided page or raises an error

read(query, opts \\ [])

Runs an `Ash.Query`.

read!(query, opts \\ [])

Run an `Ash.Query`. See `read/2` for more.

read_first(query, opts \\ [])

Runs a query on a resource, returning a first result, nil, or an error.

read_first!(query, opts \\ [])

Runs an Ash query, returning the first result or nil, or raising an error. See `read_first/2` for more.

read_one(query, opts \\ [])

Runs a query on a resource, returning a single result, nil, or an error.

read_one!(query, opts \\ [])

Runs an ash query, returning a single result or raise an error. See `read_one/2` for more.

reload(record, opts \\ [])

Refetches a record by primary key. See `get/2` for more.

reload!(record, opts \\ [])

Refetches a record by primary key or raises an error. See `reload/2` for more.

run_action(input, opts \\ [])

Runs a generic action.

run_action!(input, opts \\ [])

Runs a generic action or raises an error. See `run_action/2` for more

stream!(query, opts \\ [])

Streams the results of a query.

sum(query, field, opts \\ [])

Fetches the sum of a given field.

sum!(query, field, opts \\ [])

Fetches the sum of a given field or raises an error.

update(changeset_or_record, params_or_opts \\ %{}, opts \\ [])

Update a record.

update!(changeset_or_record, params_or_opts \\ %{}, opts \\ [])

Update a record. See `update/2` for more information.

# Types

# actor()

    @type actor() :: any()

# aggregate()

    @type aggregate() ::
      Ash.Query.Aggregate.t()
      | {name :: atom(), kind :: atom()}
      | {name :: atom(), kind :: atom(), opts :: Keyword.t()}

# load_statement()

    @type load_statement() ::
      Ash.Query.t()
      | [atom()]
      | atom()
      | Keyword.t()
      | [atom() | {atom(), atom() | Keyword.t()}]

# page_request()

    @type page_request() :: :next | :prev | :first | :last | :self | integer()

# record_or_records()

    @type record_or_records() :: Ash.Resource.record() | [Ash.Resource.record()]

# record_with_args()

    @type record_with_args() :: {Ash.Resource.record(), map() | Keyword.t()}

# resource_with_args()

    @type resource_with_args() :: {Ash.Resource.t(), map() | Keyword.t()}

# Functions

# aggregate(query, aggregate_or_aggregates, opts \\ [])

    @spec aggregate(
      Ash.Query.t() | Ash.Resource.t(),
      aggregates :: aggregate() | [aggregate()],
      opts :: Keyword.t()
    ) :: {:ok, term()} | {:error, Ash.Error.t()}

Runs an aggregate or aggregates over a resource query

If you pass an `%Ash.Query.Aggregate{}`, gotten from `Ash.Query.Aggregate.new()`, the query provided as the first argument to this function will not apply. For this reason, it is preferred that you pass in the tuple format, i.e

Prefer this: `Api.aggregate(query, {:count_of_things, :count})`

Over this: `Api.aggregate(query, Ash.Query.Aggregate.new(...))`

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

# aggregate!(query, aggregate_or_aggregates, opts \\ [])

    @spec aggregate!(
      Ash.Query.t() | Ash.Resource.t(),
      aggregates :: aggregate() | [aggregate()],
      opts :: Keyword.t()
    ) :: term() | no_return()

Runs an aggregate or aggregates over a resource query. See `aggregate/3` for more.

# avg(query, field, opts \\ [])

Fetches the average of all values of a given field.

# avg!(query, field, opts \\ [])

Fetches the average of all values of a given field or raises an error.

# bulk_create(inputs, resource, action, opts \\ [])

    @spec bulk_create(
      Enumerable.t(map()),
      resource :: Ash.Resource.t(),
      action :: atom(),
      opts :: Keyword.t()
    ) ::
      Ash.BulkResult.t()
      | Enumerable.t(
          {:ok, Ash.Resource.record()}
          | {:error, Ash.Changeset.t() | Ash.Error.t()}
          | {:notification, Ash.Notifier.Notification.t()}
        )

Creates many records.

## Assumptions

We assume that the input is a list of changesets all for the same action, or a list of input maps for the same action with the `:resource` and `:action` option provided to illustrate which action it is for.

## Performance/Feasibility

The performance of this operation depends on the data layer in question. Data layers like AshPostgres will choose reasonable batch sizes in an attempt to handle large bulk actions, but that does not mean that you can pass a list of 500k inputs and expect things to go off without a hitch (although it might). If you need to do large data processing, you should look into projects like GenStage and Broadway. With that said, if you want to do things like support CSV upload and you place some reasonable limits on the size this is a great tool. You'll need to test it yourself, YMMV.

Passing `return_records?: true` can significantly increase the time it takes to perform the operation, and can also make the operation completely unreasonable due to the memory requirement. If you want to do very large bulk creates and display all of the results, the suggestion is to annotate them with a "bulk_create_id" in the data layer, and then read the records with that `bulk_create_id` so that they can be retrieved later if necessary.

## Changes/Validations

Changes will be applied in the order they are given on the actions as normal. Any change that exposes the `bulk_change` callbacks will be applied on the entire list.

## After Action Hooks

The following requirements must be met for `after_action` hooks to function properly. If they are not met, and an after_action hook being applied to a changeset in a `change`.

1. `return_records?` must be set to `true`.
2. The changeset must be setting the primary key as part of its changes, so that we know which result applies to which changeset.

It is possible to use `after_action` hooks with `bulk_change/3`, but you need to return the hooks along with the changesets. This allows for setting up `after_action` hooks that don't need access to the returned record, or `after_action` hooks that can operate on the entire list at once. See the documentation for that callback for more on how to do accomplish that.

## Options

- `:upsert?` (`boolean/0`) - If a conflict is found based on the primary key, the record is updated in the database (requires upsert support) The default value is `false`.

- `:upsert_identity` (`atom/0`) - The identity to use when detecting conflicts for `upsert?`, e.g. `upsert_identity: :full_name`. By default, the primary key is used. Has no effect if `upsert?: true` is not provided

- `:upsert_fields` - The fields to upsert. If not set, the action's `upsert_fields` is used. Unlike singular `create`, `bulk_create` with `upsert?` requires that `upsert_fields` be specified explicitly in one of these two locations.

- `:after_action` (function of arity 2) - An after_action hook to be added to each processed changeset

- `:upsert_condition` (`term/0`) - An expression to check if the record should be updated when there's a conflict.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

- `:return_notifications?` (`boolean/0`) - Use this if you're running ash actions in your own transaction and you want to manually handle sending notifications.
  If a transaction is ongoing, and this is false, notifications will be discarded, otherwise the return value is `{:ok, result, notifications}` (or `{:ok, notifications}`)
  To send notifications later, use `Ash.Notifier.notify(notifications)`. It sends any notifications that can be sent, and returns the rest. The default value is `false`.

- `:rollback_on_error?` (`boolean/0`) - Whether or not to rollback the transaction on error, if the resource is in a transaction.
  If the action has `transaction? false` this option has no effect. If an error is returned from the data layer and the resource is in a transaction, the transaction is always rolled back, regardless. The default value is `true`.

- `:notification_metadata` (`term/0`) - Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is `%{}`.

- `:read_action` (`atom/0`) - The action to use when building the read query.

- `:assume_casted?` (`boolean/0`) - Whether or not to cast attributes and arguments as input. This is an optimization for cases where the input is already casted and/or not in need of casting The default value is `false`.

- `:load` (`term/0`) - A load statement to apply to records. Ignored if `return_records?` is not true.

- `:select` (list of `atom/0`) - A select statement to apply to records. Ignored if `return_records?` is not true.

- `:authorize_query_with` - If set to `:error`, instead of filtering unauthorized query results, unauthorized query results will raise an appropriate forbidden error. Uses `authorize_with` if not set. Valid values are :filter, :error

- `:authorize_changeset_with` - If set to `:error`, instead of filtering unauthorized changes, unauthorized changes will raise an appropriate forbidden error. Uses `authorize_with` if not set. Valid values are :filter, :error

- `:authorize_with` - If set to `:error`, instead of filtering unauthorized query results, unauthorized query results will raise an appropriate forbidden error. Valid values are :filter, :error The default value is `:filter`.

- `:context` (`map/0`) - Context to set on each changeset

- `:sorted?` (`boolean/0`) - Whether or not to sort results by their input position, in cases where `return_records?: true` was provided. The default value is `false`.

- `:return_records?` (`boolean/0`) - Whether or not to return all of the records that were inserted. Defaults to false to account for large inserts. The default value is `false`.

- `:return_errors?` (`boolean/0`) - Whether or not to return all of the errors that occur. Defaults to false to account for large inserts. The default value is `false`.

- `:batch_size` (`pos_integer/0`) - The number of records to include in each batch. Defaults to the `default_limit` or `max_page_size` of the action, or 100.

- `:return_stream?` (`boolean/0`) - If set to `true`, instead of an `Ash.BulkResult`, a mixed stream is returned.
  Potential elements:
  `{:notification, notification}` - if `return_notifications?` is set to `true` `{:ok, record}` - if `return_records?` is set to `true` `{:error, error}` - an error that occurred. May be changeset or an invidual error. The default value is `false`.

- `:return_nothing?` (`boolean/0`) - Mutes warnings about returning nothing.
  Only relevant if `return_stream?` is set to `true` and all other `return_*?` options are set to `false`. The default value is `false`.

- `:stop_on_error?` (`boolean/0`) - If true, the first encountered error will stop the action and be returned. Otherwise, errors will be skipped. The default value is `false`.

- `:notify?` (`boolean/0`) - Whether or not to generate any notifications. If this is set to `true` then the data layer must return the results from each batch. This may be intensive for large bulk actions.
  Notifications will be automatically sent unless `return_notifications?` is set to `true`. The default value is `false`.

- `:transaction` - Whether or not to wrap the entire execution in a transaction, each batch, or not at all.
  Keep in mind:
  `before_transaction` and `after_transaction` hooks attached to changesets will have to be run inside the transaction if you choose `transaction: :all`. Valid values are :all, :batch, false The default value is `:batch`.

- `:max_concurrency` (`non_neg_integer/0`) - If set to a value greater than 0, up to that many tasks will be started to run batches asynchronously The default value is `0`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

# bulk_create!(inputs, resource, action, opts \\ [])

    @spec bulk_create!(Enumerable.t(map()), Ash.Resource.t(), atom(), Keyword.t()) ::
      Ash.BulkResult.t() | no_return()

Creates many records, raising any errors that are returned. See `bulk_create/4` for more.

# bulk_destroy(query_or_stream, action, input, opts \\ [])

    @spec bulk_destroy(
      Enumerable.t(Ash.Resource.record()) | Ash.Query.t(),
      atom(),
      input :: map(),
      Keyword.t()
    ) :: Ash.BulkResult.t()

Destroys all items in the provided enumerable or query with the provided input.

The input is a map of valid inputs for the action. The input will be applied to all records in the enumerable/query.

If the data layer supports destroying from a query, and the destroy action can be done fully atomically, it will be updated in a single pass using the data layer.

Otherwise, this will stream each record and update it.

## Options

- `:resource` (`Ash.Resource`) - The resource being destroyed. This must be provided if the input given is a stream, so we know ahead of time what the resource being updated is.

- `:stream_batch_size` (`integer/0`) - Batch size to use if provided a query and the query must be streamed

- `:authorize_query?` (`boolean/0`) - If a query is given, determines whether or not authorization is run on that query. The default value is `true`.

- `:strategy` - The strategy or strategies to enable. :stream is used in all cases if the data layer does not support atomics. Valid values are :atomic, :atomic_batches, :stream The default value is `:atomic`.

- `:filter` (`term/0`) - A filter to apply to records. This is also applied to a stream of inputs.

- `:allow_stream_with` - The 'worst' strategy allowed to be used to fetch records. See `Ash.stream!/2` docs for more. Valid values are :keyset, :offset, :full_read The default value is `:keyset`.

- `:stream_with` - The specific strategy to use to fetch records. See `Ash.stream!/2` docs for more. Valid values are :keyset, :offset, :full_read

- `:lock` (`term/0`) - A lock statement to add onto the query

- `:return_query?` (`boolean/0`) - If `true`, the query that was ultimately used is returned as a third tuple element.
  The query goes through many potential changes during a request, potentially adding authorization filters, or replacing relationships for other data layers with their corresponding ids. This option can be used to get the true query that was sent to the data layer. The default value is `false`.

- `:reuse_values?` (`boolean/0`) - Whether calculations are allowed to reuse values that have already been loaded, or must refetch them from the data layer. The default value is `false`.

- `:strict?` (`boolean/0`) - If set to true, only specified attributes will be loaded when passing a list of fields to fetch on a relationship, which allows for more optimized data-fetching.
  See `Ash.Query.load/2`. The default value is `false`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

- `:return_notifications?` (`boolean/0`) - Use this if you're running ash actions in your own transaction and you want to manually handle sending notifications.
  If a transaction is ongoing, and this is false, notifications will be discarded, otherwise the return value is `{:ok, result, notifications}` (or `{:ok, notifications}`)
  To send notifications later, use `Ash.Notifier.notify(notifications)`. It sends any notifications that can be sent, and returns the rest. The default value is `false`.

- `:rollback_on_error?` (`boolean/0`) - Whether or not to rollback the transaction on error, if the resource is in a transaction.
  If the action has `transaction? false` this option has no effect. If an error is returned from the data layer and the resource is in a transaction, the transaction is always rolled back, regardless. The default value is `true`.

- `:notification_metadata` (`term/0`) - Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is `%{}`.

- `:read_action` (`atom/0`) - The action to use when building the read query.

- `:assume_casted?` (`boolean/0`) - Whether or not to cast attributes and arguments as input. This is an optimization for cases where the input is already casted and/or not in need of casting The default value is `false`.

- `:load` (`term/0`) - A load statement to apply to records. Ignored if `return_records?` is not true.

- `:select` (list of `atom/0`) - A select statement to apply to records. Ignored if `return_records?` is not true.

- `:authorize_query_with` - If set to `:error`, instead of filtering unauthorized query results, unauthorized query results will raise an appropriate forbidden error. Uses `authorize_with` if not set. Valid values are :filter, :error

- `:authorize_changeset_with` - If set to `:error`, instead of filtering unauthorized changes, unauthorized changes will raise an appropriate forbidden error. Uses `authorize_with` if not set. Valid values are :filter, :error

- `:authorize_with` - If set to `:error`, instead of filtering unauthorized query results, unauthorized query results will raise an appropriate forbidden error. Valid values are :filter, :error The default value is `:filter`.

- `:context` (`map/0`) - Context to set on each changeset

- `:sorted?` (`boolean/0`) - Whether or not to sort results by their input position, in cases where `return_records?: true` was provided. The default value is `false`.

- `:return_records?` (`boolean/0`) - Whether or not to return all of the records that were inserted. Defaults to false to account for large inserts. The default value is `false`.

- `:return_errors?` (`boolean/0`) - Whether or not to return all of the errors that occur. Defaults to false to account for large inserts. The default value is `false`.

- `:batch_size` (`pos_integer/0`) - The number of records to include in each batch. Defaults to the `default_limit` or `max_page_size` of the action, or 100.

- `:return_stream?` (`boolean/0`) - If set to `true`, instead of an `Ash.BulkResult`, a mixed stream is returned.
  Potential elements:
  `{:notification, notification}` - if `return_notifications?` is set to `true` `{:ok, record}` - if `return_records?` is set to `true` `{:error, error}` - an error that occurred. May be changeset or an invidual error. The default value is `false`.

- `:return_nothing?` (`boolean/0`) - Mutes warnings about returning nothing.
  Only relevant if `return_stream?` is set to `true` and all other `return_*?` options are set to `false`. The default value is `false`.

- `:stop_on_error?` (`boolean/0`) - If true, the first encountered error will stop the action and be returned. Otherwise, errors will be skipped. The default value is `false`.

- `:notify?` (`boolean/0`) - Whether or not to generate any notifications. If this is set to `true` then the data layer must return the results from each batch. This may be intensive for large bulk actions.
  Notifications will be automatically sent unless `return_notifications?` is set to `true`. The default value is `false`.

- `:transaction` - Whether or not to wrap the entire execution in a transaction, each batch, or not at all.
  Keep in mind:
  `before_transaction` and `after_transaction` hooks attached to changesets will have to be run inside the transaction if you choose `transaction: :all`. Valid values are :all, :batch, false The default value is `:batch`.

- `:max_concurrency` (`non_neg_integer/0`) - If set to a value greater than 0, up to that many tasks will be started to run batches asynchronously The default value is `0`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

# bulk_destroy!(stream_or_query, action, input, opts \\ [])

    @spec bulk_destroy!(
      Enumerable.t(Ash.Resource.record()) | Ash.Query.t(),
      action :: atom(),
      input :: map(),
      opts :: Keyword.t()
    ) :: Ash.BulkResult.t() | no_return()

Destroys all items in the provided enumerable or query with the provided input.

See `bulk_destroy/4` for more.

# bulk_update(query_or_stream, action, input, opts \\ [])

    @spec bulk_update(
      Enumerable.t(Ash.Resource.record()) | Ash.Query.t(),
      atom(),
      input :: map(),
      Keyword.t()
    ) :: Ash.BulkResult.t()

Updates all items in the provided enumerable or query with the provided input.

The input is a map of valid inputs for the action. The input will be applied to all records in the enumerable/query.

If the data layer supports updating from a query, and the update action can be done fully atomically, it will be updated in a single pass using the data layer.

Otherwise, this will stream each record and update it.

## Options

- `:resource` (`Ash.Resource`) - The resource being updated. This must be provided if the input given is a stream, so we know ahead of time what the resource being updated is.

- `:atomic_update` (`map/0`) - A map of atomic updates to apply. See `Ash.Changeset.atomic_update/3` for more.

- `:stream_batch_size` (`integer/0`) - Batch size to use if provided a query and the query must be streamed

- `:authorize_query?` (`boolean/0`) - If a query is given, determines whether or not authorization is run on that query. The default value is `true`.

- `:filter` (`term/0`) - A filter to apply to records. This is also applied to a stream of inputs.

- `:strategy` - The strategy or strategies to enable. :stream is used in all cases if the data layer does not support atomics. Valid values are :atomic, :atomic_batches, :stream The default value is `[:atomic]`.

- `:allow_stream_with` - The 'worst' strategy allowed to be used to fetch records. See `Ash.stream!/2` docs for more. Valid values are :keyset, :offset, :full_read The default value is `:keyset`.

- `:stream_with` - The specific strategy to use to fetch records. See `Ash.stream!/2` docs for more. Valid values are :keyset, :offset, :full_read

- `:lock` (`term/0`) - A lock statement to add onto the query

- `:return_query?` (`boolean/0`) - If `true`, the query that was ultimately used is returned as a third tuple element.
  The query goes through many potential changes during a request, potentially adding authorization filters, or replacing relationships for other data layers with their corresponding ids. This option can be used to get the true query that was sent to the data layer. The default value is `false`.

- `:reuse_values?` (`boolean/0`) - Whether calculations are allowed to reuse values that have already been loaded, or must refetch them from the data layer. The default value is `false`.

- `:strict?` (`boolean/0`) - If set to true, only specified attributes will be loaded when passing a list of fields to fetch on a relationship, which allows for more optimized data-fetching.
  See `Ash.Query.load/2`. The default value is `false`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

- `:return_notifications?` (`boolean/0`) - Use this if you're running ash actions in your own transaction and you want to manually handle sending notifications.
  If a transaction is ongoing, and this is false, notifications will be discarded, otherwise the return value is `{:ok, result, notifications}` (or `{:ok, notifications}`)
  To send notifications later, use `Ash.Notifier.notify(notifications)`. It sends any notifications that can be sent, and returns the rest. The default value is `false`.

- `:rollback_on_error?` (`boolean/0`) - Whether or not to rollback the transaction on error, if the resource is in a transaction.
  If the action has `transaction? false` this option has no effect. If an error is returned from the data layer and the resource is in a transaction, the transaction is always rolled back, regardless. The default value is `true`.

- `:notification_metadata` (`term/0`) - Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is `%{}`.

- `:read_action` (`atom/0`) - The action to use when building the read query.

- `:assume_casted?` (`boolean/0`) - Whether or not to cast attributes and arguments as input. This is an optimization for cases where the input is already casted and/or not in need of casting The default value is `false`.

- `:load` (`term/0`) - A load statement to apply to records. Ignored if `return_records?` is not true.

- `:select` (list of `atom/0`) - A select statement to apply to records. Ignored if `return_records?` is not true.

- `:authorize_query_with` - If set to `:error`, instead of filtering unauthorized query results, unauthorized query results will raise an appropriate forbidden error. Uses `authorize_with` if not set. Valid values are :filter, :error

- `:authorize_changeset_with` - If set to `:error`, instead of filtering unauthorized changes, unauthorized changes will raise an appropriate forbidden error. Uses `authorize_with` if not set. Valid values are :filter, :error

- `:authorize_with` - If set to `:error`, instead of filtering unauthorized query results, unauthorized query results will raise an appropriate forbidden error. Valid values are :filter, :error The default value is `:filter`.

- `:context` (`map/0`) - Context to set on each changeset

- `:sorted?` (`boolean/0`) - Whether or not to sort results by their input position, in cases where `return_records?: true` was provided. The default value is `false`.

- `:return_records?` (`boolean/0`) - Whether or not to return all of the records that were inserted. Defaults to false to account for large inserts. The default value is `false`.

- `:return_errors?` (`boolean/0`) - Whether or not to return all of the errors that occur. Defaults to false to account for large inserts. The default value is `false`.

- `:batch_size` (`pos_integer/0`) - The number of records to include in each batch. Defaults to the `default_limit` or `max_page_size` of the action, or 100.

- `:return_stream?` (`boolean/0`) - If set to `true`, instead of an `Ash.BulkResult`, a mixed stream is returned.
  Potential elements:
  `{:notification, notification}` - if `return_notifications?` is set to `true` `{:ok, record}` - if `return_records?` is set to `true` `{:error, error}` - an error that occurred. May be changeset or an invidual error. The default value is `false`.

- `:return_nothing?` (`boolean/0`) - Mutes warnings about returning nothing.
  Only relevant if `return_stream?` is set to `true` and all other `return_*?` options are set to `false`. The default value is `false`.

- `:stop_on_error?` (`boolean/0`) - If true, the first encountered error will stop the action and be returned. Otherwise, errors will be skipped. The default value is `false`.

- `:notify?` (`boolean/0`) - Whether or not to generate any notifications. If this is set to `true` then the data layer must return the results from each batch. This may be intensive for large bulk actions.
  Notifications will be automatically sent unless `return_notifications?` is set to `true`. The default value is `false`.

- `:transaction` - Whether or not to wrap the entire execution in a transaction, each batch, or not at all.
  Keep in mind:
  `before_transaction` and `after_transaction` hooks attached to changesets will have to be run inside the transaction if you choose `transaction: :all`. Valid values are :all, :batch, false The default value is `:batch`.

- `:max_concurrency` (`non_neg_integer/0`) - If set to a value greater than 0, up to that many tasks will be started to run batches asynchronously The default value is `0`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

# bulk_update!(stream_or_query, action, input, opts \\ [])

    @spec bulk_update!(
      Enumerable.t(Ash.Resource.record()) | Ash.Query.t(),
      action :: atom(),
      input :: map(),
      opts :: Keyword.t()
    ) :: Ash.BulkResult.t() | no_return()

Updates all items in the provided enumerable or query with the provided input.

See `bulk_update/4` for more.

# calculate(resource_or_record, calculation, opts \\ [])

    @spec calculate(
      resource_or_record :: Ash.Resource.t() | Ash.Resource.record(),
      calculation :: atom(),
      opts :: Keyword.t()
    ) :: {:ok, term()} | {:error, term()}

Evaluates the calculation on the resource.

If a record is provided, its field values will be used to evaluate the calculation.

- `:args` (`map/0`) - Values for arguments referenced by the calculation. The default value is `%{}`.

- `:refs` (`map/0`) - Values for references used by the calculation. The default value is `%{}`.

- `:actor` (`term/0`) - The actor for handling `^actor/1` templates, supplied to calculation context.

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - The tenant, supplied to calculation context.

- `:authorize?` (`boolean/0`) - Whether or not the request is being authorized, provided to calculation context. The default value is `true`.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer, provided to the calculation context.

- `:record` (`term/0`) - A record to use as the base of the calculation

- `:data_layer?` (`boolean/0`) - Set to `true` to require that the value be computed within the data layer. Only works for calculations that define an expression.

- `:reuse_values?` (`boolean/0`) - Set to `true` to reuse existing values on any provided record. Only necessary if providing a record as the basis for calculation. The default value is `false`.

- `:domain` (`Ash.Domain`) - The domain to use for the action

# calculate!(resource_or_record, calculation, opts \\ [])

    @spec calculate!(
      resource_or_record :: Ash.Resource.t() | Ash.Resource.record(),
      calculation :: atom(),
      opts :: Keyword.t()
    ) :: term() | no_return()

Evaluates the calculation on the resource or raises an error. See `calculate/3` for more.

# calculate_opts()

# can(action_or_query_or_changeset, actor, opts \\ [])

    @spec can(Ash.Can.subject(), actor(), Keyword.t()) ::
      {:ok, boolean() | :maybe}
      | {:ok, true, Ash.Changeset.t() | Ash.Query.t()}
      | {:ok, true, Ash.Changeset.t(), Ash.Query.t()}
      | {:ok, false, Exception.t()}
      | {:error, term()}

Returns whether or not the user can perform the action, or `:maybe`, returning any errors.

In cases with "runtime" checks (checks after the action), we may not be able to determine an answer, and so the value `:maybe` will be returned from `can/2`. The `can?` function assumes that `:maybe` means `true`. Keep in mind, this is just for doing things like "can they do this" in a UI, so assuming `:maybe` is `true` is fine. The actual action invocation will be properly checked regardless. If you have runtime checks, you may need to use `can` instead of `can?`, or configure what `:maybe` means.

### Accepted inputs

You can pass many different inputs as the subject to `can/3`.

    # Can this user run this query.
    Ash.Query.t()

    # Can this user run this changeset.
    Ash.Changeset.t()

    # Can this user run this action.
    Ash.ActionInput.t()

    # Can this user run this action.
    {Ash.Resource.t(), :action}

    # Can this user run this action.
    {Ash.Resource.t(), %Action{}}

    # Can this user run this action with this input.
    {Ash.Resource.t(), :atom, %{...input}}

    # Can this user run this action with this input.
    {Ash.Resource.t(), %Action{}, %{...input}}

### Options

- `:maybe_is` (`term/0`) - If the actor may be able to perform the action, what value should be returned. The default value is `:maybe`.

- `:filter_with` - If set to `:error`, the query will raise an error on a match. If set to `:filter` the query will filter out unauthorized access. Valid values are :filter, :error The default value is `:filter`.

- `:validate?` (`boolean/0`) - Whether or not to treat an invalid action as a non-allowed action. The default value is `false`.

- `:reuse_values?` (`boolean/0`) - Whether or not loaded data like aggregates, calculations and relationships should be checked in memory if possible, instead of querying. No effect if `pre_flight?` is `false`. The default value is `false`.

- `:pre_flight?` (`boolean/0`) - Whether or not this is a pre_flight check (which may perform optimized in-memory checks) or the final proper check. The default value is `true`.

- `:run_queries?` (`boolean/0`) - Whether or not to run queries. If set to `true`, `:maybe` will not be returned. The default value is `true`.

- `:data` - The record or records specifically attempting to be acted upon.

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - The tenant to use for authorization

- `:alter_source?` (`boolean/0`) - If set to `true`, the source being authorized is returned so it can be run. The default value is `false`.

- `:base_query` (`term/0`) - A base query on which to apply an generated filters

- `:no_check?` (`boolean/0`) - Whether or not authorization must pass at the strict/filter step, or if post-checks are allowed to be run The default value is `false`.

- `:on_must_pass_strict_check` (`term/0`) - Override the value returned when `no_check?` is `true` but a check must be run.

- `:atomic_changeset` (`term/0`) - A base query on which to apply an generated filters

- `:return_forbidden_error?` (`boolean/0`) - Whether or not to return a forbidden error in cases of not being authorized. The default value is `false`.

# can?(action_or_query_or_changeset, actor, opts \\ [])

    @spec can?(Ash.Can.subject(), actor(), Keyword.t()) :: boolean() | no_return()

Returns whether or not the user can perform the action, or raises on errors.

Calls `can/3` with a `maybe_is: true`. See `can/3` for more info.

### Options

- `:maybe_is` (`term/0`) - If the actor may be able to perform the action, what value should be returned. The default value is `true`.

- `:filter_with` - If set to `:error`, the query will raise an error on a match. If set to `:filter` the query will filter out unauthorized access. Valid values are :filter, :error The default value is `:filter`.

- `:validate?` (`boolean/0`) - Whether or not to treat an invalid action as a non-allowed action. The default value is `false`.

- `:reuse_values?` (`boolean/0`) - Whether or not loaded data like aggregates, calculations and relationships should be checked in memory if possible, instead of querying. No effect if `pre_flight?` is `false`. The default value is `false`.

- `:pre_flight?` (`boolean/0`) - Whether or not this is a pre_flight check (which may perform optimized in-memory checks) or the final proper check. The default value is `true`.

- `:run_queries?` (`boolean/0`) - Whether or not to run queries. If set to `true`, `:maybe` will not be returned. The default value is `true`.

- `:data` - The record or records specifically attempting to be acted upon.

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - The tenant to use for authorization

- `:alter_source?` (`boolean/0`) - If set to `true`, the source being authorized is returned so it can be run. The default value is `false`.

- `:base_query` (`term/0`) - A base query on which to apply an generated filters

- `:no_check?` (`boolean/0`) - Whether or not authorization must pass at the strict/filter step, or if post-checks are allowed to be run The default value is `false`.

- `:on_must_pass_strict_check` (`term/0`) - Override the value returned when `no_check?` is `true` but a check must be run.

- `:atomic_changeset` (`term/0`) - A base query on which to apply an generated filters

- `:return_forbidden_error?` (`boolean/0`) - Whether or not to return a forbidden error in cases of not being authorized. The default value is `false`.

# context_to_opts(map, add_to \\ [])

This function is deprecated. Converts a context map to opts to be passed into an action. .

See `Ash.Context.to_opts/2`.

# count(query, opts \\ [])

Fetches the count of results that would be returned from a given query.

# count!(query, opts \\ [])

Fetches the count of results that would be returned from a given query, or raises an error.

# create(changeset_or_resource, params_or_opts \\ %{}, opts \\ [])

    @spec create(
      changset_or_resource :: Ash.Changeset.t() | Ash.Resource.t(),
      params_or_opts :: map() | Keyword.t(),
      opts :: Keyword.t()
    ) ::
      {:ok, Ash.Resource.record()}
      | {:ok, Ash.Resource.record(), [Ash.Notifier.Notification.t()]}
      | {:error, term()}

Create a record.

- `:upsert?` (`boolean/0`) - If a conflict is found based on the primary key, the record is updated in the database (requires upsert support) The default value is `false`.

- `:return_skipped_upsert?` (`boolean/0`) - If `true`, and a record was not upserted because its filter prevented the upsert, the original record (which was not upserted) will be returned. The default value is `false`.

- `:upsert_identity` (`atom/0`) - The identity to use when detecting conflicts for `upsert?`, e.g. `upsert_identity: :full_name`. By default, the primary key is used. Has no effect if `upsert?: true` is not provided

- `:upsert_fields` - The fields to upsert. If not set, the action's upsert_fields is used, and if that is not set, then any fields not being set to defaults are written.

- `:upsert_condition` (`term/0`) - An expression to check if the record should be updated when there's a conflict.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

- `:return_notifications?` (`boolean/0`) - Use this if you're running ash actions in your own transaction and you want to manually handle sending notifications.
  If a transaction is ongoing, and this is false, notifications will be discarded, otherwise the return value is `{:ok, result, notifications}` (or `{:ok, notifications}`)
  To send notifications later, use `Ash.Notifier.notify(notifications)`. It sends any notifications that can be sent, and returns the rest. The default value is `false`.

- `:rollback_on_error?` (`boolean/0`) - Whether or not to rollback the transaction on error, if the resource is in a transaction.
  If the action has `transaction? false` this option has no effect. If an error is returned from the data layer and the resource is in a transaction, the transaction is always rolled back, regardless. The default value is `true`.

- `:notification_metadata` (`term/0`) - Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is `%{}`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

- `:load` (`term/0`) - A load statement to add onto the changeset

# create!(changeset_or_resource, params \\ %{}, opts \\ [])

    @spec create!(
      changset_or_resource :: Ash.Changeset.t() | Ash.Resource.t(),
      params_or_opts :: map() | Keyword.t(),
      opts :: Keyword.t()
    ) ::
      Ash.Resource.record()
      | {Ash.Resource.record(), [Ash.Notifier.Notification.t()]}
      | no_return()

Create a record or raises an error. See `create/2` for more information.

# destroy(changeset_or_record, opts \\ [])

    @spec destroy(Ash.Changeset.t() | Ash.Resource.record(), opts :: Keyword.t()) ::
      :ok
      | {:ok, Ash.Resource.record()}
      | {:ok, [Ash.Notifier.Notification.t()]}
      | {:ok, Ash.Resource.record(), [Ash.Notifier.Notification.t()]}
      | {:error, term()}

Destroy a record.

- `:return_destroyed?` (`boolean/0`) - If true, the destroyed record is included in the return result, e.g `{:ok, destroyed}` or `{:ok, destroyed, notifications}` The default value is `false`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

- `:return_notifications?` (`boolean/0`) - Use this if you're running ash actions in your own transaction and you want to manually handle sending notifications.
  If a transaction is ongoing, and this is false, notifications will be discarded, otherwise the return value is `{:ok, result, notifications}` (or `{:ok, notifications}`)
  To send notifications later, use `Ash.Notifier.notify(notifications)`. It sends any notifications that can be sent, and returns the rest. The default value is `false`.

- `:rollback_on_error?` (`boolean/0`) - Whether or not to rollback the transaction on error, if the resource is in a transaction.
  If the action has `transaction? false` this option has no effect. If an error is returned from the data layer and the resource is in a transaction, the transaction is always rolled back, regardless. The default value is `true`.

- `:notification_metadata` (`term/0`) - Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is `%{}`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

- `:load` (`term/0`) - A load statement to add onto the changeset

# destroy!(changeset_or_record, opts \\ [])

    @spec destroy!(Ash.Changeset.t() | Ash.Resource.record(), opts :: Keyword.t()) ::
      :ok
      | Ash.Resource.record()
      | [Ash.Notifier.Notification.t()]
      | {Ash.Resource.record(), [Ash.Notifier.Notification.t()]}
      | no_return()

Destroy a record. See `destroy/2` for more information.

# exists(query, opts \\ [])

Returns whether or not the query would return any results.

# exists?(query, opts \\ [])

Returns whether or not the query would return any results, or raises an error.

# first(query, field, opts \\ [])

Fetches the first value for a given field, or raises an error.

# first!(query, field, opts \\ [])

Fetches the first value for a given field.

# get(resource, id, opts \\ [])

    @spec get(Ash.Resource.t(), term(), Keyword.t()) ::
      {:ok, Ash.Resource.record()} | {:error, term()}

Get a record by an identifier.

For a resource with a composite primary key, pass a keyword list or map, e.g `Ash.get(MyResource, %{first_key: 1, second_key: 2})`

Additionally, a keyword list or map of keys matching an identity can be provided.

- `:error?` (`boolean/0`) - Whether or not an error should be returned or raised when the record is not found. If set to false, `nil` will be returned. The default value is `true`.

- `:load` (`term/0`) - Fields or relationships to load in the query. See `Ash.Query.load/2`

- `:lock` (`term/0`) - A lock statement to add onto the query

- `:reuse_values?` (`boolean/0`) - Whether calculations are allowed to reuse values that have already been loaded, or must refetch them from the data layer. The default value is `false`.

- `:strict?` (`boolean/0`) - If set to true, only specified attributes will be loaded when passing a list of fields to fetch on a relationship, which allows for more optimized data-fetching.
  See `Ash.Query.load/2`. The default value is `false`.

- `:authorize_with` - If set to `:error`, instead of applying authorization filters as a filter, any records not matching the authroization filter will cause an error to be returned. Valid values are :filter, :error The default value is `:filter`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

# get!(resource, id, opts \\ [])

    @spec get!(Ash.Resource.t(), term(), Keyword.t()) ::
      Ash.Resource.record() | no_return()

Get a record by an identifier, or raises an error. See `get/3` for more.

# list(query, field, opts \\ [])

Fetches a list of all values of a given field.

# list!(query, field, opts \\ [])

Fetches a list of all values of a given field or raises an error.

# load(data, query, opts \\ [])

    @spec load(
      record_or_records :: Ash.Resource.record() | [Ash.Resource.record()],
      query :: load_statement(),
      opts :: Keyword.t()
    ) :: {:ok, Ash.Resource.record() | [Ash.Resource.record()]} | {:error, term()}

Load fields or relationships on already fetched records.

Accepts a list of non-loaded fields and loads them on the provided records or a query, in which case the loaded fields of the query are used. Relationship loads can be nested, for example: `Ash.load(record, [posts: [:comments]])`.

- `:lazy?` (`boolean/0`) - If set to true, values will only be loaded if the related value isn't currently loaded. The default value is `false`.

- `:reuse_values?` (`boolean/0`) - Whether calculations are allowed to reuse values that have already been loaded, or must refetch them from the data layer. The default value is `false`.

- `:strict?` (`boolean/0`) - If set to true, only specified attributes will be loaded when passing a list of fields to fetch on a relationship, which allows for more optimized data-fetching.
  See `Ash.Query.load/2`. The default value is `false`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

# load!(data, query, opts \\ [])

    @spec load!(
      record_or_records ::
        record_or_records()
        | {:ok, record_or_records()}
        | :error
        | {:error, term()}
        | :ok
        | Ash.Page.page(),
      query :: load_statement(),
      opts :: Keyword.t()
    ) :: Ash.Resource.record() | [Ash.Resource.record()] | no_return()

Load fields or relationships on already fetched records. See `load/3` for more information.

# max(query, field, opts \\ [])

Fetches the greatest of all values of a given field.

# max!(query, field, opts \\ [])

Fetches the greatest of all values of a given field or raises an error.

# min(query, field, opts \\ [])

Fetches the least of all values of a given field.

# min!(query, field, opts \\ [])

Fetches the least of all values of a given field or raises an error.

# page(page, n)

    @spec page(Ash.Page.page(), page_request()) ::
      {:ok, Ash.Page.page()} | {:error, Ash.Error.t()}

Fetch a page relative to the provided page.

# page!(page, request)

    @spec page!(Ash.Page.page(), page_request()) :: Ash.Page.page() | no_return()

Fetch a page relative to the provided page or raises an error

# read(query, opts \\ [])

    @spec read(Ash.Query.t() | Ash.Resource.t(), Keyword.t()) ::
      {:ok, [Ash.Resource.record()] | Ash.Page.page()} | {:error, term()}

Runs an `Ash.Query`.

For more information on building a query, see `Ash.Query`.

- `:page` - Pagination options, see `Ash.read/2` for more.

- `:load` (`term/0`) - A load statement to add onto the query

- `:max_concurrency` (`non_neg_integer/0`) - The maximum number of processes allowed to be started for parallel loading of relationships and calculations. Defaults to `System.schedulers_online() * 2`

- `:lock` (`term/0`) - A lock statement to add onto the query

- `:return_query?` (`boolean/0`) - If `true`, the query that was ultimately used is returned as a third tuple element.
  The query goes through many potential changes during a request, potentially adding authorization filters, or replacing relationships for other data layers with their corresponding ids. This option can be used to get the true query that was sent to the data layer. The default value is `false`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

- `:reuse_values?` (`boolean/0`) - Whether calculations are allowed to reuse values that have already been loaded, or must refetch them from the data layer. The default value is `false`.

- `:strict?` (`boolean/0`) - If set to true, only specified attributes will be loaded when passing a list of fields to fetch on a relationship, which allows for more optimized data-fetching.
  See `Ash.Query.load/2`. The default value is `false`.

- `:authorize_with` - If set to `:error`, instead of applying authorization filters as a filter, any records not matching the authroization filter will cause an error to be returned. Valid values are :filter, :error The default value is `:filter`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

## Pagination

#### Limit/offset pagination

- `:offset` (`non_neg_integer/0`) - The number of records to skip from the beginning of the query

- `:limit` (`pos_integer/0`) - The number of records to include in the page

- `:filter` (`term/0`) - A filter to apply for pagination purposes, that should not be considered in the full count.
  This is used by the liveview paginator to only fetch the records that were already on the page when refreshing data, to avoid pages jittering.

- `:count` (`boolean/0`) - Whether or not to return the page with a full count of all records

#### Keyset pagination

- `:before` (`String.t/0`) - Get records that appear before the provided keyset (mutually exclusive with `after`)

- `:after` (`String.t/0`) - Get records that appear after the provided keyset (mutually exclusive with `before`)

- `:limit` (`pos_integer/0`) - How many records to include in the page

- `:filter` (`term/0`) - See the `filter` option for offset pagination, this behaves the same.

- `:count` (`boolean/0`) - Whether or not to return the page with a full count of all records

# read!(query, opts \\ [])

    @spec read!(Ash.Query.t() | Ash.Resource.t(), Keyword.t()) ::
      [Ash.Resource.record()] | Ash.Page.page() | no_return()

Run an `Ash.Query`. See `read/2` for more.

# read_first(query, opts \\ [])

    @spec read_first(
      resource_or_query :: Ash.Query.t() | Ash.Resource.t(),
      opts :: Keyword.t()
    ) ::
      {:ok, Ash.Resource.record() | nil} | {:error, Ash.Error.t()}

Runs a query on a resource, returning a first result, nil, or an error.

Query is automatically limited to only return one result, unlike `read_one/3`

## Options

- `:not_found_error?` (`boolean/0`) - Whether or not to return an `Ash.Error.Query.NotFound` if no record is found. The default value is `false`.

- `:page` - Pagination options, see `Ash.read/2` for more.

- `:load` (`term/0`) - A load statement to add onto the query

- `:max_concurrency` (`non_neg_integer/0`) - The maximum number of processes allowed to be started for parallel loading of relationships and calculations. Defaults to `System.schedulers_online() * 2`

- `:lock` (`term/0`) - A lock statement to add onto the query

- `:return_query?` (`boolean/0`) - If `true`, the query that was ultimately used is returned as a third tuple element.
  The query goes through many potential changes during a request, potentially adding authorization filters, or replacing relationships for other data layers with their corresponding ids. This option can be used to get the true query that was sent to the data layer. The default value is `false`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

- `:reuse_values?` (`boolean/0`) - Whether calculations are allowed to reuse values that have already been loaded, or must refetch them from the data layer. The default value is `false`.

- `:strict?` (`boolean/0`) - If set to true, only specified attributes will be loaded when passing a list of fields to fetch on a relationship, which allows for more optimized data-fetching.
  See `Ash.Query.load/2`. The default value is `false`.

- `:authorize_with` - If set to `:error`, instead of applying authorization filters as a filter, any records not matching the authroization filter will cause an error to be returned. Valid values are :filter, :error The default value is `:filter`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

# read_first!(query, opts \\ [])

    @spec read_first!(
      resource_or_query :: Ash.Query.t() | Ash.Resource.t(),
      opts :: Keyword.t()
    ) ::
      Ash.Resource.record() | nil

Runs an Ash query, returning the first result or nil, or raising an error. See `read_first/2` for more.

# read_one(query, opts \\ [])

    @spec read_one(
      resource_or_query :: Ash.Query.t() | Ash.Resource.t(),
      opts :: Keyword.t()
    ) ::
      {:ok, Ash.Resource.record() | nil} | {:error, Ash.Error.t()}

Runs a query on a resource, returning a single result, nil, or an error.

If more than one result would be returned, an error is returned instead.

## Options

- `:not_found_error?` (`boolean/0`) - Whether or not to return an `Ash.Error.Query.NotFound` if no record is found. The default value is `false`.

- `:page` - Pagination options, see `Ash.read/2` for more.

- `:load` (`term/0`) - A load statement to add onto the query

- `:max_concurrency` (`non_neg_integer/0`) - The maximum number of processes allowed to be started for parallel loading of relationships and calculations. Defaults to `System.schedulers_online() * 2`

- `:lock` (`term/0`) - A lock statement to add onto the query

- `:return_query?` (`boolean/0`) - If `true`, the query that was ultimately used is returned as a third tuple element.
  The query goes through many potential changes during a request, potentially adding authorization filters, or replacing relationships for other data layers with their corresponding ids. This option can be used to get the true query that was sent to the data layer. The default value is `false`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

- `:reuse_values?` (`boolean/0`) - Whether calculations are allowed to reuse values that have already been loaded, or must refetch them from the data layer. The default value is `false`.

- `:strict?` (`boolean/0`) - If set to true, only specified attributes will be loaded when passing a list of fields to fetch on a relationship, which allows for more optimized data-fetching.
  See `Ash.Query.load/2`. The default value is `false`.

- `:authorize_with` - If set to `:error`, instead of applying authorization filters as a filter, any records not matching the authroization filter will cause an error to be returned. Valid values are :filter, :error The default value is `:filter`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

# read_one!(query, opts \\ [])

    @spec read_one!(
      resource_or_query :: Ash.Query.t() | Ash.Resource.t(),
      opts :: Keyword.t()
    ) ::
      Ash.Resource.record() | nil

Runs an ash query, returning a single result or raise an error. See `read_one/2` for more.

# reload(record, opts \\ [])

    @spec reload(record :: Ash.Resource.record(), opts :: Keyword.t()) ::
      {:ok, Ash.Resource.record()} | {:error, Ash.Error.t()}

Refetches a record by primary key. See `get/2` for more.

# reload!(record, opts \\ [])

    @spec reload!(record :: Ash.Resource.record(), opts :: Keyword.t()) ::
      Ash.Resource.record() | no_return()

Refetches a record by primary key or raises an error. See `reload/2` for more.

# run_action(input, opts \\ [])

    @spec run_action(input :: Ash.ActionInput.t(), opts :: Keyword.t()) ::
      :ok | {:ok, term()} | {:error, Ash.Error.t()}

Runs a generic action.

Options:

- `:actor` (`term/0`) - The actor for handling `^actor/1` templates, supplied to calculation context.

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - The tenant, supplied to calculation context.

- `:authorize?` (`boolean/0`) - Whether or not the request should be authorized.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer, provided to the calculation context.

- `:domain` (`Ash.Domain`) - The domain to use for the action

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

# run_action!(input, opts \\ [])

    @spec run_action!(input :: Ash.ActionInput.t(), opts :: Keyword.t()) ::
      term() | no_return()

Runs a generic action or raises an error. See `run_action/2` for more

# stream!(query, opts \\ [])

    @spec stream!(query :: Ash.Query.t() | Ash.Resource.t(), opts :: Keyword.t()) ::
      Enumerable.t(Ash.Resource.record())

Streams the results of a query.

## Strategies

There are three strategies supported, and the best one available is always chosen. They are, in order from best to worst:

- `:keyset`
- `:offset`
- `:full_read`

By default, only `:keyset` is supported. If you want to allow worse strategies to be used, pass the worst one you wish to allow as the `allow_stream_with` option, i.e `allow_stream_with: :full_read`. If you wish to specify a specific strategy to use, pass `stream_with: :strategy_name`.

### Keyset

This utilizes keyset pagination to accomplish this stream. The action must support keyset pagination. This is the most efficient way to stream a query, because it works by using filters which can benefit from indexes in the data layer.

### Offset

This utilizes offset/limit to accomplish this stream. If the action supports offset pagination, that will be used. Otherwise, if the data layer supports limit/offset, then explicit limits/offsets will be used. This is a much less efficient way of streaming a resource than `keyset`. To use limit/offset to reliably stream, a sort must always be applied, and limit/offset in the data layer will generally require sorting the entire table to figure out what is in each batch.

### Full Read

This reads the entire table into memory with no limit. This is, generally speaking, the least efficient.

## Options

- `:batch_size` (`integer/0`) - How many records to request in each query run. Defaults to the pagination limits on the resource, or 250.

- `:allow_stream_with` - The 'worst' strategy allowed to be used to fetch records. See `Ash.stream!/2` docs for more. Valid values are :keyset, :offset, :full_read The default value is `:keyset`.

- `:stream_with` - The specific strategy to use to fetch records. See `Ash.stream!/2` docs for more. Valid values are :keyset, :offset, :full_read

- `:load` (`term/0`) - A load statement to add onto the query

- `:max_concurrency` (`non_neg_integer/0`) - The maximum number of processes allowed to be started for parallel loading of relationships and calculations. Defaults to `System.schedulers_online() * 2`

- `:lock` (`term/0`) - A lock statement to add onto the query

- `:return_query?` (`boolean/0`) - If `true`, the query that was ultimately used is returned as a third tuple element.
  The query goes through many potential changes during a request, potentially adding authorization filters, or replacing relationships for other data layers with their corresponding ids. This option can be used to get the true query that was sent to the data layer. The default value is `false`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

- `:reuse_values?` (`boolean/0`) - Whether calculations are allowed to reuse values that have already been loaded, or must refetch them from the data layer. The default value is `false`.

- `:strict?` (`boolean/0`) - If set to true, only specified attributes will be loaded when passing a list of fields to fetch on a relationship, which allows for more optimized data-fetching.
  See `Ash.Query.load/2`. The default value is `false`.

- `:authorize_with` - If set to `:error`, instead of applying authorization filters as a filter, any records not matching the authroization filter will cause an error to be returned. Valid values are :filter, :error The default value is `:filter`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

# sum(query, field, opts \\ [])

Fetches the sum of a given field.

# sum!(query, field, opts \\ [])

Fetches the sum of a given field or raises an error.

# update(changeset_or_record, params_or_opts \\ %{}, opts \\ [])

    @spec update(
      changeset_or_record :: Ash.Changeset.t() | Ash.Resource.record(),
      params_or_opts :: map() | Keyword.t(),
      opts :: Keyword.t()
    ) ::
      {:ok, Ash.Resource.record()}
      | {:ok, Ash.Resource.record(), [Ash.Notifier.Notification.t()]}
      | {:error, term()}

Update a record.

- `:params` (`map/0`) - Parameters to supply, ignored if the input is a changeset, only used when an identifier is given.

- `:atomic_upgrade?` (`boolean/0`) - If true the action will be done atomically if it can (and is configured to do so), ignoring the in memory transformations and validations. You should not generally need to disable this. The default value is `true`.

- `:domain` (`Ash.Domain`) - The domain to use.

- `:timeout` (`timeout/0`) - A positive integer, or `:infinity`. If none is provided, the timeout configured on the domain is used.

- `:tracer` (one or a list of module that adopts `Ash.Tracer`) - A tracer that implements the `Ash.Tracer` behaviour. See that module for more.

- `:action` (`term/0`) - The action to use, either an Action struct or the name of the action

- `:authorize?` - If an actor option is provided (even if it is `nil`), authorization happens automatically. If not, this flag can be used to authorize with no user. Valid values are true, false, nil

- `:context` (`map/0`) - Context to set on the query, changeset, or input

- `:tenant` (value that implements the `Ash.ToTenant` protocol) - A tenant to set on the query or changeset

- `:actor` (`term/0`) - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

- `:return_notifications?` (`boolean/0`) - Use this if you're running ash actions in your own transaction and you want to manually handle sending notifications.
  If a transaction is ongoing, and this is false, notifications will be discarded, otherwise the return value is `{:ok, result, notifications}` (or `{:ok, notifications}`)
  To send notifications later, use `Ash.Notifier.notify(notifications)`. It sends any notifications that can be sent, and returns the rest. The default value is `false`.

- `:rollback_on_error?` (`boolean/0`) - Whether or not to rollback the transaction on error, if the resource is in a transaction.
  If the action has `transaction? false` this option has no effect. If an error is returned from the data layer and the resource is in a transaction, the transaction is always rolled back, regardless. The default value is `true`.

- `:notification_metadata` (`term/0`) - Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is `%{}`.

- `:skip_unknown_inputs` - A list of inputs that, if provided, will be ignored if they are not recognized by the action. Use `:*` to indicate all unknown keys.

- `:load` (`term/0`) - A load statement to add onto the changeset

# update!(changeset_or_record, params_or_opts \\ %{}, opts \\ [])

    @spec update!(
      changeset_or_record :: Ash.Changeset.t() | Ash.Resource.record(),
      params_or_opts :: map() | Keyword.t(),
      opts :: Keyword.t()
    ) ::
      Ash.Resource.record()
      | {Ash.Resource.record(), [Ash.Notifier.Notification.t()]}
      | no_return()

Update a record. See `update/2` for more information.