Topic

Ecto and PostgreSQL

This page was updated 13 Dec - 2020

Ecto it the database wrapper for mainly Postgresql. If you come from Ruby on Rails it is used instead of ActiveRecord but it is not an ORM. Ecto is a library and domain-specific language to interact with your database. It has four main components that can be used together all by itself.

Ecto.Schema

use Ecto.Schema

schema "products" do
  field :description, :string
  field :name, :string

  timestamps()
end

Ecto.Changeset

import Ecto.Changeset

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

Ecto.Repo

Ecto.Query

import Ecto.Query

def list_active_products do
  from(
    p in Product,
    where: p.active == true,
    order_by: p.name
    order_by: [desc: p.name, desc: p.inserted_at],
    limit: 10
  )
  |> Repo.all()
end

Ecto and PostgreSQL Tutorials

Below are the latest tutorial on the topic of Ecto and PostgreSQL

Published 03 Feb - 2020

Setup a has_many / belongs_to in Phoenix

Something I do in EVERY project is to setup some sort of relation between resources. And even though Phoenix comes with generators for migrations and CRUD opeartions, you still need to modify the code to suite for the relations you want. In this short tutorial, I will go through my usual steps regarding setting up a basic has_many / belongs_to - relation.

Continue Reading

Published 10 Feb - 2020

Add an use a JSONB field in Phoenix and Ecto

PostgreSQL has native support for objects stored as JSON as actually binary JSON (or JSONB). With JSONB format, you can add index do the column for faster and more efficient reads. Even though the format supports both storing arrays of maps and nested maps, I suggest making it simple and just use an unnested single map. The use case I am going for is have some sort of userentered product attributes that I later can search and filter on.

Continue Reading

Published 11 Feb - 2020

Add Tags with Ecto has_many, through in Phoenix - Tagging part 1

I want to add tags to products. And as usual there are a situation where a product can have many tags and a tag can belong to many products. However, in my opnion, the best option is to use a 'has_many :through' relationship. So for this, I need a table called tags and a join table called taggings. In this tutorial, I will go through how would do this.

Continue Reading

Published 15 Feb - 2020

Create ghost loading cards in Phoenix LiveView

Unless you already didn't know, when a LieView component is mounted on a page, it runs the mount/2 function twice. One when the page is rendered from the initial request, and one one when the socket is connected. So if you have data that is slow or resource heavy to load, you should consider to wait until the socket is connected so the intial load is not in vein. A popular approach is to use ghost cards. A ghost gard is a loading placeholder that looks similar to the content that you try to load in but without actual data. In this tutorial I will show you how

Continue Reading

Published 31 Aug - 2020

Fuzzy find with Ecto in Phoenix LiveView

Fuzzy find is both a simple and a complex thing. Even though though it's simple to implement, its hard to get right from a UX perspective. Luckily, I can help you get going with the technological implementation.

Continue Reading

Published 02 Sep - 2020

Table sorting with Ecto and LiveView

A very common or even mandatory feature in e-commerce stores is the ability to sort a list of products by attributes. This is easy enough and a good fit for Phoenix LiveView. In this tutorial, I will build on an existing page that displays a list of products and implement sorting on product name and prices.

Continue Reading