Composing Queries

With the addition of join we can start doing some larger and more detailed queries. This is a good time to look at one of the features of Query that simplifies working with complicated queries: composability. Ecto allows us to break up large queries into smaller pieces that can be reassembled at will. This makes them easier to work with, and allows you to re-use parts of queries in more than one place. This next section will show you exactly how to do that, and along the way, you’ll learn a little bit more about how queries are constructed under the hood.

Let’s say that we wanted to look up all of the albums by Miles Davis. Using joins, this query is straightforward:

  q = from a ​in​ ​"​​albums" ...

Get Programming Ecto now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.