Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enables users to add custom query filters #37

Closed
wants to merge 1 commit into from

Conversation

subaru9
Copy link

@subaru9 subaru9 commented Sep 26, 2023

Overview

Added the EctoShorts.QueryBuilder.Custom behavior. This module enables the addition of custom query filters to the query builder through a custom module defined in the configuration of the dependent application.

Example

An example of how custom filters can be implemented using the EctoShorts.QueryBuilder.Custom behavior.

This module offers the following features:

  • Defines custom filters such as :select_balance_field_only, :currency_code, and :amount_gte.
  • Implements the create_custom_schema_filter/2 function to build queries based on custom filter types.
  
defmodule MyApp.EctoShorts.QueryBuilder.Custom do
  import Ecto.Query, only: [select: 3, where: 3]

  alias EctoShorts.QueryBuilder.Custom

  @behaviour Custom

  @custom_filters [:select_balance_field_only, :currency_code, :amount_gte]

  @impl Custom
  def custom_filters, do: @custom_filters

  @impl Custom
  def create_custom_schema_filter({:select_balance_field_only, true}, query),
    do: select(query, [b], b.balance)
  def create_custom_schema_filter({:select_balance_field_only, _}, query), do: query

  @impl Custom
  def create_custom_schema_filter({:currency_code, val}, query) do
    where(query, [b], fragment("(?).currency_code = ?", b.balance, ^to_string(val)))
  end

  @impl Custom
  def create_custom_schema_filter({:amount_gte, val}, query) do
    where(query, [b], fragment("(?).amount >= ?", b.balance, ^val))
  end
end

@subaru9 subaru9 marked this pull request as draft September 26, 2023 19:25
@subaru9
Copy link
Author

subaru9 commented Sep 7, 2024

Closed as there is a PR which allows custom filters in schema

@subaru9 subaru9 closed this Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant