Skip to content

Commit d1271e3

Browse files
authored
Merge pull request #172 from gushonorato/improve_query_callbacks
Allow pass opts to Repo in custom_index_query and custom_show_query callbacks
2 parents 18eb8fb + d4c4936 commit d1271e3

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,19 @@ It is called when fetching the resources for the index page.
538538

539539
The `custom_show_query/3` is identifical to `custom_index_query/3`, but works when fetching a single resource in the show/edit page.
540540

541+
It's also possible to pass `opts` to the Repo operation, in this case, you just have to return a tuple instead, like below:
542+
543+
```elixir
544+
defmodule MyApp.Accounts.TenantAdmin do
545+
def custom_index_query(_conn, _schema, query) do
546+
{query, skip_tenant_id: true}
547+
end
548+
549+
def custom_show_query(_conn, _schema, query) do
550+
{query, skip_tenant_id: true}
551+
end
552+
end
553+
```
541554

542555
### Extensions
543556

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)