Magik.PowerUpRepo behaviour (Magik v0.10.0) View Source
This module provide some helpers functions that common used with Repo
How to use
use Magik.PowerUpRepo
in your Repo module and all helper functions are ready for use
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
use Magik.PowerUpRepo
end
Link to this section Summary
Callbacks
Find the first entry in the database that match the filter
Fetch query by pagination parama
Preload entries return from Repo.paginate
Load query with given size and provide the result as a stream
Link to this section Callbacks
Specs
find(queryable :: Ecto.Queryable.t(), keyword() | map()) :: {:ok, item :: any()} | {:error, :not_found}
Find the first entry in the database that match the filter
case Repo.find(Product, brand_id: 1, is_active: true) do
{:ok, product} ->
IO.puts(product.name)
{:error, :not_found} ->
IO.puts("No product found")
end
Specs
paginate(query :: Ecto.Queryable.t(), paging_params :: map(), opts :: keyword()) :: {list(), map()}
Fetch query by pagination parama
Example
query = from(p in Product, where: p.quantity > 10)
{entries, paginator} = Repo.paginate(query, %{page: 2, size: 5})
Specs
Preload entries return from Repo.paginate
Example
{entries, paginator} =
from(p in Product, where: p.quantity > 10 )
|> Repo.paginate(%{page: 2, size: 5})
|> Repo.preload([:brand, :category])
Specs
stream_query(queryable :: Ecto.Queryable.t(), opts :: keyword()) :: Enumerable.t()
Load query with given size and provide the result as a stream
Options
- page_size: number of entry to load per batch
Example
from(p in Product, where: p.quantity > 10 )
|> Repo.stream_query(20)
|> Stream.map( & &1.name)
|> Enum.to_list