okay one that might actually be pretty simple to solve:
get("/api/regimens")
:
[
%{
"color" => "yellow",
"device_id" => 6,
"id" => 174,
"in_use" => false,
"name" => "Go around the square every day at 10 am",
"regimen_items" => [
#...
%{
"id" => 6120,
"regimen_id" => 174,
"sequence_id" => 1897,
"time_offset" => 36300000
},
%{
"id" => 6121,
"regimen_id" => 174,
"sequence_id" => 1897,
"time_offset" => 122700000
}
#...
]
}
]
so in a normal Ecto setup i would define these four modules:
defmodule Regimen do
use Ecto.Schema
schema "regimens" do
field :color, :string
field :in_use, :boolean
field :name, :string
has_many :regimen_items, RegimenItem
belongs_to :device, Device
end
end
defmodule RegimenItem do
use Ecto.Schema
schema "regimen_items" do
field :time_offset, :integer
belongs_to :regimen, Regimen
belongs_to :sequence, Sequence
end
end
defmodule Device do
use Ecto.Schema
schema "devices" do
#... other stuff
has_many :regimens, Regimen
has_many :sequences, Sequence
#... other stuff
end
end
defmodule Sequence do
use Ecto.Schema
schema "sequences" do
#... other stuff
has_many :regimen_items, RegimenItem
belongs_to :device, Device
#... other stuff
end
end
The other thing i forgot to mention: since the ID of the resources are provided by the API, i’m currently doing primary_key: false
or something to make Ecto not auto generate ids because i’m not aloud to supply an id when inserting
a record. (this may be a sqlite3
or adapter issue?)
Edit
I just had a small realization. It is pretty obvious now, but i’m somehow just now noticing/understanding this.
NOTE: Device
is sort of the central resource in my case.
get("/api/device")
doesn’t explicitly say it has sequences
since they are huge, and would probably lead to endless recursion.
BUT a device
most definitely has many sequences
.
It might just be that the data is presented to the consumer in the wrong way for replication.