FatEcto.Dynamics.FatBuildable behaviour (FatEcto v1.1.0)

View Source

Builds queries after filtering fields based on user-provided filterable and overrideable fields.

This module provides functionality to filter Ecto queries using predefined filterable fields (handled by Builder) and overrideable fields (handled by a fallback function).

Options

  • filterable_fields: A map of fields and their allowed operators. Example: %{
    "id" => ["$EQUAL", "$NOT_EQUAL"],
    "name" => ["$ILIKE"]
    }
  • overrideable_fields: A list of fields that can be overridden. Example: ["name", "phone"]
  • ignoreable_fields_values: A map of fields and their ignoreable values. Example: %{
    "name" => ["%%", "", [], nil],
    "phone" => ["%%", "", [], nil]
    }

Example Usage

defmodule FatEcto.Dynamics.MyApp.HospitalFilter do
  use FatEcto.Dynamics.FatBuildable,
    filterable_fields: %{
      "id" => ["$EQUAL", "$NOT_EQUAL"]
    },
    overrideable_fields: ["name", "phone"],
    ignoreable_fields_values: %{
      "name" => ["%%", "", [], nil],
      "phone" => ["%%", "", [], nil]
    }

  import Ecto.Query

  def override_whereable(_dynamics, "name", "$ILIKE", value) do
    dynamic([q], ilike(fragment("(?)::TEXT", q.name), ^value))
  end

  def override_whereable(_dynamics, "phone", "$ILIKE", value) do
    dynamic([q], ilike(fragment("(?)::TEXT", q.phone), ^value))
  end

  def override_whereable(dynamics, _, _, _) do
    dynamics
  end

  # Optional: Override after_whereable to perform custom processing on the final dynamics
  def after_whereable(dynamics) do
    IO.puts("Do something on final Dynamics")
    dynamics
  end
end

Summary

Callbacks

Callback for performing custom processing on the final dynamics.

Callback for handling custom filtering logic for overrideable fields.

Callbacks

after_whereable(dynamics)

@callback after_whereable(dynamics :: Ecto.Query.dynamic_expr()) ::
  Ecto.Query.dynamic_expr()

Callback for performing custom processing on the final dynamics.

This function is called at the end of the build/2 function. The default behavior is to return the dynamics, but it can be overridden by the using module.

override_whereable(dynamics, field, operator, value)

@callback override_whereable(
  dynamics :: Ecto.Query.dynamic_expr(),
  field :: String.t() | atom(),
  operator :: String.t(),
  value :: any()
) :: Ecto.Query.dynamic_expr()

Callback for handling custom filtering logic for overrideable fields.

This function acts as a fallback for overrideable fields. The default behavior is to return the dynamics, but it can be overridden by the using module.