Friday, June 24, 2016

Understanding Elixir’s Ecto Querying DSL: The Basics

This article will take a look at the basics of querying with Ecto, a domain-specific language (DSL) for writing queries and interacting with databases in Elixir. This will involve going through joins, associations, aggregation functions, and so on.

A basic knowledge of Elixir is assumed, and knowing the fundamentals of Ecto beforehand will help too.

Ectoing Application

All of the examples in this article series can be run against my demonstrative Ectoing application. I highly encourage you to set this up (as described below) and run all of the queries as you read along. This will help to solidify your understanding by playing around with the examples to see what works and what doesn't work.

Let's quickly set up the application:

git clone http://ift.tt/28ScwyY
cd ectoing
mix deps.get

# don't forget to update the credentials in config/config.exs
mix ecto.create
mix ecto.migrate

# populate the database with some dummy data
mix run priv/repo/seeds.exs

(I've chosen to use MySQL for this. The examples throughout this article should work uniformly across all supported databases, so whilst the Mariaex dependency could be switched out to use another database, I would advise against this. This is because some examples in the next article will contain MySQL-dependent code.)

The database structure is as follows:

Ectoing application ERD

Continue reading %Understanding Elixir’s Ecto Querying DSL: The Basics%


by Thomas Punt via SitePoint

No comments:

Post a Comment