FatEcto v0.2.0 FatEcto.FatQuery View Source

Entry Point module for building queries.

import or alias it inside your module.

Link to this section Summary

Functions

Call the respective query method depending on the params.

Parameters

Build a group_by query depending on the params.

Parameters

Build a include query depending on the params.

Parameters

Build a join query depending on the params.

Parameters

Build a order_by query depending on the params.

Parameters

Build a select query depending on the params.

Parameters

Build a where query depending on the params.

Parameters

Fetch the result from the repo based on the query params

Apply limit and offset to the query if not provided and return meta

Link to this section Functions

Link to this function build(queryable, query_opts) View Source

Call the respective query method depending on the params.

Parameters

  • queryable- Schema name that represents your database model.
  • query_opts - include query options as a map

Examples

iex> query_opts = %{
...>   "$select" => %{
...>     "$fields" => ["name", "location", "rating"],
...>     "fat_rooms" => ["beds", "capacity"]
...>  },
...>   "$order" => %{"id" => "$desc"},
...>   "$where" => %{"rating" => 4},
...>   "$group" => "nurses",
...>   "$include" => %{
...>       "fat_doctors" => %{
...>           "$include" => ["fat_patients"],
...>           "$where" => %{"name" => "ham"},
...>           "$order" => %{"id" => "$desc"},
...>           "$join" => "$right"
...>          }
...>     },
...>   "$right_join" => %{
...>      "fat_rooms" => %{
...>        "$on_field" => "id",
...>        "$on_join_table_field" => "hospital_id",
...>        "$select" => ["beds", "capacity", "level"],
...>        "$where" => %{"incharge" => "John"}
...>       }
...>     }
...>  }
iex> build(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in "fat_rooms", on: f0.id == f1.hospital_id, right_join: f2 in assoc(f0, :fat_doctors), where: f0.rating == ^4 and ^true, where: f1.incharge == ^"John" and ^true, group_by: [f0.nurses], order_by: [desc: f0.id], select: merge(map(f0, [:name, :location, :rating, :id, {:fat_rooms, [:beds, :capacity]}]), %{^:fat_rooms => map(f1, [:beds, :capacity, :level])}), preload: [fat_doctors: #Ecto.Query<from f in FatEcto.FatDoctor, where: f.name == ^"ham" and ^true, order_by: [desc: f.id], limit: ^10, offset: ^0, preload: [:fat_patients]>]>

Options

  • $include- Include the assoication model 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.
  • $group- Added the group_by attribute in the query.
  • $order- Sort the result based on the order attribute.
  • $right_join- Specify the type of join.
  • $on_field- Specify the field for join.
  • $on_join_table_field- Specify the field for join in the joining table.
Link to this function build_group_by(queryable, group_by_params) View Source

Build a group_by query depending on the params.

Parameters

  • queryable- Schema name that represents your database model.
  • query_opts - include query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...>  "$group" => "total_staff"
...> }
iex> Elixir.FatEcto.FatQuery.build(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f in FatEcto.FatHospital, where: f.rating == ^4 and ^true, group_by: [f.total_staff], order_by: [desc: f.id], select: map(f, [:name, :location, :rating, :id, {:fat_rooms, [:beds, :capacity]}])>

Options

  • $select- Select the fields from hospital and rooms.
  • $where- Added the where attribute in the query.
  • $group- Added the group_by attribute in the query.
  • $order- Sort the result based on the order attribute.
Link to this function build_include(queryable, include_params, model) View Source

Build a include query depending on the params.

Parameters

  • queryable- Schema name that represents your database model.
  • query_opts - include query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"name" => "ham"},
...>      "$order" => %{"id" => "$desc"},
...>      "$join" => "$right"
...>    }
...>  }
...> }
iex> Elixir.FatEcto.FatQuery.build(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in assoc(f0, :fat_doctors), where: f0.rating == ^4 and ^true, order_by: [desc: f0.id], select: map(f0, [:name, :location, :rating, :id, {:fat_rooms, [:beds, :capacity]}]), preload: [fat_doctors: #Ecto.Query<from f0 in FatEcto.FatDoctor, left_join: f1 in assoc(f0, :fat_patients), where: f0.name == ^"ham" and ^true, order_by: [desc: f0.id], limit: ^10, offset: ^0, preload: [: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 function build_join(queryable, join_params, join_type \\ "$join") View Source

Build a join query depending on the params.

Parameters

  • queryable- Schema name that represents your database model.
  • query_opts - include query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"name" => "ham"},
...>      "$order" => %{"id" => "$desc"}
...>    }
...>  },
...>  "$right_join" => %{
...>    "fat_rooms" => %{
...>      "$on_field" => "id",
...>      "$on_join_table_field" => "hospital_id",
...>      "$select" => ["beds", "capacity", "level"],
...>      "$where" => %{"incharge" => "John"}
...>    }
...>  }
...> }
iex> Elixir.FatEcto.FatQuery.build(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, right_join: f1 in "fat_rooms", on: f0.id == f1.hospital_id, join: f2 in assoc(f0, :fat_doctors), where: f0.rating == ^4 and ^true, where: f1.incharge == ^"John" and ^true, order_by: [desc: f0.id], select: merge(map(f0, [:name, :location, :rating, :id, {:fat_rooms, [:beds, :capacity]}]), %{^:fat_rooms => map(f1, [:beds, :capacity, :level])}), preload: [fat_doctors: #Ecto.Query<from f0 in FatEcto.FatDoctor, left_join: f1 in assoc(f0, :fat_patients), where: f0.name == ^"ham" and ^true, order_by: [desc: f0.id], limit: ^10, offset: ^0, preload: [: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.
  • $right_join- Specify the type of join.
  • $on_field- Specify the field for join.
  • $on_join_table_field- Specify the field for join in the joining table.
Link to this function build_order_by(queryable, order_by_params) View Source

Build a order_by query depending on the params.

Parameters

  • queryable- Schema name that represents your database model.
  • query_opts - include query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$asc"},
...>  "$where" => %{"rating" => 4},
...>  "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"name" => "ham"},
...>      "$order" => %{"id" => "$desc"}
...>    }
...>  }
...> }
iex> Elixir.FatEcto.FatQuery.build(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, join: f1 in assoc(f0, :fat_doctors), where: f0.rating == ^4 and ^true, order_by: [asc: f0.id], select: map(f0, [:name, :location, :rating, :id, {:fat_rooms, [:beds, :capacity]}]), preload: [fat_doctors: #Ecto.Query<from f0 in FatEcto.FatDoctor, left_join: f1 in assoc(f0, :fat_patients), where: f0.name == ^"ham" and ^true, order_by: [desc: f0.id], limit: ^10, offset: ^0, preload: [:fat_patients]>]>

Options

  • $include- Include the assoication doctors.
  • $select- Select the fields from FatEcto.FatHospital and rooms.
  • $where- Added the where attribute in the query.
  • $order- Sort the result based on the order attribute.
Link to this function build_select(queryable, select_params, model) View Source

Build a select query depending on the params.

Parameters

  • queryable- Schema name that represents your database model.
  • query_opts - include query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"rating" => 4},
...> "$include" => %{
...>    "fat_doctors" => %{
...>      "$include" => ["fat_patients"],
...>      "$where" => %{"name" => "ham"},
...>      "$order" => %{"id" => "$desc"}
...>    }
...>  }
...> }
iex> Elixir.FatEcto.FatQuery.build(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f0 in FatEcto.FatHospital, join: f1 in assoc(f0, :fat_doctors), where: f0.rating == ^4 and ^true, order_by: [desc: f0.id], select: map(f0, [:name, :location, :rating, :id, {:fat_rooms, [:beds, :capacity]}]), preload: [fat_doctors: #Ecto.Query<from f0 in FatEcto.FatDoctor, left_join: f1 in assoc(f0, :fat_patients), where: f0.name == ^"ham" and ^true, order_by: [desc: f0.id], limit: ^10, offset: ^0, preload: [: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.
Link to this function build_where(queryable, where_params, opts \\ []) View Source

Build a where query depending on the params.

Parameters

  • queryable- Schema name that represents your database model.
  • query_opts - include query options as a map

Examples

iex> query_opts = %{
...>  "$select" => %{
...>    "$fields" => ["name", "location", "rating"],
...>    "fat_rooms" => ["beds", "capacity"]
...>  },
...>  "$order" => %{"id" => "$desc"},
...>  "$where" => %{"location" => %{"$not_like" => "%addre %"}},
...>  "$group" => "total_staff"
...> }
iex> Elixir.FatEcto.FatQuery.build(FatEcto.FatHospital, query_opts)
#Ecto.Query<from f in FatEcto.FatHospital, where: not(like(fragment("(?)::TEXT", f.location), ^"%addre %")) and ^true, group_by: [f.total_staff], order_by: [desc: f.id], select: map(f, [:name, :location, :rating, :id, {:fat_rooms, [:beds, :capacity]}])>

Options

  • $select- Select the fields from hospital and rooms.
  • $where- Added the where attribute in the query.
  • $not_like- Added the notlike attribute in the where query.
  • $group- Added the group_by attribute in the query.
  • $order- Sort the result based on the order attribute.
Link to this function fetch(queryable, query_params) View Source

Fetch the result from the repo based on the query params.

Parameters

  • queryable- Schema name that represents your database model.
  • query_opts - include query options as a map

Examples

iex> query_opts = %{
...>  "$find" => "$all",
...>  "$select" => %{"$fields" => ["name", "rating"], "fat_rooms" => ["beds"]},
...>  "$where" => %{"id" => 10}
...> }
iex> fetch(FatEcto.FatHospital, query_opts)
#Struct

Options

  • $find => $all- To fetch all the results from database.
  • $find => $one- To fetch single record from database.
  • $select- Select the fields from hospital and rooms.
  • $where- Added the where attribute in the query.

Apply limit and offset to the query if not provided and return meta.

Parameters

  • queryable- Schema name that represents your database model.
  • query_opts - include query options as a map

Examples

iex> query_opts = %{
...>  "$find" => "$all",
...>  "$select" => %{"$fields" => ["name", "rating"], "fat_rooms" => ["beds"]},
...>  "$where" => %{"id" => 10},
...>  "$limit" => 15,
...>  "$skip" => 0
...> }
iex> build(FatEcto.FatHospital, query_opts)
#Result

Options

  • $find => $all- To fetch all the results from database.
  • $find => $one- To fetch single record from database.
  • $select- Select the fields from hospital and rooms.
  • $where- Added the where attribute in the query.
  • $limit- Limit the number of records returned from the repo.
  • $skip- Used an an offset.

If no limit is defined in the query. FAT uses the default limit specified in the fat_ecto config.