sheharyarn
I’m writing (my first) hex package to introduce “Ecto shortcuts” in an elixir/phoenix app. I was getting really tired of doing this all the time:
PhoenixApp.Repo.all(User)
# and
PhoenixApp.Repo.get(User, 2)
It should allow you to simply do:
User.all
User.get(2)
# etc...
I have a few concerns with this project, especially if I might be going against Elixir patterns. I would really appreciate if I could get some feedback on the code before I publish it.
See the Code here
Currently, my main concerns are:
- Is the naming scheme correct? (i.e. is
Ecto.Shortcutsa good name?) - Is doing this a good idea?
- Are there any anti-patterns in the code?
- Am I using macros correctly?
- What should I keep in mind while publishing this as a hex package?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
I’m using an Umbrella project for a Phoenix application, and I want to have one Ecto Repo and one PostgreSQL database shared by all apps....
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
Other Trending Topics
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
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
- #blog-post
- #ai
- #phoenix_html
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Qqwy
Welcome! Great that you’re creating a Hex package.
Let’s try to answer your questions:
Your naming scheme is correct. While any names for packages are of course allowed, the advised convention is to indeed use
Foo.Barif you wrote a packageBarthat extends the more general packageFoo. In cases there exists both aBarand aFooand you wrote some kind of compatibility-layer between them, the order doesn’t matter (so you could go for e.g. alphabetical order there). So the nameEcto.Shortcutsis great!Arguably. Some people will agree, others won’t. I myself would say that it is nice to be able to shorten Repo-based things inside your Controllers, (and saying that you basically should never talk to the Repo anywhere else). If Ecto.Shortcuts is the nicest way of doing so, is again very much a matter of taste.
It does mean that your ‘model’ is again directly connected with the repository, which is something that Ecto itself explicitly didn’t do. If you’re for instance dealing with multiple repositories that you want to switch between (a common example of this is Sharding), this is a problem.
The most important thing that might be considered an ‘antipattern’ is that your code is still missing documentation. In Elixir, documentation is a first-class member of the language, and most people try to add documentation to their functions.
I don’t see any other obvious anti-patterns at first glance. A really helpful tool to increase the quality of your code is Credo, which tries to point out some anti-patterns and inconsistencies in your code.
The way you are using
.
__using__/1is correctBesides the fact that documentation is important, be sure to use Semantic Versioning for your package, because this means that when other people list a certain version as dependency requirement, their code will not break if you decide update your package.
sheharyarn
Awesome! Thank you for your feedback!
Once I get all of the functionality working, I’ll start working on the documentation. One more question, how would the tests look like for it? I have a very limited experience with TDD, and I’m not exactly sure how would I go about writing tests for macros.
Qqwy
You’re very welcome!
Test Driven Development means that you first write tests and then fill in your code so they pass, which is not what you’re doing here. (Which is, of course, also a fine approach.
Each way has its own advantages and drawbacks.)
The thing you probably want to test here, and also the way you can test the macro, is by simply creating a few modules that include the
using Ecto.Shortcutsline. You could then test if the commands are properly passed on to the actual Repo when you call them on your test modules.Another possible test is to test what happens if
using Ecto.Shortcutsis added to a module that actually does not contain an Ecto schema.If you need inspiration, feel free to pop open any of Elixir’s core libraries, such as for instance Ecto itself to see how they write tests there. Most of them are very understandable and of (at least in my opinion) a high quality.
And, especially when your modules start getting more complex, it might be worthwhile to check out José Valim’s post on using Mocks during testing.
josevalim
The issue with the approach above is that you are coupling your schema with one particular database and the schema is designed to represent any data source, which may not even be a database in the first place. We explore some of those things here: Ecto’s insert_all and schemaless queries « Plataformatec Blog
My concern with using something such as shortcuts is that people will forget those basic tenets schemas and repositories were built on. So I would rather define modules such as
MyApp.Accounts.get_user(id), which is the direction Phoenix is taking, without a need to tie repositories directly into schemas.sheharyarn
I’ve decided to go with the name Ecto.Rut
The intention here is to offer simple wrappers around
Ecto.Repomethods and simplify common use-cases in Ecto. This can be a means to ease people into the “Ecto way” or just simply reduce code repetition. But I do agree with you; complicated statements should use Ecto’s rich and flexible DSL.perishabledave
FYI theres another library that does about the same thing:
https://github.com/MishaConway/ecto_shortcuts
In any case, congratulations on your first package!
Misha
Those sound eerily familiar… is it from the README of my own project you seem to have ripped off and claimed as your own?
Anyways, I don’t want to get petty on this forum, but I had to call out what seems like plagiarism to me. Ironically, this thread was a good run down of the issues to address in own project since it was reviewing a proxy of it.
Before I knew about this, I was already in the process of adding complete test coverage and updating to support the new Phoenix idiom that Jose pointed out. Though I am slightly annoyed, it is good validation that I was working on the right upgrades.
Sorry to introduce myself to this community in such a negative light, but I hope to have a lot of positive things to contribute in the future.
sheharyarn
I’ll be very honest @Misha, when I started writing the package I did not even know yours existed. The code I originally shared for review was this:
When I tried to publish it, it was only then that I realized
ecto_shortcutshad already been taken. When Jose asked me about it I was unable to articulate what I wanted this package to do and ripped off two lines from your README (which i thought explained it pretty well). I’m very sorry for that, but I’d like you to compare the rest of the readme and source code. I hope you’ll see that my plagiarism ended at the two lines.When I learn a new language, I try to publish scripts, write packages and use it in production and try to do as much of it as myself, that’s the only way I learn - it’s just that my english isn’t that good. This was exactly that. Again, I’m very sorry.