small medium large xlarge

Programming Ecto: Build Database Apps in Elixir for Scalability and Performance


Cover image for Programming Ecto

Programming Ecto

Build Database Apps in Elixir for Scalability and Performance


Languages may come and go, but the relational database endures. Learn how to use Ecto, the premier database library for Elixir, to connect your Elixir and Phoenix apps to databases. Get a firm handle on Ecto fundamentals with a module-by-module tour of the critical parts of Ecto. Then move on to more advanced topics and advice on best practices with a series of recipes that provide clear, step-by-step instructions on scenarios commonly encountered by app developers. Co-authored by the creator of Ecto, this title provides all the essentials you need to use Ecto effectively.

Customer Reviews

Let’s face it, Ecto is not a small library. I think Darin and Eric did a fantastic job
of breaking it all down into understandable pieces, giving a ton of examples along
the way. You’ll learn how to use Ecto, and perhaps more importantly, how it was
meant to be used. And if you think you know it very well already, I’m sure you’ll
learn new things too!

- Wojtek Mach

Hex Core Team, Consultant, Plataformatec

It does a great job of not only explaining how to use Ecto, but also illuminating
the SQL underneath and the design decisions that the Ecto team made when
building it. For a new team, this would be a great book to teach with; likewise,
for an experienced team, this makes for a good reference book.

- Ben Marx

Software Architect, Bleacher Report

This is a useful guide for beginners, but also a great resource for developers that
have a medium level of knowledge of Ecto. I learned new tricks reading this book.

- Ulisses De Almeida

Elixir Developer, author of "Learn Functional Programming with Elixir"

For many Elixir projects, Ecto is your most important partner, so it’s important
to learn to use it effectively. Darin and Eric have created a wonderful roadmap to
help beginners and experienced developers explore its powerful feature set. It’s
full of clear examples that will help you quickly master Ecto—finish this book and
you’ll be well on your way to creating powerful, scalable, reliable, and maintainable
database applications.

- Bryan Stearns

Senior Software Engineer and Consultant

An eloquent discussion of the tools Ecto provides for database programming in
Elixir: testing with sandboxes, changesets, embedded schemas, polymorphic associations,
and much more. This book will be a reference for most engineers
working in Elixir and Ecto.

- Matt Milton

Software Engineer, Enbala Power Networks

See All Reviews

Choose Your Format(s)

  • $24.95 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

What You Need

To follow along with the book, you should have Erlang/OTP 20+ and Elixir 1.5+ installed, along with either PostgreSQL (preferred) or MySQL. The book will guide you through setting up a sample application that integrates Ecto.

Contents & Extracts

  • Introduction
  • Ecto Fundamentals
    • Getting Started with Repo
      • Ecto and Elixir
      • Ecto Modules
      • How Ecto Is Organized
      • Setting Up the Sample App
      • The Repository Pattern
      • The Repo Module
      • Putting Our Repo to Work
      • Customizing Your Repo
      • Wrapping Up
    • Querying Your Database
      • Query Basics
      • Refining Our Results with where
      • Working with Joins
      • Composing Queries
      • Other Ways to Use Queries
      • Wrapping Up
    • Connecting Your Tables to Elixir Structs with Schemas
      • Creating Schemas
      • Writing Queries with Schemas
      • Inserting and Deleting with Schemas
      • Adding Associations to Schemas
      • Working with Associations in Queries
      • Optimizing Associations with Embedded Schemas
      • Deleting Records with Associations
      • Using Schemas to Seed a Database
      • Wrapping Up
    • Making Changes with Changesets excerpt
      • Introducing Changesets
      • Casting and Filtering
      • Validating Your Data
      • Capturing Errors
      • Using Changesets Without Schemas
      • Working with Associations
      • Wrapping Up
    • Making Multiple Changes with Transactions and Multi
      • Running Transactions with Functions
      • Running Transactions with Ecto.Multi excerpt
      • Wrapping Up
    • Making Changes to Your Database
      • Introducing Migrations
      • Your First Migration
      • Running Migrations
      • Rolling Back Migrations
      • Adding Indexes
      • Changing Data and Table Structure Together
      • Specifying Up and Down Operations
      • Changing Default Behaviors
      • Wrapping Up
  • Ecto Applied
    • Adding Ecto to an Elixir Application Without Phoenix
      • Creating a New Project
      • Adding Ecto’s Dependencies
      • Creating Your Repo Module
      • Adding Ecto to the Supervision Tree
      • Using Multiple Ecto Repos
      • Starting Your App
      • Wrapping Up
    • Working with Changesets and Phoenix Forms
      • Generating a Form for a Single Schema
      • Displaying Changeset Errors
      • Creating a Form with an Association
      • Creating a Form with Multiple Associations
      • Wrapping Up
    • Testing With Sandboxes
      • Setting Up an Async Test
      • Changing the Ownership Mode
      • Safely Sharing Connections with Allowances
      • Wrapping Up
    • Creating and Using Custom Types
      • Building on Top of Ecto’s Types
      • Adding Custom Types Without the Built-In Types
      • Wrapping Up
    • Inserting and Updating with Upserts
      • Performing Upserts Without Schemas
      • Performing Upserts with Schemas
      • Wrapping Up
    • Optimizing Your Application Design
      • Separating the Pure from the Impure
      • Working with Contexts
      • Working with Umbrella Applications
      • Wrapping Up
    • Working with Embedded Schemas
      • Creating Embedded Schemas
      • Adding Embeds to Another Schema
      • Making Changes
      • Choosing Between Embedded Schemas and Associations
      • Wrapping Up
    • Creating Polymorphic Associations
      • Polymorphism in Other Frameworks
      • Approach #1: Multiple Foreign Keys
      • Approach #2: Using an Abstract Schema
      • Approach #3: Using many_to_many
      • Wrapping Up
    • Optimizing IEx for Ecto
      • Adding Imports and Aliases
      • Adding Helper Functions
      • Wrapping Up
    • Using Schemas Without Tables
      • Downsides to Locking Schemas to Tables
      • Breaking Up the Artist Schema
      • Creating Table-less Schemas
      • Saving the Table-less Structs
      • Wrapping Up
    • Tuning for Performance
      • Preparing to Optimize
      • Optimizing Queries
      • Executing Bulk Operations
      • Fetching Large Datasets with Streams
      • Wrapping Up


Darin Wilson has been coding professionally since the days when you got help with coding problems by posting a message to a BBS over a 300-baud modem. Over the years, he’s developed apps for banking, education, television, social media, and robotic arms. He is currently a principal software engineer at Infinite Red, a web and mobile application development company.

Eric Meadows-Jönsson is a member of the Elixir core team and the creator of Ecto. He is a passionate open source developer and is heavily involved in the Elixir community.