HarisudhanRavi
How best to handle multiple results with Repo.get/3?
I recently saw this YouTube video by a React dev writing a get_by_id function:
He uses findFirst() from some javascript ORM. Even though it’s not an issue, it feels weird to me, because id is usually a primary key in the database and is a unique index. There is no need to use findFirst to query by primary key.
But Ecto handles it differently using Repo.get/3. It throws an exception if there is more than one result which seems to be straightforward.
I am a complete Elixir Dev and have little experience with JS using Vue. I am not sure about ORMs in other languages. Is this a good code using this kind of query by a primary key? How do other languages do this?
PS: I know this is an Elixir form. Since good design patterns is a recent topic in the Elixir community, I thought this would be a place to discuss this.
Most Liked
cmo
There are other options available, e.g. findUniqueOrThrow, which is more akin to Repo.get.
tfwright
Yes I would say this is the better way to implement this kind of API and the JS version is overly defensive and thus simply less correct .
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance











