ucast icon indicating copy to clipboard operation
ucast copied to clipboard

Support conditions that use nested relations

Open ccatterina opened this issue 2 years ago • 3 comments

It seems that sql interpreter doesn't support conditions with nested relations. Indeed the condition o { operator: 'eq', value: 2, field: 'x.y.z' } is translated into WHERE 'x'.'y.z' = :1.

Are you planning to support this type of conditions?

ccatterina avatar Oct 13 '21 13:10 ccatterina

The functionality to support relations is implemented through 2 options:

  1. In case, there is a dot in a field name options.joinRelation is called to determine whether it's a relation.
  2. If it's a relation, then ucast calls options.foreignField callback to determine the name of the Ifrelated field in SQL
  3. If it's not a relation, ucast calls options.localField. By default, it just escapes a column but you can implement a logic that checks if the column is of JSON type then convert it to JSON expression (e.g., JSONB expression in Postgress)

I tested this functionality only for direct relations (and not for relations of relations - i.e., deeper relations). After that, I just haven't had to time look again at this functionality. As currently, this is not on my priority list. And very likely, I will look into it only in the next year

stalniy avatar Oct 14 '21 18:10 stalniy

Hi, sorry for the late reply. I took a look at these functions. It seems that Query class and ORM adapters need some adjustments to support deeper relations. If I have time, I'll try to raise a PR.

Thank you!

ccatterina avatar Oct 27 '21 09:10 ccatterina

I've made some changes to the typeorm adapter and the Query class in order to add this functionality. Unfortunately I have no experience with the other ORMs and I can't be sure that my changes don't affect the other adapters.

Here's my changes if someone need it as a starting point for a patch: https://github.com/ccatterina/ucast/commit/086d9fe3dd07a83ae20a9bafeb3aa6590bd8c63c

ccatterina avatar Nov 19 '21 15:11 ccatterina