Fl4m3Ph03n1x

Fl4m3Ph03n1x

What should I put in MyApp.Repo file?

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?

Most Liked

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.

APB9785

APB9785

Creator of ECSx
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.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement