Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keys.filter() will not filter out an object property that equals an empty object. #19

Open
bjamba opened this issue May 7, 2019 · 4 comments

Comments

@bjamba
Copy link

bjamba commented May 7, 2019

I'm working with a codebase that is using Objection.js to create database models from a Postgres DB. The model in question has a property represented in the DB as a JSONB not null data type, so we anticipate that when we run a query returning a row where this column is an empty JSON object, we'll get something like model.jsonColumn = {}.

I noticed that when I try to either run a Keys.filter(model, ['!jsonColumn']) blacklist or do not include jsonColumn in my whitelist, the jsonColumn value still displays with the empty object. I've played around with manipulating the properties of our model, and I've noticed that the filtering does work when I wrap the JSON.stringify method around the column, i.e. model.jsonColumn = JSON.stringify(model.jsonColumn).

I'm wondering if it would make sense to update the Keys.filter to handle properties whose values are empty objects as a quality-of-life improvement. Would love to hear your thoughts, and thanks for this library!

@SylvainEstevez
Copy link
Collaborator

@bjamba Thanks for raising the question. I actually spent quite some time thinking on the best way to deal with empty objects when I wrote the filter utility.

What behavior would you see happening for the following example?

Key.filter({ a: {}, b: { foo: 'bar' } }, ['!a', '!b.foo'])

Would you see the result to be {} or { b: {} }?

@bjamba
Copy link
Author

bjamba commented May 7, 2019

I'm currently seeing the result as { a: {} }. I think if I were to think about what I would like to see in that instance, I'd think I'd like to see { b: {} }, since if we're nesting into properties.

I guess to put it another way, the way I imagine the filters could work is so that if you are explicitly filtering out a parent key, it should filter out anything that's a child of it. But if I'm filtering a child key that would effectively render the parent key to be an empty object, we preserve the object.

@bjamba
Copy link
Author

bjamba commented May 13, 2019

Hi, circling back on this to ask if there had been any additional thoughts on solving this issue or if there was more clarification on my end that was needed. Thanks!

@SylvainEstevez
Copy link
Collaborator

@bjamba Sorry for not getting back to you earlier. What you are saying makes total sense and I'll provide a fix within 10 days. If you wish to make a PR yourself, that's even better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants