[004.2] Ecto Basics

Ecto Basics
[04.17.2017]

Today we're going to start exploring Ecto by beginning to build out the data
layer for the Firestorm Forum. It's a
forum, so we'll have Users, Categories, Threads, and Posts. Let's get started.

Project

We're starting with a basic umbrella
app.
We haven't covered these yet, but I've linked to a great primer for them. They
make it easy to create independent Elixir applications inside of a single
repository and use them as dependencies. The individual apps live inside of the
apps directory, so let's cd into there and create a firestorm_data
application that will serve as our data layer.

cd apps
mix new --sup firestorm_data
cd firestorm_data

We're going to be using Ecto, so let's pull that in as a dependency.

vim mix.exs

We'll also need to pull in postgrex since we're using Ecto with PostgreSQL.

A repository maps to an underlying data store, controlled by the adapter.

In essence, a Repo is how you ultimately interact with your data store.

We can create a repo like so:

mix ecto.gen.repo -r FirestormData.Repo

This created a Repo module for us and a bit of configuration. It also tells us
to perform a couple of manual tasks:

Don't forget to add your new repo to your supervision tree
(typically in lib/firestorm_data.ex):
supervisor(FirestormData.Repo, [])
And to add it to the list of ecto repositories in your
configuration files (so Ecto tasks work as expected):
config :firestorm_data,
ecto_repos: [FirestormData.Repo]

Let's do that (though the application actually lives at
lib/firestorm_data/application.ex rather than where the hint suggests):

# This file is responsible for configuring your application# and its dependencies with the aid of the Mix.Config module.useMix.Configconfig:firestorm_data,ecto_repos:[FirestormData.Repo]import_config"#{Mix.env}.exs"

We've also uncommented the last line - this allows us to have environment
specific configuration. We'll configure environments for dev and test,
ignoring prod for now:

Here I use a configuration that also lets you override the user, password, and
host with environment variables if you'd like. This is what I use on Heroku, and
I figured I might as well show it off now. Let's copy this file to
config/test.exs, changing the database name:

Here I've also allows the database to be renamed since that's something that I
use in continuous integration environments. We also specified that we want to
use the Ecto.Adapters.SQL.Sandbox pool. This is necessary for testing purposes
later.

NOTE: You might need slightly different configuration, depending on how your
PostgreSQL database is set up. There are good hints for this in Ecto's Getting
Started Guide.

So now we have our Repo configured, at least for test and dev
environments. Let's see what the generated lib/firestorm_data/repo.ex file
looks like:

This just generated a data structure that defines our query - it does not
execute it. That's because, since it hasn't discussed our Repo yet, it
has no clue where to query it at all. We can pass this query to Repo.one to
fetch a single record from the database.

sign up for full access

Meet your expert

I've been building web-based software for businesses for over 18 years. In the last four years I realized that functional programming was in fact amazing, and have been pretty eager since then to help people build software better.