spagyther

spagyther

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?

Most Liked

spagyther

spagyther

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.

egze

egze

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

woylie

woylie

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

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement