[DISCUSSION] Deep JSON key fields filter #351
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
A working hack of this feature to illuminate what needs to be done. I'm open to suggestions for things I should improve but wanted to put this here for anyone interested in the feature. This is not meant to be merged in its current state.
Discussion
Supporting filtering by json fields should be possible with the postgresql connector. That-is, a find that looks like so:
Should result in
A number of issues make this difficult, not least of all because strongloop/loopback-datasource-juggler pre-filters unrecognizable fields in the query. A workaround is to alter the
strict
setting on the model definition; see https://github.com/strongloop/loopback-connector-postgresql/blob/e58ffccc58232d50e01620aa54951f398754c317/test/postgresql.test.js#L692-L702 for full discussion.Once we get the fields passed the juggler, we can deal with the filters in the
buildColumnNames
override. The same code used in #304 to create the where clause can be used for the select clause, but unfortunately the postgresql output becomes no longer identifiable.To mitigate this, we can use an
as
on nested fields, and because PostgreSQL doesn't like.
for names, we can replace those with__
among other things.To capture this we need to override
fromRow
; and construct a deep dictionary using the key/value.Finally, the juggler tries to do our job once again--unfortunately this time, there doesn't seem to be a fix beyond patching upstream. It filters the fields again after they've been assembled, this removes the entire
address
since it doesn't appear infields
. Just adding it wouldn't be good else we'd get the entire deep object... I've patched it upstream here: u8sand/loopback-datasource-juggler@81ed9dfWith the patch applied, it works, except that we can't identify sub-field types and end up with a string instead of an integer.
I hope we can somehow mitigate some duplication happening in the
loopback-datasource-juggler
before this becomes a viable feature.I also need to add more tests, deal with the other filter formatting, and carefully review things to ensure we're not introducing any vulnerabilities (more field escaping may be in order).
Any suggestions welcome to make this feature more of a reality.
Related issues
Checklist