How do I define new operators in Flop's custom_field?

I am building an API for querying spatio-temporal data and for filtering / sorting I chose to use Flop since it does most of what I need out of the box and most of what it doesn’t do out of the box, I can implement using custom_fields.

However, I don’t seem to get how to define new operators in custom_field’s. For example, I’d like to define Postgres’s range operators (<@, >@, &&, etc.) to use them in filters, but as soon as I try, it throws me an error and lists me the built-in operators.

For example, this doesn’t work:

@derive {
    Flop.Schema,
    filterable: [:id, :year_range],
    sortable: [:id],
    adapter_opts: [
      custom_fields: [
        year_range: [
          filter: {CustomFlopFilters, :year_range_filter, [source: :year]},
          ecto_type: Int4Range,
          operators: [:&&] # <-- Here is the problem
        ]
      ]
    ]
  }

Of course, I managed to get everything working by semantically abusing the built-in operators, e.g.:

@derive {
    Flop.Schema,
    filterable: [:id, :year_range],
    sortable: [:id],
    adapter_opts: [
      custom_fields: [
        year_range: [
          filter: {CustomFlopFilters, :year_range_filter, [source: :year]},
          ecto_type: Int4Range,
          operators: [:=~]
        ]
      ]
    ]
  }

But there comes a point where you need to define new ones, or maybe you prefer to use a notation that makes sense to you.

Any suggestions on how to proceed?

1 Like

It’s currently not possible to define custom operators. You’ll have to use one of the existing ones, as you did.

1 Like

Thank you for the answer (and for the great library too), I was afraid of that.

Are there plans of making it possible or I would be better off with forking Flop and implementing the ops myself?

I don’t have plans working on that myself at the moment, but I’m open to a proposal/PR.

1 Like

Great, I’ll first implement a couple new ops and if everything works out fine, I’ll try to figure out how to generalize the process.

2 Likes

Ping me if you want to join forces. I would also love to have custom operators.

2 Likes