senconscious

senconscious

Package for sorting ecto query

EctoJuno is a package that provides parsing, validation and applying sorting parameters for your ecto queries on models. Currently sorting by joint query is not implemented, as well as custom validators for your parameters. Also you need elixir 1.14 to use this package. Hope this package will help someone not just me ). Looking for a feedback, so if you have any thoughts, please share.

Below is a sample usage of library (assuming you have User ecto schema and Repo module):

alias EctoJuno.Query.Sorting

def list_sorted_users_for_admin(params) do
 User
 |> Sorting.sort_query(User, params)
 |> Repo.all()
end

def list_sorted_users_for_manager(params) do
 User
 |> Sorting.sort_query([:id, :inserted_at], params)
 |> Repo.all()
end

First Post!

senconscious

senconscious

Released v0.2.0 version in which added sorting by joint query.
Let’s assume you have User and Post schemas, Repo module. And users has many posts. If you need sort users by post’s titles than your code will look like:

alias EctoJuno.Query.Sorting

def sort_users_by_posts do
 params = %{"sort_by" => "title", "sort_direction" => "desc"}

 User
 |> join(:left, [u], p in assoc(u, :posts), as: :posts)
 |> Sorting.sort_query(User, params, :posts)
 |> Repo.all()
end

In the next version there’ll be sorting templates: you can define which schema and or binding will be used based on sorting field you passing by

Last Post!

senconscious

senconscious

Released custom schema pickup based on provided sort_by field feature.

Example template definition:

defmodule Sample.UserSorting do
 use EctoJuno.Query.SortingTemplate

  def prepare_sorting_params("post_" <> field) do
   {Post, field, :posts}
  end

  def prepare_sorting_params(nil) do
   {User, "inserted_at"}
  end

  def prepare_sorting_params(field) do
   {User, field}
  end
end

Example usage:

# Sort by post's title column
User
|> join(:left, [u], p in assoc(u, :posts), as: :posts)
|> UserSorting.sort_query(%{sort_by: "post_title", sort_direction: "desc"})
|> Repo.all()

# Sort by user's age column
User
|> join(:left, [u], p in assoc(u, :posts), as: :posts)
|> UserSorting.sort_query(%{sort_by: "age", sort_direction: "desc"})
|> Repo.all()

Where Next?

Popular in RFCs Top

sodapopcan
EDIT: I forgot to link the repo, ha: GitHub - sodapopcan/vials: Tweak your mix generators · GitHub This project is not quite ready for p...
New
Billzabob
Hello! I have an idea for an Elixir library I’d like to work on, but wanted to get some thoughts from the community first. It would allo...
New
KristerV
I got fed up with removing unused aliases manually so made a package for it: GitHub - KristerV/remove_unused_ex: Remove unused aliases an...
New
brettbeatty
At my work we build a lot of mix tasks for backfills and other maintenance tasks. Even with OptionParser we seem to copy a lot of boilerp...
New
Hedgehog-ai
Hello I would like feedback on an experimental neuroevolution (including substrate encoding) library called Bardo based on the amazing wo...
New
laibulle
Hello, I am playing with quantitative finance with Elixir. This library is more a way for me to explore and learn in this area and especi...
New
borodark
Hey folks, I’m building Zed (not editor) a declarative BEAM deploy tool for FreeBSD and illumos that uses ZFS user properties as the stat...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement