hamiltop / rethinkdb_ecto

Shim library for using Ecto with RethinkDB. Not a full adapter.
24 stars 4 forks source link

Associations #11

Open hamiltop opened 8 years ago

hamiltop commented 8 years ago

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.

  1. Provide wrappers for the Schemas to include the preloaded data:

Repo.get!(Post |> preload([:comments]), id)

  1. 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.

martimatix commented 8 years ago

Purely on syntax, I prefer the second option - I don't know how I feel about a pipe operator inside an argument.

hamiltop commented 8 years ago

@martimatix Repo.get!(preload(Post, [:comments]), id) would work just as well.

hamiltop commented 8 years ago

From slack:

Post |> preload([:comments]) |> Repo.query(query_with_join)

I actually do like that.