FatEcto v0.5.0 FatEcto.FatQuery.FatInclude View Source

Preload associated schemas based on the conditions passed in the query params.

Parameters

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

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["location", "rating"],
...>    "fat_rooms" => ["name", "floor"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => %{"fat_patients" => %{}},
...>      "$order" => %{"id" => "$desc"},
...>      "$where" => %{"experience_years" => 3},
...>      "$join" => "$right"
...>    }
...>  }
...> }
iex> Elixir.MyApp.Query.build!(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in assoc(f0, :fat_doctors), left_join: f2 in assoc(f1, :fat_patients), where: f0.rating == ^4 and ^true, where: f1.experience_years == ^3 and ^true, order_by: [desc: f1.id], order_by: [desc: f0.id], limit: ^34, offset: ^0, select: map(f0, [:location, :rating, {:fat_rooms, [:name, :floor]}]), preload: [[fat_doctors: [:fat_patients]]]>

Options

  • $include - Include the assoication doctors.
  • $include: :fat_patients- Include the assoication patients which has association with doctors.
  • $select - Select the fields from hospital and rooms.
  • $where - Added the where attribute in the query.
  • $order - Sort the result based on the order attribute.
  • $join - Join the doctors table with hospital .

Link to this section Summary

Link to this section Functions

Link to this function

build_include(queryable, include_params, model, build_options)

View Source
build_include(
  any(),
  nil | binary() | maybe_improper_list() | map(),
  any(),
  any()
) :: any()

Preload associated schemas.

Parameters

  • queryable - Ecto Queryable that represents your schema name, table name or query.
  • include_params - Include query options as a map.
  • model - schema name of the table.
  • build_options - Pass options related to otp_app.

Examples

iex> query_opts = %{
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => %{"fat_patients" => %{}},
...>      "$order" => %{"id" => "$desc"},
...>      "$where" => %{"experience_years" => 3},
...>      "$join" => "$right"
...>    }
...>  }
...> }
iex> Elixir.FatEcto.FatQuery.FatInclude.build_include(FatEcto.FatHospital, query_opts["$include"], FatEcto.FatHospital, [])
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in assoc(f0, :fat_doctors), left_join: f2 in assoc(f1, :fat_patients), where: f1.experience_years == ^3 and ^true, order_by: [desc: f1.id], limit: ^10, offset: ^0>