robinmonjo

robinmonjo

Is it good practice to have a context be the plural of a schema?

Hello all,

I’m migrating my app to the new Phoenix 1.3 guidelines. It’s a really simple app and I was wondering if it’s ok to name a context the plural form of the schema ?

I have a User schema and I’m pretty sure it won’t end up with accounts and stuff. Is it ok to have the context be “users” ?

And what does this means ? def update_user(%User{} = user, attrs), do: I don’t get the %User{} = user in the params ? Is it pattern matching to ensure user is a %User{} struct ?

Thanks for your answers :slight_smile:

Most Liked

robinmonjo

robinmonjo

Thank you for your answer @thomasbrus ! It starts to make sense.

I think that I should think about the context module as an entry point for a domain of my application. So if my app handle users (persistence/schema, registration, authentication etc …) I’m free to create a “Users” context with the users.ex file exposing all the API of my domain and have as much module as I like in the “Users” context. I could have something like this:

users/
   users.ex # exposing my Users context API
   user.ex # containing the schema
   user_repo.ex # containing the changesets / validations etc ...
   auth.ex # containing logic for the auth

As for the pattern matching I was aware of the possibility to directly destructure the parameter, but it was weird to see this %User{} = user because it doesn’t destructure any values so I didn’t see how %User{} = user is “better” than just user. But it makes sense now:

defmodule Test do                   
   def func(%{} = m), do: IO.inspect(m)
end
Test.func("hello") # no function clause matching
Test.func(%{test: "test"}) # ok

So it’s more than just destructuring it’s “nested” pattern matching :slight_smile:

thomasbrus

thomasbrus

From what I understand, the “purpose” of the context module is that it is an entry point for interacting with the modules/entities within that context and that it defines a clear boundary between those modules and the rest of your application. An internal API if you will.

However, since it seems that the user scheme is the only thing in that context (whose name you already have trouble coming up with!), I think it would make sense to place the User module at the root level. It is always still an option to group it together with other modules later on that belong to the same context (Authentication.User, Authentication.Registration, ..).

And what does this means ? def update_user(%User{} = user, attrs), do: I don’t get the %User{} = user in the params ? Is it pattern matching to ensure user is a %User{} struct ?

It is a pattern match yes, when the function is invoked the actual value is matched against the function arguments from right to left: %User{} = user = %User{name: "robinmonjo", ...}. So it indeed also verifies the right hand side is a user struct.

(You may also directly capture fields of the struct via this syntax:)

def update_user(%User{name: name} = user, attrs)

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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
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
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement