rethinkdb_ecto
rethinkdb_ecto copied to clipboard
Associations
I spent a bit of time playing with associations.
Definite steps forward: Use Ecto to manage associations. has_one
, has_many
, belongs_to
, embeds_one
, embeds_many
, etc. are all very well built and easy to inspect at runtime.
Uncertainty: How do we read and write?
Ecto does preloading on reads it via Ecto.Query. We would need an alternative approach.
Read
I see two approaches that could provide this functionality.
- Provide wrappers for the Schemas to include the preloaded data:
Repo.get!(Post |> preload([:comments]), id)
- Accept an option for to include the preloaded data:
Repo.get!(Post, id, preload: [:comments])
Writes
Ecto.Changeset can handle all the writes just fine. Insert and Update should work exactly like ecto.
Purely on syntax, I prefer the second option - I don't know how I feel about a pipe operator inside an argument.
@martimatix Repo.get!(preload(Post, [:comments]), id)
would work just as well.
From slack:
Post |> preload([:comments]) |> Repo.query(query_with_join)
I actually do like that.