ash v0.10.0 Ash View Source

Logo

Introduction

Traditional MVC Frameworks (Rails, Django, .Net, Phoenix, etc) leave it up to the user to build the glue between requests for data (HTTP requests in various forms as well as server-side domain logic) and their respective ORMs. In that space, there is an incredible amount of boilerplate code that must get written from scratch for each application (authentication, authorization, sorting, filtering, sideloading relationships, serialization, etc).

Ash is an opinionated yet configurable framework designed to reduce boilerplate in an Elixir application. Ash does this by providing a layer of abstraction over your system's data layer(s) with Resources. It is designed to be used in conjunction with a phoenix application, or on its own.

To riff on a famous JRR Tolkien quote, a Resourceis "One Interface to rule them all, One Interface to find them" and will become an indispensable place to define contracts for interacting with data throughout your application.

To start using Ash, first declare your Resources using the Ash Resource DSL. You could technically stop there, and just leverage the Ash Elixir API to avoid writing boilerplate. More likely, you would use extensions like Ash.JsonApi or Ash.GraphQL with Phoenix to add external interfaces to those resources without having to write any extra code at all.

Ash is an open-source project and draws inspiration from similar ideas in other frameworks and concepts. The goal of Ash is to lower the barrier to adopting and using Elixir and Phoenix, and in doing so help these amazing communities attract new developers, projects, and companies.

Example Resource

defmodule Post do
  use Ash.Resource

  actions do
    read :default

    create :default
  end

  attributes do
    attribute :name, :string
  end

  relationships do
    belongs_to :author, Author
  end
end

For those looking to add ash extensions:

  • see Ash.Dsl.Extension for adding configuration.
  • If you are looking to write a new data source, also see the Ash.DataLayer documentation.
  • If you are looking to write a new authorizer, see Ash.Authorizer
  • If you are looking to write a "front end", something powered by Ash resources, a guide on building those kinds of tools is in the works.

Link to this section Summary

Functions

Returns the action with the matching name and type on the resource

Get an attribute name from the resource

A list of authorizers to be used when accessing the resource

The data layer of the resource, or nil if it does not have one

Returns the primary action of a given type for a resource

A list of field names corresponding to the primary key of a resource

Gets a relationship by name from the resource

Link to this section Types

Specs

action() ::
  Ash.Resource.Actions.Create.t()
  | Ash.Resource.Actions.Read.t()
  | Ash.Resource.Actions.Update.t()
  | Ash.Resource.Actions.Destroy.t()

Specs

actor() :: record()

Specs

api() :: module()

Specs

attribute() :: Ash.Resource.Attribute.t()
Link to this type

cardinality_many_relationship()

View Source

Specs

cardinality_many_relationship() ::
  Ash.Resource.Relationships.HasMany.t()
  | Ash.Resource.Relationships.ManyToMany.t()
Link to this type

cardinality_one_relationship()

View Source

Specs

cardinality_one_relationship() ::
  Ash.Resource.Relationships.HasOne.t()
  | Ash.Resource.Relationships.BelongsTo.t()

Specs

data_layer() :: module()

Specs

data_layer_query() :: struct()

Specs

error() :: struct()

Specs

filter() :: map()

Specs

params() :: Keyword.t()

Specs

query() :: Ash.Query.t()

Specs

record() :: struct()

Specs

Link to this type

relationship_cardinality()

View Source

Specs

relationship_cardinality() :: :many | :one

Specs

resource() :: module()

Specs

side_loads() :: Keyword.t()

Specs

sort() :: Keyword.t()

Link to this section Functions

Link to this function

action(resource, name, type)

View Source

Specs

action(resource(), atom(), atom()) :: action() | nil

Returns the action with the matching name and type on the resource

Link to this function

attribute(resource, name)

View Source

Specs

attribute(resource(), String.t() | atom()) :: attribute() | nil

Get an attribute name from the resource

Specs

authorizers(resource()) :: [module()]

A list of authorizers to be used when accessing the resource

Specs

data_layer(resource()) :: data_layer()

The data layer of the resource, or nil if it does not have one

Link to this function

implements_behaviour?(module, behaviour)

View Source
Link to this function

in_transaction?(resource)

View Source

Specs

in_transaction?(resource()) :: boolean()
Link to this function

primary_action(resource, type)

View Source

Specs

primary_action(resource(), atom()) :: action() | nil

Returns the primary action of a given type for a resource

Specs

primary_key(resource()) :: [atom()]

A list of field names corresponding to the primary key of a resource

Link to this function

relationship(resource, relationship_name)

View Source

Specs

relationship(any(), any()) :: any()

Gets a relationship by name from the resource

Link to this function

rollback(resource, term)

View Source

Specs

rollback(resource(), term()) :: no_return()
Link to this function

transaction(resource, func)

View Source

Specs

transaction(resource(), (() -> term())) :: term()