FatEcto.Query.Buildable behaviour (FatEcto v1.2.0)
View SourceBuilds Ecto queries after filtering fields based on user-provided filterable and overrideable fields.
This module extends FatDynamicsBuildable to work directly with Ecto queries, applying conditions to an existing query rather than just building dynamics.
Options
filterable
: A map of fields and their allowed operators. Example: [
]id: ["$EQUAL", "$NOT_EQUAL"], name: ["$ILIKE"]
overrideable
: A list of fields that can be overridden. Example: ["name", "phone"]ignoreable
: A map of fields and their ignoreable values. Example: [
]"name" => ["%%", "", [], nil], "phone" => ["%%", "", [], nil]
Example Usage
defmodule FatEcto.Query.HospitalQueryBuilder do
use FatEcto.Query.Buildable,
filterable: [
id: ["$EQUAL", "$NOT_EQUAL"]
],
overrideable: ["name", "phone"],
ignoreable: [
name: ["%%", "", [], nil],
phone: ["%%", "", [], nil]
]
import Ecto.Query
def override_buildable(query, "name", "$ILIKE", value) do
from(q in query, where: ilike(fragment("(?)::TEXT", q.name), ^value)
end
def override_buildable(query, "phone", "$ILIKE", value) do
from(q in query, where: ilike(fragment("(?)::TEXT", q.phone), ^value)
end
def override_buildable(query, _, _, _) do
query
end
# Optional: Override after_buildable to perform custom processing on the final query
def after_buildable(query) do
IO.puts("Do something on final query")
query
end
end
Summary
Callbacks
Callback for performing custom processing on the final query.
Callback for handling custom filtering logic for overrideable fields with query support.
Callbacks
@callback after_buildable(query :: Ecto.Query.t()) :: Ecto.Query.t()
Callback for performing custom processing on the final query.
This function is called at the end of the build/3
function. The default behavior is to return the query,
but it can be overridden by the using module.
@callback override_buildable( query :: Ecto.Query.t(), field :: String.t() | atom(), operator :: String.t(), value :: any() ) :: Ecto.Query.t()
Callback for handling custom filtering logic for overrideable fields with query support.
This function acts as a fallback for overrideable fields. The default behavior is to return the query, but it can be overridden by the using module.