Ash.Api behaviour (ash v1.37.2) View Source

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.Api do
  use Ash.Api

  resources do
    resource OneResource
    resource SecondResource
  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

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 to be exposed in the Api module. See the resource DSL documentation for more.

Link to this section Summary

Callbacks

Create a record.

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

Destroy a record.

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

Get a record by a primary key.

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

Load fields or relationships on already fetched records.

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

Fetch a page relative to the provided page.

Fetch a page relative to the provided page.

Run a query on a resource.

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

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

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

Refetches a record by primary key.

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

Update a record.

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

Link to this section Types

Specs

page_request() :: :next | :prev | :first | :last | integer()

Specs

t() :: module()

Link to this section Functions

Link to this section Callbacks

Specs

create(Ash.Changeset.t(), params :: Keyword.t()) ::
  {:ok, Ash.Resource.record()} | {: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.

  • :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, can be set to true to get a stacktrace for each error that occured. See the error_handling guide for more. The default value is false.

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

Specs

create!(Ash.Changeset.t(), params :: Keyword.t()) ::
  Ash.Resource.record() | no_return()

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

Specs

destroy(Ash.Changeset.t() | Ash.Resource.record(), params :: Keyword.t()) ::
  :ok | {:error, term()}

Destroy a record.

  • :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, can be set to true to get a stacktrace for each error that occured. See the error_handling guide for more. The default value is false.

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

Specs

destroy!(Ash.Changeset.t() | Ash.Resource.record(), params :: Keyword.t()) ::
  :ok | no_return()

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

Link to this callback

get(resource, id_or_filter, params)

View Source

Specs

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)

  • :load - Fields or relationships to load in the query. See Ash.Query.load/2

  • :tenant - The tenant to set on the query being run

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

  • :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, can be set to true to get a stacktrace for each error that occured. See the error_handling guide for more. The default value is false.

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

get!(resource, id_or_filter, params)

View Source

Specs

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

load(record_or_records, query, opts)

View Source

Specs

load(
  record_or_records :: Ash.Resource.record() | [Ash.Resource.record()],
  query :: Ash.Query.t(),
  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]]).

  • :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, can be set to true to get a stacktrace for each error that occured. See the error_handling guide for more. The default value is false.

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

Specs

load!(
  record_or_records :: Ash.Resource.record() | [Ash.Resource.record()],
  query :: Ash.Query.t(),
  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

page(arg1, page_request)

View Source

Specs

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.

Link to this callback

page!(arg1, page_request)

View Source

Specs

Fetch a page relative to the provided page.

Specs

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 - Nested pagination options, see the section on pagination for more

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

  • :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, can be set to true to get a stacktrace for each error that occured. See the error_handling guide for more. The default value is false.

  • :actor - 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 - 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

Specs

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

Specs

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

Specs

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

Specs

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

Refetches a record by primary key.

Specs

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

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

Specs

update(Ash.Changeset.t(), params :: Keyword.t()) ::
  {:ok, Ash.Resource.record()} | {:error, term()}

Update a record.

  • :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, can be set to true to get a stacktrace for each error that occured. See the error_handling guide for more. The default value is false.

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

Specs

update!(Ash.Changeset.t(), params :: Keyword.t()) ::
  Ash.Resource.record() | no_return()

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