-
Notifications
You must be signed in to change notification settings - Fork 1
/
order_by.ex
37 lines (31 loc) · 948 Bytes
/
order_by.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
defmodule EctoForge.Extension.Get.OrderBy do
@moduledoc """
# Use with your `EctoForge.CreateInstance`
```elixir
use EctoForge.CreateInstance,
extensions_get: [
EctoForge.Extension.Get.OrderBy,
],
repo: MyApp.Repo
```
## When you coonnect
You can use Api https://hexdocs.pm/ecto/Ecto.Query.html#order_by/3
### Example
```elixir
{:ok, []} = MyApp.Model.get_all(order_by: [asc: :name, desc_nulls_first: :population])
```
"""
import Ecto.Query
use EctoForge.CreateExtension.Get
def before_query_add_extension_to_get(_module, _mode, _repo, _l_ex, query, nil) do
{query, %{}}
end
def before_query_add_extension_to_get(_module, _mode, _repo, _l_ex, query, attrs) do
{order_by_attrs, _} = Access.pop(attrs, :order_by)
if is_list(order_by_attrs) or is_map(order_by_attrs) do
{order_by(query, ^order_by_attrs), attrs}
else
{query, attrs}
end
end
end