achempion

achempion

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.

Showing Posts 1 to 10

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.

NobbZ

NobbZ

Not sure how this is related to ecto efficiency…

Anyway, what is requireing you to import or alias? If its just for some onetime thing, why not use the fully qualified module and function names?

Alternatively you could also set up a .iex.exs in your project that does all the preparational imports and aliases for you.

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.

achempion

achempion OP

Thanks for all the feedback, I’ve changed the title to “Ecto usage patterns”.

Adzz

Adzz

Completely agree, I love the explicitness but I do feel like sometimes that you have to jump through a lot of hoops. But I would say don’t be afraid to write higher level abstractions!

I wrote ecto_morph to help with things like that, take a look if you are interested GitHub - Adzz/ecto_morph: morph your Ecto capabilities into the s t r a t o s p h e r e ! · GitHub

But it would be totally acceptable to create Active Record style helpers if you needed / wanted them in your application.

fuelen

fuelen

Wow, I created a library with very similar API called Composite (Composite - a library for writing dynamic queries) which allows to handle a lot of complex cases.

edisonywh

edisonywh

Oh hey that’s really similar indeed! I haven’t seen your library anywhere, the only similar ones I’ve found were TokenOperator & QueryBuilder.

That’s pretty cool! Good sign that the idea isn’t entirely out of whack :slight_smile: Have you been using it in prod & have you found any issues?

fuelen

fuelen

Exactly this implementation - no, it is in our todo list. In production, we use simplified version of it. Composite is an enhanced version after real production experience and a lot of local code reviews.

al2o3cr

al2o3cr

It’s definitely possible to write these in comparably-short ways, once you’ve done import Ecto.Query:

User |> order_by(desc: :id) |> limit(1) |> Repo.one()

and

Repo.get_by(User, email: "email") |> Ecto.assoc(:posts) |> Repo.all()

These assume you have your schema and repo aliased in, but that isn’t required - full names will work just as well.

hauleth

hauleth

Well, in this case I would prefer:

User |> where(email: "email") |> preload(:posts) |> Repo.one()

As it can reduce amount of queries and round trips.

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 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
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
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews