Customize Your Repo

Add more functionalities to Repo

We won’t need to do much to our Repo module for many projects. With that one use Ecto.Repo call, we’ll have access to everything we need. But there may be times when we need to call some particular Repo functions repeatedly with the same set of options, or maybe we’d want to add some behavior that Repo doesn’t currently have. Fortunately, the Repo module we created is a plain old Elixir module just like any other, so it’s possible to add customized behavior by adding more functions.

Aggregate function

In an instance where we are going to do a lot of counting in our music application, such as how many albums we have, how many artists, and so on, getting the number of records in a table is pretty easy with Repo’s aggregate function. Here’s how we can see how many albums we have:

Get hands-on with 1400+ tech skills courses.