Connecting to the database
In the introduction, we hinted at how we'll use Ecto to store our application data in a PostgreSQL database. Ecto is able to connect to many databases, but for each database it needs the respective driver to establish the connection, support queries, and so on.
Â
In our case we'll use the postgrex
library, which provides the PostgreSQL Elixir driver. But don't think Ecto is tied to PostgreSQL in any way, because Ecto supports other databases like MySQL, DynamoDB, and SQLite, among others.
Let's start by adding the ecto
and postgrex
libraries to our elixir_drip
umbrella app dependencies:
$ cat apps/elixir_drip/mix.exs defmodule ElixirDrip.Mixfile do use Mix.Project defp deps do [ {:postgrex, "~> 0.13"}, {:ecto, "~> 2.1"}, # ... ] end end
These two dependencies will be fetched by mix
as soon as we run mix deps.get
. The next step consists of defining and configuring the Ecto repository. Every database operation passes through it,...