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
endGarage.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.
Add associations to your list of dependencies in mix.exs:
def deps do
[
{:associations, github: "Wigny/associations"}
]
end