Dlacreme

Dlacreme

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,

Showing Posts 1 to 4

stefanchrobot

stefanchrobot

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

lib
  - accounts
    - account.ex
    - user.ex
  - accounts.ex
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.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
nseaSeb
AcmeScript — Writing JS hooks as if I were still using Elixir I’ve been having fun building a little something over the last few days: Ac...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews