ash v0.1.1 Ash.Resource View Source

A resource is a static definition of an entity in your system.

In general an entity will refer to a single data concept and use a Data Layer, which allow them to be persisted and manipulated via Ash. Currently Ash provides Postgres and ETS (Erlang Term Storage) as Data Layers, but more are planned. Any one can create a Data Layer, either to provide support for a new database, a new storage format like CSV, or to power a custom use case like a resource that is backed by an external API.

It is also possible to create a resource without a Data Layer, which for example could be used to provide autogemerated documentation of an API that you already made.

Regardless of whether or not a resource is backed by data, resources are designed to contain as much of your business logic as possible in a static declaration. Resources provide opportunities to declare CRUD operations, attributes, relationships, and other behavior, all of which can be customized to map to your underlying business logic.

Once a resource is declared, it will expose a public, standardized API that can be consumed in many different forms.

ConsumerUse Case
Business LogicServer side code such as from Phoenix Contexts
Web LayerFull JSON:API web layer compliance via AshJsonApi and AshGraphQl
Front EndsUIs can use a schema file to know exactly how to interact web layer API

In your typical application using Ash, resources would be located in the lib/resources directory. The file name should be the single underscored name of the data that backs the resource with a .ex extension (ie: lib/resources/post.ex).

To create a resource simply add use Ash.Resource, ... at the top of your resource module, and refer to the DSL documentation for the rest. The options for use Ash.Resource are described below.

For example, here is a resource definition using Postgres:

defmodule MyApp.Post do
  use Ash.Resource, name: "post", type: "post"
  use AshPostgres, repo: MyApp.Repo
end

Resource DSL documentation: Ash.Resource.DSL


Ash.Resource

  • name(string) Required: The name of the resource. This will typically be the pluralized form of the type
  • type(string) Required: The type of the resource, e.g post or author. This is used throughout the system.
  • max_page_size(integer): The maximum page size for any read action. Any request for a higher page size will simply use this number. Constraints: must be greater than zero
  • default_page_size(integer): The default page size for any read action. If no page size is specified, this value is used. Constraints: must be greater than zero
  • primary_key(boolean | primary_key[1]): If true, a default id uuid primary key that autogenerates is used. If false, none is created. See the primary_key opts for info on specifying primary key options. - Default: true

Ash.Resource - primary_key[1]

  • field(atom): The field name of the primary key of the resource. - Default: :id
  • type(atom): The data type of the primary key of the resource. - Default: :uuid

Note: Do not call the functions on a resource, as in MyResource.type() as this is a private API and can change at any time. Instead, use the Ash module, for example: Ash.type(MyResource)