Skip to content

Commit fb01bb6

Browse files
committed
Add repo opts to custom_index_query and custom_show_query callbacks
1 parent e827667 commit fb01bb6

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

lib/kaffy/resource_query.ex

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ defmodule Kaffy.ResourceQuery do
2525
current_offset
2626
)
2727

28-
custom_query = Kaffy.ResourceAdmin.custom_index_query(conn, resource, paged)
29-
current_page = Kaffy.Utils.repo().all(custom_query)
28+
{current_page, opts} =
29+
case Kaffy.ResourceAdmin.custom_index_query(conn, resource, paged) do
30+
{custom_query, opts} ->
31+
{Kaffy.Utils.repo().all(custom_query, opts), opts}
32+
33+
custom_query ->
34+
{Kaffy.Utils.repo().all(custom_query), []}
35+
end
3036

3137
do_cache = if search == "" and Enum.empty?(filtered_fields), do: true, else: false
32-
all_count = cached_total_count(schema, do_cache, all)
38+
all_count = cached_total_count(schema, do_cache, all, opts)
3339
{all_count, current_page}
3440
end
3541

@@ -47,8 +53,11 @@ defmodule Kaffy.ResourceQuery do
4753
def fetch_resource(conn, resource, id) do
4854
schema = resource[:schema]
4955
query = from(s in schema, where: s.id == ^id)
50-
custom_query = Kaffy.ResourceAdmin.custom_show_query(conn, resource, query)
51-
Kaffy.Utils.repo().one(custom_query)
56+
57+
case Kaffy.ResourceAdmin.custom_show_query(conn, resource, query) do
58+
{custom_query, opts} -> Kaffy.Utils.repo().one(custom_query, opts)
59+
custom_query -> Kaffy.Utils.repo().one(custom_query)
60+
end
5261
end
5362

5463
def fetch_list(_, [""]), do: []
@@ -60,10 +69,12 @@ defmodule Kaffy.ResourceQuery do
6069
|> Kaffy.Utils.repo().all()
6170
end
6271

63-
def total_count(schema, do_cache, query) do
72+
def total_count(schema, do_cache, query, opts \\ [])
73+
74+
def total_count(schema, do_cache, query, opts) do
6475
result =
6576
from(s in query, select: fragment("count(*)"))
66-
|> Kaffy.Utils.repo().one()
77+
|> Kaffy.Utils.repo().one(opts)
6778

6879
if do_cache and result > 100_000 do
6980
Kaffy.Cache.Client.add_cache(schema, "count", result, 600)
@@ -72,10 +83,12 @@ defmodule Kaffy.ResourceQuery do
7283
result
7384
end
7485

75-
def cached_total_count(schema, false, query), do: total_count(schema, false, query)
86+
def cached_total_count(schema, do_cache, query, opts \\ [])
87+
88+
def cached_total_count(schema, false, query, opts), do: total_count(schema, false, query, opts)
7689

77-
def cached_total_count(schema, do_cache, query) do
78-
Kaffy.Cache.Client.get_cache(schema, "count") || total_count(schema, do_cache, query)
90+
def cached_total_count(schema, do_cache, query, opts) do
91+
Kaffy.Cache.Client.get_cache(schema, "count") || total_count(schema, do_cache, query, opts)
7992
end
8093

8194
defp get_filter_fields(params, resource) do

0 commit comments

Comments
 (0)