solnic

solnic

Drops.Relation - High-Level Relation Abstraction on top of Ecto

Hey folks,

I’m back with a new library for y’all :slight_smile: Gonna x-post from my blog:


I’m excited to announce the latest addition to the Elixir Drops suite of libraries: Drops.Relation. This new library provides a high-level API for defining database relations with automatic schema inference and composable queries, simplifying database interactions when developing applications in Elixir.

Drops.Relation is based on 10 years of my work on the Ruby Object Mapper project and brings the most powerful features of ROM to Elixir.

What is Drops.Relation?

Drops.Relation bridges the gap between Ecto and application-level data handling and management. It automatically introspects your database tables, generates Ecto schemas, and provides a convenient query API that feels like working directly with Ecto.Repo while adding powerful composition features.

To put it simply it makes you develop applications faster and simplifies maintanance.

Think of it as a smart wrapper around Ecto that eliminates boilerplate while adding sophisticated query composition capabilities, while preserving your full control over your relations and their schemas.

Getting Started

Add it to your mix.exs:

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

Configure it in your config.exs:

config :my_app, :drops,
  relation: [
    repo: MyApp.Repo
  ]

Then run the installation task:

mix drops.relation.install

This should create aliases for ecto tasks that will ensure that schemas are refreshed whenever you run migrations.

To test it out, just run migrations:

mix ecto.migrate

If things go well, you will see this at the end of the output:

Cache refresh completed

From there, you can start defining your relations with inferred schemas :sparkles:

Automatic Schema Inference

One of Drops.Relation’s standout features is automatic schema inference. Instead of manually defining every field, type, and constraint, you can let the library introspect your database:

defmodule MyApp.Users do
  use Drops.Relation, otp_app: :my_app

  schema("users", infer: true)
end

The library automatically discovers:

  • Column names and types
  • Primary and foreign keys
  • Indexes and constraints
  • Default values and nullability

You can access the generated schema programmatically:

schema = MyApp.Users.schema()
schema[:email]
# %Drops.Relation.Schema.Field{
#   name: :email,
#   type: :string,
#   source: :email,
#   meta: %{
#     default: nil,
#     index: true,
#     type: :string,
#     primary_key: false,
#     foreign_key: false,
#     nullable: false
#   }
# }

Familiar Repository API

Drops.Relation provides all the familiar Ecto.Repo functions you’re used to but makes them more accessible:

# Reading data
user = Users.get(1)
user = Users.get_by(email: "john@example.com")
users = Users.all()
users = Users.all_by(active: true)

# Writing data
{:ok, user} = Users.insert(%{name: "John", email: "john@example.com"})
{:ok, user} = Users.update(user, %{name: "Jane"})
{:ok, user} = Users.delete(user)

# Aggregations
count = Users.count()
avg_age = Users.aggregate(:avg, :age)

Composable Queries

Where Drops.Relation really shines is in query composition. You can chain query operations together to build complex queries:

# Basic composition
active_users = Users
|> Users.restrict(active: true)
|> Users.order(:name)
|> Enum.to_list()

# Complex restrictions with multiple conditions
admins = Users
|> Users.restrict(role: ["admin", "super_admin"])
|> Users.restrict(active: true)
|> Users.order([{:last_login, :desc}, :name])

# Works seamlessly with Enum functions
user_names = Users
|> Users.restrict(active: true)
|> Enum.map(& &1.name)

In the first release Drops.Relation provides 3 high-level query operations:

  • Drops.Relation.restrict - filters data based on conditions
  • Drops.Relation.order - sets ordering
  • Drops.Relation.preload - preloads associations

More operations will be added in future releases.

Custom Queries with defquery

For more complex scenarios, you can define reusable query functions using the defquery macro:

defmodule MyApp.Users do
  use Drops.Relation, otp_app: :my_app

  schema("users", infer: true)

  defquery active() do
    from(u in relation(), where: u.active == true)
  end

  defquery by_role(role) when is_binary(role) do
    from(u in relation(), where: u.role == ^role)
  end

  defquery by_role(roles) when is_list(roles) do
    from(u in relation(), where: u.role in ^roles)
  end

  defquery recent(days \\ 7) do
    cutoff = DateTime.utc_now() |> DateTime.add(-days, :day)
    from(u in relation(), where: u.inserted_at >= ^cutoff)
  end

  defquery with_posts() do
    from(u in relation(),
         join: p in assoc(u, :posts),
         distinct: u.id)
  end
end

These custom queries are fully composable with built-in operations:

# Compose custom queries
recent_admins = Users
|> Users.active()
|> Users.by_role("admin")
|> Users.recent(30)
|> Users.order(:name)
|> Enum.to_list()

# Mix with restrict operations
active_users_with_email = Users
|> Users.active()
|> Users.restrict(email: {:not, nil})
|> Users.order(:email)

Advanced Feature: Boolean Query Logic

For complex query logic involving multiple conditions, Drops.Relation provides a powerful query macro with boolean operations:

import Drops.Relation.Query

# Simple AND operation
adult_active_users = Users
|> query([u], u.active() and u.adult())
|> Enum.to_list()

# Complex nested conditions
complex_query = Users
|> query([u],
    (u.active() and u.adult()) or
    (u.inactive() and u.with_email())
  )
|> Users.order(:name)
|> Enum.to_list()

# Mix built-in and custom operations
filtered_users = Users
|> query([u],
    u.active() and
    u.restrict(role: ["admin", "user"])
  )
|> Enum.to_list()

What’s Next?

Future releases will include:

  • Enhanced association handling
  • Type-safe high-level query handling
  • More composable query operations
  • Integration with other Elixir Drops libraries
  • Generators for Phoenix

Try It!

Drops.Relation is available on Hex.pm and the source code is on GitHub.

The library is currently in its early stages, testing and feedback are welcome!

Give it a try and let me know what you think! I’m always interested in feedback and contributions from the community :purple_heart:


Where Next?

Popular in Libraries Top

tompave
Hello there, I would like to share a feature toggles library (AKA feature flags) I’ve been working on. The main package is FunWithFlags...
New
nikokozak
Hello all, I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
New
blatyo
https://www.conduitframework.com/ The best overview for how things are tied together is this presentation. Modules and functions are pre...
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New
Crowdhailer
Experimenting with this code. OK.try do user <- fetch_user(1) cart <- fetch_cart(1) order = checkout(cart, user) save_or...
New
vic
Expat is a tiny experiment I did for extracting patterns and being able to reuse them (compose and share patterns between elixir librarie...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Sub Categories:

We're in Beta

About us Mission Statement