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.

Choose Your Format(s)

  • $24.95 In Stock
    • Beta: What do I get?

  • Ebooks are DRM free.

  • Ebook delivery options.

About this Title

Pages: 225 (est)
Published: 2019-02-10
Release: B8.0 (2019-03-22)
ISBN: 978-1-68050-282-4

Elixir and Phoenix are taking the application development world by storm, and Ecto, the database library that ships with Phoenix, is going right along with them. There are plenty of examples that show you the basics, but to use Ecto to its full potential, you need to learn the library from the ground up.

This definitive guide starts with a tour of the core features of Ecto—repos, queries, schemas, changesets, transactions—gradually building your knowledge with tasks of ever-increasing complexity. Along the way, you’ll be learning by doing—a sample application handles all the boilerplate so you can focus on getting Ecto into your fingers.

Build on that core knowledge with a series of recipes featuring more advanced topics. Change your pooling strategy to maximize your database’s efficiency. Use nested associations to handle complex table relationships. Add streams to handle large result sets with ease. Based on questions from Ecto users, these recipes cover the most common situations developers run into.

Whether you’re new to Ecto, or already have an app in production, this title will give you a deeper understanding of how Ecto works, and help make your database code cleaner and more efficient.

What You Need

To follow along with the book, you should have Erlang/OTP 19+ and Elixir 1.4+ installed. The book will guide you through setting up a sample application that integrates Ecto.

Contents & Extracts

This book is currently in beta, so the contents and extracts will change as the book is developed.

  • Introduction
  • Ecto Fundamentals
    • Getting Started With Repo
      • Ecto and Elixir
      • Ecto Modules
      • 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 A 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
    • 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
    • Optimizing iEX For Ecto
    • Using Schemas Without Tables
    • Changing Your Database Pooling Strategy
    • Adding Streaming And High Throughput
    • Working With Database Functions


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 the web team lead 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. He works as a developer at Forza Football.