This article was originally published on Serokell's blog – https://serokell.io/blog/ecto-guide-for-beginners.
If you have tried your hand at doing web development with Phoenix, you have definitely run into Ecto. It’s the go-to database library for Elixir programmers. Unfortunately, Phoenix tutorials usually don’t cover Ecto deeply enough to develop an intuition of why things are the way they are.
In this article, we’ll instead try to understand how Ecto works. First, we’ll use Ecto to play around with a basic blog database. After that, we’ll dive deeper into different Ecto’s modules and what they offer.
What is Ecto?
Ecto is the go-to database toolkit in the Elixir ecosystem, usually used to interact with SQL databases like Postgres and MySQL. It is very powerful and can be used for all the interaction with databases you need, such as inserting, validating, changing, and querying data.
It has four main components:
Schema. Schemas are maps from database tables into Elixir structs; the module provides everything you need to create them.
Changeset. Changesets help validate the data that you want to insert into the database or modify.
Repo. This is the main point through which any interaction with the database is called.
Query. A macro-powered DSL for doing composable queries with Elixir-like syntax.
You are free to use any combination of these modules in your application, according to your requirements. There is no need to use all of them.
What are the benefits of using Ecto?
Most likely, the choice of using Ecto will come as a consequence of other choices that you’ve made. If you are using Phoenix and Elixir, the chance is you will use Ecto. 🙃
Generally, Ecto offers the same benefits as other things in the Elixir ecosystem: we focus on modularity and explicitness and of course it has macros.
Explicit. Ecto is explicit about what is happening: there is not so much behind-the-scenes magic as in other libraries. For example, if you don’t preload associated items, the framework will not fetch them for you. While it can cause some headaches in the start, it prevents you from making mistakes that you will forever remember when your project goes viral on Product Hunt.
Configurable. Ecto is very flexible. For example, by default, Ecto is made to connect to SQL databases like MySQL & PostgreSQL. But you can technically use it to connect to whichever data source your heart prefers like MongoDB or ETS, among other things.
Pick-and-choose. While Ecto is frequently called a framework, it consists of a few modules, most of which you can use or not use, depending on your preferences. Schema-less queries, pure SQL instead of Ecto.Query, just using changesets for data validation – you want it, you can do it. I’m not judging.
Ecto tutorial: create a database for a blog
Okay, now that you have a sense of what Ecto is and what it is used for, let’s delve right in!
In this tutorial, we’ll set up a basic blog database for a Phoenix project using Ecto. It will contain users, posts, and comments.
Set up the project
First, let’s create a blank Phoenix project.
mix phx.new blog
After that, cd blog and run mix ecto.create. This will create the database for the project.
Design the database
In our database, we will have three tables.
Users. It will have a field for username.
Posts. It will have a field for post text. It will also have a foreign key referencing a user.
Comments. It will have a field for comment text. It will also have a foreign key that references a user and a foreign key that references a post.
For the reasons of simplicity, we’ll skip categories, tags, titles, meta titles, and other fluff.