Custom filtering functonality with dynamic aliases

We are using Couchbase as our NoSQL database of choice. This service has
the advantage that it has a SQL-like querying language over JSON named n1ql.

We are doing queries through a SQL adapter with a few tweaks and it works nicely,
even though some of the functionality n1ql provides has to be put within fragments.
These queries are generated via the dynamic/2 macro based off of a filter object.

Now, the issue I am facing is implementing the ANY operation so that it is dynamic.
Because it requires an alias to be set, ideally I would want to generate one per each
of the ANY statements that are to be added to the query. The syntax goes as follows:

ANY <alias> IN <path> SATISFIES <alias>.name LIKE "%test%" END

Thus there are two issues here; creating the alias and then referencing it later from a
dynamic call responsible for defining the clauses that go inside the SATISFIES block.

One solution to this could be generating a random string as an alias, but there’d be
no way to inject it within the fragment directly after the ANY word and this would fail.
Another solution could perhaps be generating actual aliases that Ecto can then parse
and properly transform into a query, or maybe even adding a new macro that does this.

I am not sure how to go about this exactly, let me know your ideas! :slightly_smiling_face:

1 Like