Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

I created a default Phoenix application with Ecto using PSQL as a database. In this basic project I noticed there is a file under my_app called MyApp.Repo, which is basically empty:

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres
end

Problem

My first reaction was, “this is the file where I will place all my CRUD operations”.
But after some thinking, I realized this was not a great idea.

Simple as this project may be, I will have different flows that will do different things. I will have some functionality to create reserves, other to update clients, other for the products they buy …

Putting it all in one place would only create a big ball of mud.

Questions

So this led me to a couple of questions regarding Elixir and its standards:

  • What are we supposed to put on this file?
  • Is there a community standard for how you organize a project in regards to how you access the database?

Showing Posts 1 to 8

D4no0

D4no0

I would generally advice to not put anything in this file.

The main scope of this file is to simply generate the necessary repo functions based on the features provided by your underlying database engine, hence why it’s defined here and not in the application config. For example if you are using a database that doesn’t support transactions, calling Repo.transact or similar functions will raise a compile-time warning, making it easier to make sense of it.

The only exception I can see where it would make sense is if you are planning on adding additional core functionality for your repo that is not supported out of the box and requires you manually calling the adapter functions.

sodapopcan

sodapopcan

Furthermore, there are also callbacks which go in this module.

APB9785

APB9785

Creator of ECSx
garrison

garrison

You can put repo-related utility functions in there like the above transact(). In one of my projects I put an explain_all() function in there (which simply calls explain(:all, ...)) just to make it easier to quickly debug a query.

See the contexts guide.

Eiji

Eiji

At first time it looks empty and useless as “nothing fits there” at least better than in other places. However that’s because we don’t see many things. :see_no_evil:

  1. As mentioned by others lots of the code is generated, so in fact Repo modules brings lots of useful features out-of-the-box with almost no code. :male_detective:

  2. Secondly Repo-specific functionality (of course except said generated functions) is rare. For example one of the things you can put there is a logic for soft_delete. However many projects does not use this and other potential features that fit well in Repo module. :bulb:

For those reasons Repo modules may be a bit confusing, but the deeper you go the more you find. It’s like an untouched mountain full of gold that’s just waiting to be discovered. :heart_eyes:

sodapopcan

sodapopcan

I have some stuff like this but like to wrap in if Mix.env() != :prod do to ensure they don’t leak into production code. Repo.first is another one I usually throw in there (for dev/test only).

sbuttgereit

sbuttgereit

Remember that innocent line, use Ecto.Repo, is itself bringing a large amount of functionality into the module filling out much of the typical purpose of the module. For many, that’s going to be sufficient on its own without needing to go further.

In an application I’m working on, I’ve actually got a fair amount of code there… but I’m also a bit off the standard Ecto implementation. My application uses dynamically defined and started datastores (meaning at runtime), so the Repo doesn’t really represent a single database in my application, but rather is the means by which to start, stop, and manage multiple repositories. So putting this management code in the Repo module makes sense with this broader definition. But again, most won’t have that.

In the end, I’d just leave you with… if you don’t have any specific, clear need of anything that’s best represented as being part of your “datastore”… don’t sweat it: those few lines are just fine and will take you far. Once there’s a real reason to put something there, you’ll likely be feeling it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews