FatEcto v0.1.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

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 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 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_aggregate(queryable, opts_aggregate) View Source
Link to this function build_avg(queryable, field) View Source
Link to this function build_count(queryable, field) View Source
Link to this function build_count_distinct(queryable, field) View Source
Link to this function build_group_by(queryable, opts_group_by) 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

query_opts = %{
  "$select" => %{
    "$fields" => ["name", "location", "rating"],
    "fat_rooms" => ["beds", "capacity"]
  },
  "$order" => %{"id" => "$desc"},
  "$where" => %{"rating" => 4},
  "$group" => "total_staff"
}

iex> 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, opts_include, 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

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> 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, opts, 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

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> 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,
     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_max(queryable, field) View Source
Link to this function build_min(queryable, field) View Source
Link to this function build_order_by(queryable, opts_order_by) 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

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> 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, opts_select, 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

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> 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_sum(queryable, field) View Source
Link to this function build_where(queryable, opts_where, 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

query_opts = %{
  "$select" => %{
    "$fields" => ["name", "location", "rating"],
    "fat_rooms" => ["beds", "capacity"]
  },
  "$order" => %{"id" => "$desc"},
  "$where" => %{"location" => %{"$not_like" => "%addre %"}},            
  "$group" => "total_staff"
}

iex> 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_opts) 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

query_opts = %{
"$find" => "$all",
"$select" => %{"$fields" => ["name", "rating"], "fat_rooms" => ["beds"]},
"$where" => %{"id" => 10}
}

iex> build(FatEcto.FatHospital, query_opts)

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.

It will only work if you have configured your repo in the fat_ecto config.

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

query_opts = %{
"$find" => "$all",
"$select" => %{"$fields" => ["name", "rating"], "fat_rooms" => ["beds"]},
"$where" => %{"id" => 10}
}

iex> build(FatEcto.FatHospital, query_opts)

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.

It will only work if you have configured your repo and default limit in the fat_ecto config.