View Source Ash.Api behaviour (ash v2.4.18)

An Api allows you to interact with your resources, and holds non-resource-specific configuration.

For example, the json api extension adds an api extension that lets you toggle authorization on/off for all resources in that Api. You include them in an Api like so:

defmodule MyApp.Registry do
  use Ash.Registry,
    extensions: [Ash.Registry.ResourceValidations]

  entries do
    entry OneResource
    entry SecondResource
  end
end

defmodule MyApp.Api do
  use Ash.Api

  resources do
    registry MyApp.Registry
  end
end

Then you can interact through that Api with the actions that those resources expose. For example: MyApp.Api.create(changeset), or MyApp.Api.read(query). Corresponding actions must be defined in your resources in order to call them through the Api.

interface

Interface

The functions documented here can be used to call any action on any resource in the Api. For example, MyApi.read(Myresource, [...]).

Additionally, you can define a code_interface on each resource. See the code interface guide for more.

Link to this section Summary

Callbacks

Create a record. See create/2 for more information.

Create a record.

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

Destroy a record.

Get a record by a primary key. See get/3 for more.

Get a record by a primary key.

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

Load fields or relationships on already fetched records.

Fetch a page relative to the provided page.

Fetch a page relative to the provided page.

Run an ash query. See read/2 for more.

Run a query on a resource.

Run an ash query, raising on more than one result. See read_one/2 for more.

Run a query on a resource, but fail on more than one result.

Refetches a record by primary key. See reload/1 for more.

Refetches a record by primary key.

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

Update a record.

Link to this section Types

@type load_statement() ::
  Ash.Query.t()
  | [atom()]
  | atom()
  | Keyword.t()
  | [atom() | {atom(), atom() | Keyword.t()}]
@type page_request() :: :next | :prev | :first | :last | integer()
@type t() :: module()

Link to this section Callbacks

Create a record. See create/2 for more information.

@callback create(Ash.Changeset.t(), params :: Keyword.t()) ::
  {:ok, Ash.Resource.record()}
  | {:ok, Ash.Resource.record(), [Ash.Notifier.Notification.t()]}
  | {:error, term()}

Create a record.

  • :upsert? - 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 - 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

  • :timeout - A positive integer, or :infinity. If none is provided, the timeout configured on the api is used (which defaults to 30_000).

  • :tracer - A tracer that implements the Ash.Tracer behaviour. See that module for more.

  • :verbose? - Log engine operations (very verbose!) The default value is false.

  • :action - 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.

  • :stacktraces? - For Ash errors, whether or not each error has a stacktrace. See the error_handling guide for more. The default value is true.

  • :tenant - A tenant to set on the query or changeset

  • :actor - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

  • :after_action - A hook to be run just before the action returns, but before fields are selected (still inside the same transaction, if your data layer supports transactions). This is mostly important if you want to load calculations after the action, which depend on having fields selected, but you want to authorize with the minimal set of fields that are actually being selected. Runs only if the action is successful, and is passed the changeset and result of the action. Should return {:ok, result} or {:error, error}.
    For example, if you had a full_name calculation, but were only selecting, first_name and full_name, you might do something like this:

    MyApp.User
    |> Ash.Changeset.for_create(:create, %{first_name: "first_name", last_name: "last_name"}
    |> Ash.Changeset.select(:first_name))
    |> Api.create(after_action: fn _changeset, user -> Api.load(user, :full_name) end)

    If you tried to load that :full_name calculation after receiving the data, the last_name would not be selected and as such would not be usable in the calculation, regardless of whether or not the calculation includes that field in its select list.

  • :return_notifications? - Use this if you're running ash actions in your own transaction and you want notifications to happen still.
    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.

  • :notification_metadata - Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is %{}.

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

