View Source Ash.Api behaviour (ash v1.53.2)
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 to be exposed in the Api module.
See the resource DSL documentation 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.
Functions
Gets the resources of an Api module. DO NOT USE AT COMPILE TIME.
Link to this section Types
Link to this section Callbacks
@callback create!(Ash.Changeset.t(), params :: Keyword.t()) :: Ash.Resource.record() | {Ash.Resource.record(), [Ash.Notifier.Notification.t()]} | no_return()
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 isfalse
.:upsert_identity
- The identity to use when detecting conflicts forupsert?
, e.g.upsert_identity: :full_name
. By default, the primary key is used. Has no effect ifupsert?: true
is not provided:timeout
- A positive integer, or:infinity
. If none is provided, the timeout configured on the api is used (which defaults to30_000
).:verbose?
- Log engine operations (very verbose!) The default value isfalse
.: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 isnil
), 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 istrue
.: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 afull_name
calculation, but were only selecting,first_name
andfull_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, thelast_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, useAsh.Notifier.notify(notifications)
. It sends any notifications that can be sent, and returns the rest. The default value isfalse
.:notification_metadata
- Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is%{}
.
@callback destroy!(Ash.Changeset.t() | Ash.Resource.record(), params :: 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.
@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 isfalse
.:timeout
- A positive integer, or:infinity
. If none is provided, the timeout configured on the api is used (which defaults to30_000
).:verbose?
- Log engine operations (very verbose!) The default value isfalse
.: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 isnil
), 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 istrue
.: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, useAsh.Notifier.notify(notifications)
. It sends any notifications that can be sent, and returns the rest. The default value isfalse
.:notification_metadata
- Metadata to be merged into the metadata field for all notifications sent from this operation. The default value is%{}
.
@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.
@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?
- Wether 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 istrue
.:load
- Fields or relationships to load in the query. SeeAsh.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 to30_000
).:verbose?
- Log engine operations (very verbose!) The default value isfalse
.: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 isnil
), 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 istrue
.: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
@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.
@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 isfalse
.:timeout
- A positive integer, or:infinity
. If none is provided, the timeout configured on the api is used (which defaults to30_000
).:verbose?
- Log engine operations (very verbose!) The default value isfalse
.: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 isnil
), 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 istrue
.: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
@callback page!(Ash.Page.page(), page_request()) :: Ash.Page.page() | no_return()
Fetch a page relative to the provided page.
@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
- Nested pagination options, see the section on pagination for more:load
- A load statement to add onto the query:return_query?
- Iftrue
, 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 isfalse
.:timeout
- A positive integer, or:infinity
. If none is provided, the timeout configured on the api is used (which defaults to30_000
).:verbose?
- Log engine operations (very verbose!) The default value isfalse
.: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 isnil
), 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 istrue
.: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 withafter
):after
- Get records that appear after the provided keyset (mutually exclusive withbefore
):limit
- How many records to include in the page:filter
- See thefilter
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.
@callback update!(Ash.Changeset.t(), params :: Keyword.t()) :: Ash.Resource.record() | {Ash.Resource.record(), [Ash.Notifier.Notification.t()]} | no_return()
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 to30_000
).:verbose?
- Log engine operations (very verbose!) The default value isfalse
.: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 isnil
), 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 istrue
.: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 afull_name
calculation, but were only selecting,first_name
andfull_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, thelast_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, useAsh.Notifier.notify(notifications)
. It sends any notifications that can be sent, and returns the rest. The default value isfalse
.: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
@spec authorize(t()) :: :when_requested | :always | :by_default
@spec resources(t()) :: [Ash.Resource.t()]
Gets the resources of an Api module. DO NOT USE AT COMPILE TIME.
If you need the resource list at compile time, you will need to introduce a compile time
dependency on all of the resources, and therefore should use the registry directly. Registry |> Ash.Registry.entries()
.