Ecto Modules

Learn about the modules of Ecto.

Modules of Ecto

Ecto’s core functionality is contained in six main modules. In Part I, we’ll look at each of them in detail. Later in this chapter, we’ll dive into Repo.

Repo

The Repo module is the heart of Ecto and acts as a kind of proxy for our database. All communication to and from the database goes through Repo.

Query

The Query module contains Ecto’s powerful but elegant API for writing queries. Here, we’ll find everything we need to pull the data we want out of our database and make precise changes.

Schema

A schema is a kind of map from database tables to our code. The Schema module contains tools to help us create these maps. The best part is Ecto schemas are very flexible. We’re not confined to a simple one-to-one relationship between our tables and structs. This allows for whole new levels of expressiveness when creating our data structures.

Changeset

Many database layers have one or two kinds of change. Ecto understands that one size does not fit all, so it provides the changeset, a data structure that captures all aspects of changing our data. The Changeset module provides functions for creating and manipulating changesets, allowing us to structure our changes safely, flexibly, and enable them to be easily tested.

Transaction

We often need to coordinate several database changes simultaneously, where they must all succeed or fail together. The transaction function works great for simple cases, but the Multi module can handle even complex cases while keeping our code clean and testable.

Migration

Change happens. As our application grows and evolves, so does the underlying database. Changing the structure of a database can be tricky, particularly when multiple developers are involved, but Migration helps us coordinate these changes so that everyone stays in sync.

Get hands-on with 1400+ tech skills courses.