def get_available_internal_users do
user_coll = from(u in User,
select: {u.name, u.role, u.id},
where: u.role != "Client") |> Repo.all
for {name, role, id} <- user_coll, into: %{}, do: {{name, role}, id}
end
Just wrote this function to get all users from my project’s DB which do not have the role of ‘Client’ and put their name, role and id into a map.
Such nice, concise and readable code.
First I was doing a lot of List.first/1 and List.last/1 and Tuple.to_list/1 to get the values into the map. Then it came to me “why not just pattern match the tuple and use the matches to get them into the Map?” and boom… that code came to fruition ![]()
Honestly can’t think of any other language that would allow me to query a SQL DB, parse the data and make it all so readable and short. There isn’t a need to write comments really, it reads by itself.






















