FatEcto v0.5.0 FatEcto.FatQuery.FatOrderBy View Source

Builds query with asc or desc order.

=> $asc

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • query_opts - Include query options as a map.

Example

iex> query_opts = %{
...> "$select" => %{
...>   "$fields" => ["name", "location", "rating"],
...>   "fat_rooms" => ["floor", "name"]
...>  },
...>  "$where" => %{"name" => "saint claire"},
...>  "$group" => ["rating", "total_staff"],
...>  "$order" => %{"total_staff" => "$asc"},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>     "$include" => ["fat_patients"],
...>     "$where" => %{"rating" => %{"$gt" => 5}},
...>     "$order" => %{"experience_years" => "$asc"},
...>     "$select" => ["name", "designation", "phone"]
...>    }
...>   },
...>  "$right_join" => %{
...>    "fat_rooms" => %{
...>      "$on_field" => "id",
...>      "$on_table_field" => "hospital_id",
...>      "$select" => ["floor", "name", "is_active"],
...>      "$where" => %{"floor" => 10},
...>      "$order" => %{"name" => "$asc"}
...>     }
...>   }
...> }
iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in "fat_rooms", on: f0.id == f1.hospital_id, left_join: f2 in assoc(f0, :fat_doctors), where: f0.name == ^"saint claire" and ^true, where: f1.floor == ^10 and ^true, where: f2.rating > ^5 and ^true, group_by: [f0.rating], group_by: [f0.total_staff], order_by: [asc: f1.name], order_by: [asc: f2.experience_years], order_by: [asc: f0.total_staff], limit: ^34, offset: ^0, select: merge(map(f0, [:name, :location, :rating, {:fat_rooms, [:floor, :name]}]), %{^"fat_rooms" => map(f1, [:floor, :name, :is_active])}), preload: [[fat_doctors: [:fat_patients]]]>

Options

  • $select - Select the fields from hospital and rooms.
  • $right_join: :$select- Select the fields from rooms.
  • $include: :$select - Select the fields from doctors.
  • $right_join - Right join the table rooms.
  • $include - Include the assoication model doctors and patients.
  • $gt - Added the greaterthan attribute in the where query inside include .
  • $order - Sort the result based on the order attribute.
  • $right_join: :$order - Sort the result based on the order attribute inside join.
  • $include: :$order - Sort the result based on the order attribute inside include.
  • $group - Added the group_by attribute in the query.

=> $desc

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • query_opts - Include query options as a map.

Example

 iex> query_opts = %{
 ...> "$select" => %{
 ...>   "$fields" => ["name", "location", "rating"],
 ...>   "fat_rooms" => ["floor", "name"]
 ...>  },
 ...>  "$where" => %{"name" => "saint claire"},
 ...>  "$group" => ["rating", "total_staff"],
 ...>  "$order" => %{"rating" => "$desc"},
 ...>  "$include" => %{
 ...>    "fat_doctors" => %{
 ...>     "$include" => ["fat_patients"],
 ...>     "$where" => %{"rating" => %{"$gt" => 5}},
 ...>     "$order" => %{"experience_years" => "$asc"},
 ...>     "$select" => ["name", "designation", "phone"]
 ...>    }
 ...>   },
 ...>  "$right_join" => %{
 ...>    "fat_rooms" => %{
 ...>      "$on_field" => "id",
 ...>      "$on_table_field" => "hospital_id",
 ...>      "$select" => ["name", "floor", "is_active"],
 ...>      "$where" => %{"floor" => 10},
 ...>      "$order" => %{"floor" => "$desc"}
 ...>     }
 ...>   }
 ...> }
 iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
 #Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in "fat_rooms", on: f0.id == f1.hospital_id, left_join: f2 in assoc(f0, :fat_doctors), where: f0.name == ^"saint claire" and ^true, where: f1.floor == ^10 and ^true, where: f2.rating > ^5 and ^true, group_by: [f0.rating], group_by: [f0.total_staff], order_by: [desc: f1.floor], order_by: [asc: f2.experience_years], order_by: [desc: f0.rating], limit: ^34, offset: ^0, select: merge(map(f0, [:name, :location, :rating, {:fat_rooms, [:floor, :name]}]), %{^"fat_rooms" => map(f1, [:name, :floor, :is_active])}), preload: [[fat_doctors: [:fat_patients]]]>

Options

  • $select - Select the fields from hospital and rooms.
  • $right_join: :$select- Select the fields from rooms.
  • $include: :$select - Select the fields from doctors.
  • $right_join - Right join the table rooms.
  • $include - Include the assoication model doctors and patients.
  • $gt - Added the greaterthan attribute in the where query inside include .
  • $order - Sort the result based on the order attribute.
  • $right_join: :$order - Sort the result based on the order attribute inside join.
  • $include: :$order - Sort the result based on the order attribute inside include.
  • $group - Added the group_by attribute in the query.

Link to this section Summary

Functions

Order the results with respect to order_by clause in the params.

Link to this section Functions

Link to this function

build_order_by(queryable, group_params, build_options, opts \\ [])

View Source

Order the results with respect to order_by clause in the params.

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • order_by_params - Order_By query options as a map.
  • opts - Pass options related to query bindings.
  • build_options - Pass options related to otp_app.

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["name", "floor"]
...>  },
...>  "$order" => %{"id" => "$asc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"designation" => "ham"},
...>      "$order" => %{"id" => "$desc"}
...>    }
...>  }
...> }
iex> Elixir.FatEcto.FatQuery.FatOrderBy.build_order_by(FatEcto.FatHospital, query_opts["$order"], [], [])
#Ecto.Query<from f0 in FatEcto.FatHospital, order_by: [asc: f0.id]>