Skip to content

Commit

Permalink
Add query modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
alanvardy committed Jun 18, 2024
1 parent 60daaed commit 98d7784
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ Note that if you are navigating to the live table using [Phoenix LiveView live_s
- `pagination: [:top, :bottom]` Whether to show the pagination above and below
- `text: Exzeitable.Text.Default` The translation that appears on the table, defaults to English.
- `assigns: %{}` Passes additional assigns to socket.assigns. Keep your payload small!
- `query_modifier: {MyModule, :my_function}` Passes the query to MyModule.my_function/2, where query can then be dynamically altered before being returned. Arguments are the query, and the `Exzeitable.Params` struct, which is how Exzeitable stores state. Return value is the query.

```elixir
defmodule MyApp.MyModule do
def my_function(query, _state) do
# Make your modifications and return the new query
query
end
end
```

### Field options

Expand Down
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ config :exzeitable, Exzeitable.Mailer, adapter: Swoosh.Adapters.Test
config :stream_data, max_runs: 100

# Print only warnings and errors during test
config :logger, level: :warn
config :logger, level: :warning

# Initialize plugs at runtime for faster test compilation
config :phoenix, :plug_init_mode, :runtime
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

version: '3'
services:
dbserver:
image: postgres:14.2
Expand Down
21 changes: 20 additions & 1 deletion lib/exzeitable/database.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
defmodule Exzeitable.Database do
@moduledoc "Database interactions"
import Ecto.Query
alias Exzeitable.Params

@doc "Get the data using query"
@spec get_records(map) :: [map]
def get_records(%{query: query} = params) do
def get_records(%Params{query: query} = params) do
query
|> order_query(params)
|> search_query(params)
|> paginate_query(params)
|> modify_query(params)
|> get_query(params)
end

Expand Down Expand Up @@ -50,6 +52,23 @@ defmodule Exzeitable.Database do
from(q in query, select: count(q.id))
end

defp modify_query(query, %Params{query_modifier: nil}) do
query
end

defp modify_query(query, %Params{query_modifier: {mod, fun}} = params) when is_atom(mod) and is_atom(fun) do
apply(mod, fun, [query, params])
end

defp modify_query(_query, %Params{query_modifier: invalid}) do
raise """
Invalid option given for :query_modifier
Expected: {Module, :function}
Got: #{inspect(invalid)}
"""
end

# Repo.all
@spec get_query(Ecto.Query.t(), map) :: [map]
defp get_query(query, %{repo: repo}), do: repo.all(query)
Expand Down
1 change: 1 addition & 0 deletions lib/exzeitable/params.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule Exzeitable.Params do
:module,
:csrf_token,
# optional
:query_modifier,
:order,
:parent,
:belongs_to,
Expand Down
3 changes: 2 additions & 1 deletion test/exzeitable/database_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ defmodule Exzeitable.DatabaseTest do
alias Exzeitable.Database
alias TestWeb.User

@assigns %{
@assigns %Exzeitable.Params{
query: from(u in User),
parent: nil,
csrf_token: "234",
routes: TestWeb.Router.Helpers,
repo: TestWeb.Repo,
path: :user_path,
Expand Down

0 comments on commit 98d7784

Please sign in to comment.