Dlacreme

Dlacreme

Do you organize/separate your Ecto Schemas?

Hi guys,

I’ve been working on a few Phoenix applications by now, and something I keep changing is the way I organize my Schemas, because somehow, I’m never satisfied.

At the beggining, my schemas were in my context folder (lib/app/account/user.ex) however as soon as my context gets bigger, my schemas slowly fades out among the other files. I think it hard for a teammate to know which file is a schema and which is not.
After this, I tried to group all my schemas in a separate folder (lib/app/schemas/user.ex) and while I like the idea, I still had this problem that sometimes, a schema and a context file have the same name. I have to use alias x, as: xSchemas in some files, so overall, it’s confusing because of the naming changes from one file to another.

Anyway, today I need to setup a new project and here I am, having a subfolder schemas in my context folder and also adding the suffix ‘Schema’ to all my schemas to avoid name shadowing (lib/app/account/schemas/user_schema.ex). But I am also considering using plural for schemas only and keeping them in my schemas folder.

At this point I guess you understood that I am very picky with readability and organization. I think Elixir and Phoenix are amazing for this but this one thing keeps me up at night.

Curious to know if any of you already found the ultimate answer, I haven’t found anything on this forum nor Github !

Cheers,

Marked As Solved

stefanchrobot

stefanchrobot

How about using plural for the context? So you’d have:

lib
  - accounts
    - account.ex
    - user.ex
  - accounts.ex

Also Liked

dimitarvp

dimitarvp

I usually always start with lib/<app>/schema/ and only move to smaller and more contained directories as the app starts to grow.

baldwindavid

baldwindavid

I don’t think there is an ultimate answer, but I tend to start out simple and expand as needed.

For example, I might start out with…

lib
  - inventory
    - property.ex - schema
  - inventory.ex - context

You’re just talking about schemas here, but as more concepts get added, eventually the high-level including schemas might be…

lib
  - inventory
    - properties.ex - sub-context with property crud and such
    - property.ex - schema
    - units.ex - sub-context with unit crud and such
    - unit.ex - schema
  - inventory.ex - context that delegates to functions in `properties.ex` and `units.ex`

Some apps will even break out a separate context/app that is only concerned with data persistence and then interact with that from the other business domain contexts. In those cases, I’d tend to break into /lib/<app>/schema, /lib/<app>/queries, etc. since it is a pure data layer.

I’ve done the analysis paralysis thing when it comes to this topic and it rarely seems worth it though. There is a decent chance that whatever organization that makes sense today might need to evolve in the future. I think that is okay. Elixir tends to make renames and restructuring fairly painless.

opsb

opsb

I’ve used this on several large phoenix projects and it’s always worked well. It’s easy browse the directory structure and also easy to find the right file when fuzzy searching in an editor.

Where Next?

Popular in Discussions Top

matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
WildYorkies
It seems that the more I read, the more I find Elixir users speaking about all the ways that Elixir is not good for x, y, and z use cases...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |&gt; https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement