Using Ecto outside Phoenix in Elixir projects

The common approach for most people building elixir projects is to use phoenix, a productive, reliable and fast elixir web framework and a very often you start thinking that the dependencies that phoenix comes with are part of phoenix.
A popular example is Ecto, a toolkit for data mapping and language integrated query for Elixir. You can use Ecto without phoenix in your elixir projects just like every other OTP application out there and vice versa.
Setup
This quick example demonstrates how to use Ecto with Postgres in your mix projects. Create a supervised elixir application using in this command
mix new no_phoenix --sup
Ecto runs on its own BEAM processes, hence we need to add it as part of the supervision tree to ensure that our application will start up Ecto and also restarts it when it crashes.
Add Dependencies
Next, add Ecto and Postgres dependencies to the mix mix.exs
file.
defp deps do [ {:ecto_sql, "~> 3.0"}, {:postgrex, ">= 0.0.0"} ]end
Create the Repository
Create the Ecto repository using this MIX task
mix ecto.gen.repo -r NoPhoenix.Repo
This will generate a file repo.ex
inside lib/no_phoenix/
directory and config/config.exs
. Let’s make some little addition to this file. Modify application.ex
to start your Ecto repo as part of the supervision tree
children = [ NoPhoenix.Repo]
And include this config line in config/config.exs
file
config :no_phoenix,
ecto_repos: [NoPhoenix.Repo]
Set up the database connection parameters
config :no_phoenix, NoPhoenix.Repo, database: "no_phoenix", username: "user", password: "pass", hostname: "localhost"
Use mix ecto.create
to create the database. You can as well use an existing database
The files final files look like this:
application.ex
config/config.exs
mix.exs
Creating tables and manipulation data
You can manually create tables in Postgres without Ecto. But let’s keep everything Ecto. Here come Ecto migrations, we will create a table called scorers to save the top goal scorers in the Nigerian football professional league 2019 using this command mix ecto.gen.migration add_scorers_table
this creates a folder and also generates priv/repo/migrations/20191116081837_add_scorers_table.exs
file
Our table will be simple, just 4 columns: name, team, goals and primary id which is auto-generated. Add the fields to the migration file
add :name, :string, size: 40add :team, :string, size: 20add :goals, :integer
Your final migration should look like this
priv/repo/migrations/20191116081837_add_scorers_table.exs
Run the migration using mix ecto.migrate
. That's all. Everything should be good
Running the app
From the terminal, we can run iex -S mix
to start the REPL We will insert some records into the database using Ecto. Let’s alias our Repo for easy reference alias.NoPhoenix.Repo
so we can refer to it as just Repo.
Insert the scores in REPL using Repo.insert_all("scorers", scores)
Your REPL should display similar results as shown below. {4, nil}
means we inserted for four records
iex(3)> Repo.insert_all("scorers", scores)12:42:06.926 [debug] QUERY OK db=1.2ms decode=1.7ms queue=0.6msINSERT INTO "scorers" ("goals","name","team") VALUES ($1,$2,$3),($4,$5,$6),($7,$8,$9),($10,$11,$12) [8, "Mfon Udoh", "Akwa United", 6, "Usman Musa", "Gombe United", 6, "Yaya Kone", "Lobi Stars", 6, "Ndifreke Effiong", "Kano Pillars"]{4, nil}iex(4)>
Read more about Ecto here https://hexdocs.pm/ecto/Ecto.html. Example code repo: https://github.com/shubie/ecto-no-phonix
Hit me up on Twitter @afegbuas. Till next time. e go be ✌️