DEV Community

Cover image for Fast Ordered Model in Phoenix with ecto_list
Sofiane Baddag
Sofiane Baddag

Posted on

Fast Ordered Model in Phoenix with ecto_list

Why?

I was coding a paid video service with a system of Serie/Video. A serie has several videos ordered according to a defined order.
I was surprised by how longer it took me to build this feature and by the lack of solution provided by the community. As I realized that I may encounter this problem again, say, if I build a Trello clone, I thought I have to do something.

Here comes ecto_list !

What?

Ecto_list is both a library and a tutorial with a set of snippets that helps you build ordered model with Ecto rapidly. No LiveView involved !

How does it work?

There is 2 ways to save the order of items:

  • in the items themselves (like act_as_list Ruby gem) : i.e. the Videos
  • in the model they belong to: i.e. the Serie.

Ecto_list takes the latter option. Why ? Because we want to avoid making several SQL query to update all ordering for one item only and, also, we assume we preload all the items anyway. That’s why it makes sense to save the order in the model that has many items.

This library is super easy to understand. There is one function at the moment that does the magic of ordering the items according to a defined order: EctoList.ordered_items_list/2 . The rest is adding the right snippets at the right parts of the source code.

Conclusion

This is my first library ever ( yeaah! πŸŽ‰). There is still room for improvements so if you think we can add some interesting features, feel free to contribute.

Links:

Oldest comments (0)