Defrag

Defrag is a lightweight Elixir library that provides a convenient macro for writing Ecto fragments using familiar string interpolation syntax. It makes your database queries more readable and maintainable by allowing you to use string interpolation instead of multiple question marks in your Ecto fragments.

Features

Installation

If available in Hex, the package can be installed by adding defrag to your list of dependencies in mix.exs:

def deps do
  [
    {:defrag, "~> 0.1.0"}
  ]
end

Usage

# Import the macro
import Defrag

# Instead of writing:
fragment("date_part('year', ?)", some_date)

# You can write:
defrag("date_part('year', #{some_date})")

# Multiple interpolations are supported:
defrag("BETWEEN #{start_date} AND #{end_date}")
# Becomes: fragment("BETWEEN ? AND ?", start_date, end_date)

In Ecto Queries

import Ecto.Query
import Defrag

def users_born_in_year(query, year) do
  from u in query,
  where: defrag("date_part('year', #{u.birth_date}) = #{^year}")
end

Documentation

Detailed documentation can be found at HexDocs.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.