ash v1.24.1 Ash.Api behaviour View Source

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

Your Api can also house config that is not resource specific. 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.

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 c:load/2 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()

Link to this section Functions

Link to this section Callbacks

Specs

create(Ash.changeset(), params :: Keyword.t()) ::
  {:ok, Ash.record()} | {:error, Ash.error()}

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 is provided, authorization happens automatically. If not, this flag can be used to authorize with no user. The default value is false.

  • :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(), params :: Keyword.t()) :: Ash.record() | no_return()

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

Specs

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

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 is provided, authorization happens automatically. If not, this flag can be used to authorize with no user. The default value is false.

  • :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() | Ash.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(), id_or_filter :: term(), params :: Keyword.t()) ::
  {:ok, Ash.record()} | {:error, Ash.error()}

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

  • :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 is provided, authorization happens automatically. If not, this flag can be used to authorize with no user. The default value is false.

  • :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(), id_or_filter :: term(), params :: Keyword.t()) ::
  Ash.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.record() | [Ash.record()],
  query :: Ash.query(),
  opts :: Keyword.t()
) :: {:ok, Ash.record() | [Ash.record()]} | {:error, Ash.error()}

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 is provided, authorization happens automatically. If not, this flag can be used to authorize with no user. The default value is false.

  • :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.record() | [Ash.record()],
  query :: Ash.query(),
  opts :: Keyword.t()
) :: Ash.record() | [Ash.record()] | no_return()

Load fields or relationships on already fetched records. See c:load/2 for more information.

Link to this callback

page(arg1, page_request)

View Source

Specs

page(Ash.page(), page_request()) :: {:ok, Ash.page()} | {:error, Ash.error()}

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

page!(Ash.page(), page_request()) :: Ash.page() | no_return()

Fetch a page relative to the provided page.

Specs

read(Ash.query(), params :: Keyword.t()) ::
  {:ok, [Ash.record()]}
  | {:ok, [Ash.record()], Ash.query()}
  | {:error, Ash.error()}

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 is provided, authorization happens automatically. If not, this flag can be used to authorize with no user. The default value is false.

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

read!(Ash.query() | Ash.resource(), params :: Keyword.t()) ::
  [Ash.record()] | {[Ash.record()], Ash.Query.t()} | no_return()

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

Specs

read_one(Ash.query() | Ash.resource(), params :: Keyword.t()) ::
  {:ok, Ash.record()} | {:ok, Ash.record(), Ash.query()} | {:error, Ash.error()}

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

read_one!(Ash.query() | Ash.resource(), params :: Keyword.t()) ::
  Ash.record() | {Ash.record(), Ash.query()} | no_return()

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

Specs

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

Refetches a record by primary key.

Specs

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

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

Specs

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

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 is provided, authorization happens automatically. If not, this flag can be used to authorize with no user. The default value is false.

  • :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(), params :: Keyword.t()) :: Ash.record() | no_return()

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