ash v0.10.0 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. Defining a resource won't do much for you. Once you have some resources defined, you include them in an Api like so:

defmodule MyApp.Api do
  use Ash.Api

  resources do
    resource OneResource
    resource SecondResource
end

Then you can interact through that Api with the actions that those resources expose. For example: MyApp.Api.create(OneResource, %{attributes: %{name: "thing"}}), 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

Destroy a record

Destroy a record

Get a record by a primary key

Get a record by a primary key

Run a query on a resource

Run a query on a resource

Refetches a record from the database

Refetches a record from the database, raising on error.

Side load on already fetched records

Side load on already fetched records

Update a record

Update a record

Link to this section Functions

Specs

resources(Ash.api()) :: [Ash.resource()]

Link to this section Callbacks

Link to this callback

create(resource, params)

View Source

Specs

create(resource :: Ash.resource(), 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.

  • :attributes - Changes to be applied to attribute values The default value is %{}.

  • :relationships - Changes to be applied to relationship values The default value is %{}.

  • :side_load - Side loads to include in the query, same as you would pass to Ash.Query.side_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.

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

create!(resource, params)

View Source

Specs

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

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.

  • :attributes - Changes to be applied to attribute values The default value is %{}.

  • :relationships - Changes to be applied to relationship values The default value is %{}.

  • :side_load - Side loads to include in the query, same as you would pass to Ash.Query.side_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.

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

Specs

destroy(record :: 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.

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

destroy!(record, params)

View Source

Specs

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

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.

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

Get a record by a primary key

  • :side_load - Side loads to include in the query, same as you would pass to Ash.Query.side_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.

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

  • :side_load - Side loads to include in the query, same as you would pass to Ash.Query.side_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.

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

Specs

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

Run a query on a resource

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

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

Specs

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

Run a query on a resource

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

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

Specs

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

Refetches a record from the database

Specs

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

Refetches a record from the database, raising on error.

See reload/1.

Link to this callback

side_load(resource, params)

View Source

Specs

side_load(resource :: Ash.resource(), params :: Keyword.t()) ::
  {:ok, [Ash.resource()]} | {:error, Ash.error()}

Side load on already fetched records

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

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

side_load!(resource, params)

View Source

Specs

side_load!(resource :: Ash.resource(), params :: Keyword.t()) ::
  [Ash.resource()] | no_return()

Side load on already fetched records

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

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

Specs

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

Update a record

  • :attributes - Changes to be applied to attribute values The default value is %{}.

  • :relationships - Changes to be applied to relationship values The default value is %{}.

  • :side_load - Side loads to include in the query, same as you would pass to Ash.Query.side_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.

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

Specs

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

Update a record

  • :attributes - Changes to be applied to attribute values The default value is %{}.

  • :relationships - Changes to be applied to relationship values The default value is %{}.

  • :side_load - Side loads to include in the query, same as you would pass to Ash.Query.side_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.

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