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.

woylie

woylie

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

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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 record...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

We're in Beta

About us Mission Statement