achempion

achempion

Ecto usage patterns

Hi,

My background is many years of ruby development. I was introduced to
Elixir couple of years ago and since then use it very often as it is
“go to” language for all my projects.

One main difference with ruby ecosystem for me is elixir is more
explicit. You have to define aliases, you have to import all required
functions and so on.

Although I like explicitness, I think in some cases it may slow down a
pace. As an example, often you need to check something in a database
in your console.

In order to do that with Ecto, you have do all imports, then you have
to remember all aliases for schemas and namespaces with context names.

I miss the ActiveRecord where you could write
User.last or User.find_by(email: "email").posts.

With Ecto you have to explicitly preload associations, import helper
functions and so on.

I’ve riched the point when I think about writing some wrapper/helper
which will load on console initialization and will define all aliases
in advance and automatically load associations on method calls.

I would like to know am I alone with this need and is it really an
issue. Maybe I need to have more experience with Ecto when I got used
to write all aliases/includes/preload every time I want to hack in a
console.

Thank you for any feedback on this topic.

Most Liked Responses

LostKobrakai

LostKobrakai

defmodule MyApp.Explore do
  defmacro __using__(_) do
    quote do
      alias App.Repo
      alias App.UserContext.User
      import Ecto.Query
    end
  end
end

and then you only need to call use MyApp.Explore whenever you need to do something with your data.

edisonywh

edisonywh

Don’t think it has anything to do with “Ecto efficiency”.

Automatically preloading association was one of the biggest source of performance issues in Rails with N+1 queries, I am really glad that Ecto does not do that by default, it forces you to understand what exactly your code needs (I love when libraries nudges you to good, explicit/obvious patterns).

Anyway, I do like User.find_by(email: "email") style of code (which you can also use Repo.get_by() btw, but I was missing it for context functions, so I created a small library here GitHub - edisonywh/condiment: 🍡 Add flavors to your context function without the hassles. · GitHub that helps make this easier.

EDIT: Like nobbz pointed out as well, you can define a .iex.exs file, on the app level or on the global level, and that saves you a lot of typing too.

LostKobrakai

LostKobrakai

Personally I’d evaluate the reasons for “hack(ing) in the console”. If you do the same thing over and over again it should have some proper API to call and if it’s different things all the time I’m wondering why those need to happen in the first place. For just looking at the db state I personally just use a GUI for the db.

Where Next?

Popular in Discussions Top

sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
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
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18146 126
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
jsonify
So, is Heroku the only free option for hosting Phoenix/Elixir at this point? I’m not ready to commit to paying monthly and was wondering ...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

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
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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

We're in Beta

About us Mission Statement