ash v1.24.1 Ash.Resource.Dsl View Source

The built in resource DSL. The core DSL components of a resource are:

Link to this section Summary

Functions

A section for declaring resource actions.

Declare named aggregates on the resource.

A section for declaring attributes on the resource.

Declare named calculations on the resource.

Options for configuring the multitenancy behavior of a resource.

A section for declaring relationships on the resource.

Resource-wide configuration

Declare validations prior to performing actions against the resource

Link to this section Functions

A section for declaring resource actions.

All manipulation of data through the underlying data layer happens through actions. There are four types of action: create, read, update, and destroy. You may recognize these from the acronym CRUD. You can have multiple actions of the same type, as long as they have different names. This is the primary mechanism for customizing your resources to conform to your business logic. It is normal and expected to have multiple actions of each type in a large application.

If you have multiple actions of the same type, one of them must be designated as the primary action for that type, via: primary?: true. This tells the ash what to do if an action of that type is requested, but no specific action name is given.

Constructors

Link to this macro

aggregates(body)

View Source (macro)

Declare named aggregates on the resource.

These are aggregates that can be loaded only by name using Ash.Query.load/2. They are also available as top level fields on the resource.

Constructors

Link to this macro

attributes(body)

View Source (macro)

A section for declaring attributes on the resource.

Attributes are fields on an instance of a resource. The two required pieces of knowledge are the field name, and the type.

Constructors

Link to this macro

calculations(body)

View Source (macro)

Declare named calculations on the resource.

These are calculations that can be loaded only by name using Ash.Query.load/2. They are also available as top level fields on the resource.

Constructors

Link to this macro

multitenancy(body)

View Source (macro)

Options for configuring the multitenancy behavior of a resource.

To specify a tenant, use Ash.Query.set_tenant/2 or Ash.Changeset.set_tenant/2 before passing it to an operation.

Options

  • :strategy - Determine how to perform multitenancy. :attribute will expect that an attribute matches the given tenant, e.g org_id. context (the default) implies that the tenant will be passed to the datalayer as context. How a given data layer handles multitenancy will differ depending on the implementation. See the datalayer documentation for more. The default value is :context.

  • :attribute - If using the attribute strategy, the attribute to use, e.g org_id

  • :global? - Whether or not the data also exists outside of each tenant. This allows running queries and making changes without setting a tenant. This may eventually be extended to support describing the relationship to global data. For example, perhaps the global data is shared among all tenants (requiring "union" support in data layers), or perhaps global data is "merged" using some strategy (also requiring "union" support). The default value is false.

  • :parse_attribute - An mfa ({module, function, args}) pointing to a function that takes a tenant and returns the attribute value The default value is {Ash.Resource.Dsl, :identity, []}.

Link to this macro

relationships(body)

View Source (macro)

A section for declaring relationships on the resource.

Relationships are a core component of resource oriented design. Many components of Ash will use these relationships. A simple use case is side_loading (done via the Ash.Query.load/2).

Constructors

Link to this macro

resource(body)

View Source (macro)

Resource-wide configuration

Options

  • :description - A human readable description of the resource, to be used in generated documentation

  • :base_filter - A filter statement to be applied to any queries on the resource

Sections

Link to this macro

validations(body)

View Source (macro)

Declare validations prior to performing actions against the resource

Constructors