@callback destroy(Ash.Changeset.t() | Ash.Resource.record(), params :: 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? - If true, the destroyed record is included in the return result, e.g {:ok, destroyed} or {:ok, destroyed, notifications} The default value is false.

  • :timeout - A positive integer, or :infinity. If none is provided, the timeout configured on the api is used (which defaults to 30_000).

  • :tracer - A tracer that implements the Ash.Tracer behaviour. See that module for more.

  • :verbose? - Log engine operations (very verbose!) The default value is false.

  • :action - 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.

  • :stacktraces? - For Ash errors, whether or not each error has a stacktrace. See the error_handling guide for more. The default value is true.

  • :tenant - A tenant to set on the query or changeset

  • :actor - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

  • :return_notifications? - Use this if you're running ash actions in your own transaction and you want notifications to happen still.
    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.

  • :notification_metadata - Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is %{}.

Link to this callback

get!( resource, id_or_filter, params )

View Source
@callback get!(
  resource :: Ash.Resource.t(),
  id_or_filter :: term(),
  params :: Keyword.t()
) :: Ash.Resource.record() | no_return()

Get a record by a primary key. See get/3 for more.

Link to this callback

get( resource, id_or_filter, params )

View Source
@callback get(
  resource :: Ash.Resource.t(),
  id_or_filter :: term(),
  params :: Keyword.t()
) :: {:ok, Ash.Resource.record()} | {:error, term()}

Get a record by a primary key.

For a resource with a composite primary key, pass a keyword list, e.g MyApi.get(MyResource, first_key: 1, second_key: 2)

  • :error? - 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 - Fields or relationships to load in the query. See Ash.Query.load/2

  • :context - Context to be set on the query being run

  • :timeout - A positive integer, or :infinity. If none is provided, the timeout configured on the api is used (which defaults to 30_000).

  • :tracer - A tracer that implements the Ash.Tracer behaviour. See that module for more.

  • :verbose? - Log engine operations (very verbose!) The default value is false.

  • :action - 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.

  • :stacktraces? - For Ash errors, whether or not each error has a stacktrace. See the error_handling guide for more. The default value is true.

  • :tenant - A tenant to set on the query or changeset

  • :actor - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

Link to this callback

load!( record_or_records, query, opts )

View Source
@callback load!(
  record_or_records :: Ash.Resource.record() | [Ash.Resource.record()],
  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.

Link to this callback

load( record_or_records, query, opts )

View Source
@callback 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: MyApi.load(record, [posts: [:comments]]).

  • :lazy? - If set to true, values will only be loaded if the related value isn't currently loaded. The default value is false.

  • :timeout - A positive integer, or :infinity. If none is provided, the timeout configured on the api is used (which defaults to 30_000).

  • :tracer - A tracer that implements the Ash.Tracer behaviour. See that module for more.

  • :verbose? - Log engine operations (very verbose!) The default value is false.

  • :action - 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.

  • :stacktraces? - For Ash errors, whether or not each error has a stacktrace. See the error_handling guide for more. The default value is true.

  • :tenant - A tenant to set on the query or changeset

  • :actor - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

Link to this callback

page!(page, page_request)

View Source
@callback page!(Ash.Page.page(), page_request()) :: Ash.Page.page() | no_return()

Fetch a page relative to the provided page.

Link to this callback

page(page, page_request)

View Source
@callback page(Ash.Page.page(), page_request()) ::
  {:ok, Ash.Page.page()} | {:error, term()}

Fetch a page relative to the provided page.

A page is the return value of a paginated action called via read/2.

@callback read!(Ash.Query.t() | Ash.Resource.t(), params :: Keyword.t()) ::
  [Ash.Resource.record()]
  | {[Ash.Resource.record()], Ash.Query.t()}
  | no_return()

Run an ash query. See read/2 for more.

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

Run a query on a resource.

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

  • :page - Pagination options, see the pagination docs for more

  • :load - A load statement to add onto the query

  • :return_query? - 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.

  • :timeout - A positive integer, or :infinity. If none is provided, the timeout configured on the api is used (which defaults to 30_000).

  • :tracer - A tracer that implements the Ash.Tracer behaviour. See that module for more.

  • :verbose? - Log engine operations (very verbose!) The default value is false.

  • :action - 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.

  • :stacktraces? - For Ash errors, whether or not each error has a stacktrace. See the error_handling guide for more. The default value is true.

  • :tenant - A tenant to set on the query or changeset

  • :actor - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

pagination

Pagination

Limit/offset pagination

  • :offset - The number of records to skip from the beginning of the query

  • :limit - The number of records to include in the page

  • :filter - 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 - Whether or not to return the page with a full count of all records

Keyset pagination

  • :before - Get records that appear before the provided keyset (mutually exclusive with after)

  • :after - Get records that appear after the provided keyset (mutually exclusive with before)

  • :limit - How many records to include in the page

  • :filter - See the filter option for offset pagination, this behaves the same.

  • :count - Whether or not to return the page with a full count of all records

@callback read_one!(Ash.Query.t() | Ash.Resource.t(), params :: Keyword.t()) ::
  Ash.Resource.record() | {Ash.Resource.record(), Ash.Query.t()} | no_return()

Run an ash query, raising on more than one result. See read_one/2 for more.

@callback read_one(Ash.Query.t() | Ash.Resource.t(), params :: Keyword.t()) ::
  {:ok, Ash.Resource.record()}
  | {:ok, Ash.Resource.record(), Ash.Query.t()}
  | {:error, term()}

Run a query on a resource, but fail on more than one result.

This is useful if you have a query that doesn't include a primary key but you know that it will only ever return a single result.

@callback reload!(record :: Ash.Resource.record(), params :: Keyword.t()) ::
  Ash.Resource.record() | no_return()

Refetches a record by primary key. See reload/1 for more.

@callback reload(record :: Ash.Resource.record()) ::
  {:ok, Ash.Resource.record()} | {:error, term()}

Refetches a record by primary key.

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

@callback update(Ash.Changeset.t(), params :: Keyword.t()) ::
  {:ok, Ash.Resource.record()}
  | {:ok, Ash.Resource.record(), [Ash.Notifier.Notification.t()]}
  | {:error, term()}

Update a record.

  • :timeout - A positive integer, or :infinity. If none is provided, the timeout configured on the api is used (which defaults to 30_000).

  • :tracer - A tracer that implements the Ash.Tracer behaviour. See that module for more.

  • :verbose? - Log engine operations (very verbose!) The default value is false.

  • :action - 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.

  • :stacktraces? - For Ash errors, whether or not each error has a stacktrace. See the error_handling guide for more. The default value is true.

  • :tenant - A tenant to set on the query or changeset

  • :actor - If an actor is provided, it will be used in conjunction with the authorizers of a resource to authorize access

  • :after_action - A hook to be run just before the action returns, but before fields are selected (still inside the same transaction, if your data layer supports transactions). This is mostly important if you want to load calculations after the action, which depend on having fields selected, but you want to authorize with the minimal set of fields that are actually being selected. Runs only if the action is successful, and is passed the changeset and result of the action. Should return {:ok, result} or {:error, error}.
    For example, if you had a full_name calculation, but were only selecting, first_name and full_name, you might do something like this:

    MyApp.User
    |> Ash.Changeset.for_create(:create, %{first_name: "first_name", last_name: "last_name"}
    |> Ash.Changeset.select(:first_name))
    |> Api.create(after_action: fn _changeset, user -> Api.load(user, :full_name) end)

    If you tried to load that :full_name calculation after receiving the data, the last_name would not be selected and as such would not be usable in the calculation, regardless of whether or not the calculation includes that field in its select list.

  • :return_notifications? - Use this if you're running ash actions in your own transaction and you want notifications to happen still.
    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.

  • :notification_metadata - Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is %{}.

Link to this section Functions

This function is deprecated. use Ash.Api.Info.allow/1 instead.

See Ash.Api.Info.allow/1.

Link to this function

allow_unregistered?(api)

View Source
This function is deprecated. use Ash.Api.Info.allow_unregistered?/1 instead.

See Ash.Api.Info.allow_unregistered?/1.

This function is deprecated. use Ash.Api.Info.authorize/1 instead.

See Ash.Api.Info.authorize/1.

This function is deprecated. use Ash.Api.Info.registry/1 instead.

See Ash.Api.Info.registry/1.

This function is deprecated. use Ash.Api.Info.require_actor?/1 instead.

See Ash.Api.Info.require_actor?/1.

This function is deprecated. use Ash.Api.Info.resource/2 instead.

See Ash.Api.Info.resource/2.

This function is deprecated. use Ash.Api.Info.resources/1 instead.

See Ash.Api.Info.resources/1.

This function is deprecated. use Ash.Api.Info.timeout/1 instead.

See Ash.Api.Info.timeout/1.