Unlocking the Power of Ecto: A Comprehensive Guide

Ecto is a powerful database interface tool for Phoenix applications, providing a robust set of features to interact with your database. In this article, we’ll delve into the world of Ecto, exploring its different components, including Ecto.Repo, Ecto.Schema, and Ecto.Changeset.

Getting Started with Ecto

To begin, let’s create a new Phoenix application and set up our database. We’ll use a simple e-commerce application as an example, with three tables: stores, products, and users.

bash
mix phx.new catalog

Next, we’ll configure our database credentials in config/dev.exs and run ecto.create to create our database.

Understanding Ecto Components

Ecto consists of four main components:

  1. Ecto.Repo: Repositories provide an abstraction on the data store, allowing us to create, update, destroy, and query existing entries.
  2. Ecto.Schema: Schemas are used to map external data into Elixir structs, defining the structure of our data.
  3. Ecto.Query: Queries enable us to retrieve information from a given repository, using a secure and composable syntax.
  4. Ecto.Changeset: Changesets allow us to track and verify changes to our data before applying them to the database.

Working with Ecto Schemas

Let’s create a schema for our stores table:
“`elixir
defmodule Catalog.Store do
use Ecto.Schema

schema “stores” do
field :name, :string

timestamps()

end
end

We can then use this schema to create a new store:
elixir
alias Catalog.Store

store = %Store{name: “My Store”}
Repo.insert(store)
“`
Using Ecto Changesets

Changesets allow us to filter, cast, and validate our data before applying it to the database. Let’s create a changeset for our products table:
“`elixir
defmodule Catalog.Product do
use Ecto.Schema

schema “products” do
field :name, :string
field :price, :integer

timestamps()

end

def changeset(product, params) do
product
|> cast(params, [:name, :price])
|> validate_required([:name, :price])
end
end

We can then use this changeset to create a new product:
elixir
alias Catalog.Product

product = %Product{}
changeset = Product.changeset(product, %{name: “My Product”, price: 10})
Repo.insert(changeset)
“`
Associations with Ecto

Ecto provides several types of associations, including hasmany/belongsto, hasone/belongsto, and many-to-many. Let’s create an association between our stores and products tables:
“`elixir
defmodule Catalog.Store do
use Ecto.Schema

schema “stores” do
field :name, :string
has_many :products, Catalog.Product

timestamps()

end
end

defmodule Catalog.Product do
use Ecto.Schema

schema “products” do
field :name, :string
field :price, :integer
belongs_to :store, Catalog.Store

timestamps()

end
end

We can then use this association to retrieve a store's products:
elixir
store = Repo.get(Store, 1)
products = Repo.preload(store, :products).products
“`
This is just a brief introduction to the world of Ecto. With its powerful features and flexible syntax, Ecto is an essential tool for any Phoenix application.

Leave a Reply

Your email address will not be published. Required fields are marked *