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
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 modeldoctors
.$include: :fat_patients
- Include the assoicationpatients
. Which has association withdoctors
.$select
- Select the fields fromhospital
androoms
.$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.
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 fromhospital
androoms
.$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.
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 assoicationdoctors
.$include: :fat_patients
- Include the assoicationpatients
which has association withdoctors
.$select
- Select the fields fromhospital
androoms
.$where
- Added the where attribute in the query.$order
- Sort the result based on the order attribute.$join
- Join thedoctors
table withhospital
.
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 assoicationdoctors
.$include: :fat_patients
- Include the assoicationpatients
. Which has association withdoctors
.$select
- Select the fields fromhospital
androoms
.$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.
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 assoicationdoctors
.$select
- Select the fieldsfrom FatEcto.FatHospital
androoms
.$where
- Added the where attribute in the query.$order
- Sort the result based on the order attribute.
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 assoicationdoctors
.$include: :fat_patients
- Include the assoicationpatients
. Which has association withdoctors
.$select
- Select the fields fromhospital
androoms
.$where
- Added the where attribute in the query.$order
- Sort the result based on the order attribute.
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 fromhospital
androoms
.$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.
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 fromhospital
androoms
.$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 fromhospital
androoms
.$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.