Programming Ecto

Book description

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.

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.

Publisher resources

View/Submit Errata

Table of contents

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

Product information

  • Title: Programming Ecto
  • Author(s): Darin Wilson, Eric Meadows-Jonsson
  • Release date: April 2019
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680502824