FatEcto.Query.Dynamics.Buildable behaviour (FatEcto v1.2.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: 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.HospitalDynamicsBuilder do
  use FatEcto.Query.Dynamics.Buildable,
    filterable: [
      id: ["$EQUAL", "$NOT_EQUAL"]
    ],
    overrideable: ["name", "phone"],
    ignoreable: [
      name: ["%%", "", [], nil],
      phone: ["%%", "", [], nil]
    ]

  import Ecto.Query

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

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

  def override_buildable(_field, _operator, _value) do
    nil
  end

  # Optional: Override after_buildable to perform custom processing on the final dynamics
  def after_buildable(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_buildable(dynamics)

@callback after_buildable(dynamics :: Ecto.Query.dynamic_expr() | nil) :: any()

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_buildable(field, operator, value)

@callback override_buildable(
  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 nil, but it can be overridden by the using module.