Back to Ash

Data Layers

usage-rules/data_layers.md

3.24.61.3 KB
Original Source
<!-- SPDX-FileCopyrightText: 2019 ash contributors <https://github.com/ash-project/ash/graphs/contributors> SPDX-License-Identifier: MIT -->

Data Layers

Data layers determine how resources are stored and retrieved. Examples of data layers:

  • Postgres: For storing resources in PostgreSQL (via AshPostgres)
  • ETS: For in-memory storage (Ash.DataLayer.Ets)
  • Mnesia: For distributed storage (Ash.DataLayer.Mnesia)
  • Embedded: For resources embedded in other resources (data_layer: :embedded) (typically JSON under the hood)
  • Ash.DataLayer.Simple: For resources that aren't persisted at all. Leave off the data layer, as this is the default.

Specify a data layer when defining a resource:

elixir
defmodule MyApp.Post do
  use Ash.Resource,
    domain: MyApp.Blog,
    data_layer: AshPostgres.DataLayer

  postgres do
    table "posts"
    repo MyApp.Repo
  end

  # ... attributes, relationships, etc.
end

For embedded resources:

elixir
defmodule MyApp.Address do
  use Ash.Resource,
    data_layer: :embedded

  attributes do
    attribute :street, :string
    attribute :city, :string
    attribute :state, :string
    attribute :zip, :string
  end
end

Each data layer has its own configuration options and capabilities. Refer to the rules & documentation of the specific data layer package for more details.