Skip to content

About

Declarative associations between plain structs, loaded in batches.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

36 Commits

Folders and files

Repository files navigation

Associations

Declarative associations between plain structs, loaded in batches.

Structs that come from anywhere (an API client, an ETS table, a context function) have no associations of their own. Associations lets a module declare how they relate and how to list them, then reads those declarations in load/3.

defmodule Garage do
  use Associations

  alias Garage.{Car, Customer}

  @impl true
  def list(schema, fields, values, _args) do
    Store.list_by(schema, fields, values)
  end

  association Car do
    belongs_to :owner, Customer
  end

  association Customer do
    has_many :cars, Car, foreign_key: :owner_id
  end
end
Garage.load(car, :owner)
#=> %Customer{id: 1}

Garage.load([customer, dealer], :cars)
#=> [[%Car{id: 1}, %Car{id: 2}], [%Car{id: 1}]]

See Associations for the full documentation.

Installation

Add associations to your list of dependencies in mix.exs:

def deps do
  [
    {:associations, github: "Wigny/associations"}
  ]
end

About

Declarative associations between plain structs, loaded in batches.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages