-
Notifications
You must be signed in to change notification settings - Fork 22
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
Inconsistency between documentation and implementation on fields filter #3
Comments
@superkhau Did this change when Does the Node API only accept the array form and not the So, IIUC, this example should be: |
I believe it takes both, gotta verify though: Line 230 in 2f44e21
@githistory Can you provide a sample repo for me to verify with? |
FWIW I'm using [email protected] and the only thing that works for me is an inclusive array of property names. e.g. To use the example given. { fields: ["id", "make", "model"] } Anything other format supplied to fields results in empty objects being returned from the call to applyFilter. Puzzled me for about half and hour. |
@FraserChapman Could you please provide a sample repo, so we can reproduce? I know that's what @superkhau will ask! :-) |
No offense, but IMHO, a sample repo is unnecessary as any data will do. This is a obvious bug and very easy to fix. I wonder if this code is being maintained since this bug report is so old. The documentation says the pattern is { fields: {id: true, make: true, model: true} } but only something like { fields : [ 'id', 'make', 'model' ] } will ever work. The code should "normalize" the filter.fields object by converting it from a map of fields to include/exclude, to an array of strings of fields to include. I recommend the following fix. Replace the if (filter.fields) {... } on or about line 29 of loopback-filters/lib/index.js with this:
loopback-filters/test/filter.test.js should also be updated to provide a regression test of this capability. An ultimate fix would cache this fieldSet for reuse and would also use the declared properties (and relations) of a model and would consider the strict property - but I digress. See loopback=datasource-juggler\lib\utils.js\fieldsToArray(..) for a full implementation. On this page LoopBack 3.0 Querying data |
OK @ewrayjohnson I updated the docs. However, it's not clear to me how you exclude fields with the syntax you indicate. Do you know? |
The LoopBack documentation WAS correct and this is how LoopBack currently works. It looks like the documentation now does not match the released code.
"A fields filter specifies properties (fields) to include or exclude from the results."
true=include
false=exclude
By default, queries return all model properties in results. However, if you specify a fields filter, then the query will include those fields specified with a value of true and exclude those with false. Why then is the true/false necessary? Here's why: when the model is defined with strict=false, undefined fields can be stored (in a schema-less database ) and a query without a fields filter will return undefined fields. However, if a query has a fields filter, only those fields defined in the fields filter with a true value will be returned. The false therefore allows a client to not have to know if strict is set.
… On May 17, 2017, at 6:33 PM, Rand McKinney ***@***.***> wrote:
OK @ewrayjohnson I updated the docs. However, it's not clear to me how you exclude fields with the syntax you indicate. Do you know?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
You said:
So I changed fields filter accordingly to have (in the example) Now you're saying that it WAS correct but now is not. So which is correct? The doc should of course state what actually works, not what should work. |
Now I see how I confused you. THIS thread is for strongloop/loopback-filters NOT strongloop/loopback (the real LoopBack). The documentation I referred to was for LoopBack, which WAS correct for the real LoopBack. However in "loopback-filters" filter.fields does NOT work the same as in the real LoopBack. I provided the code fix for loopback-filters so that IT would work like the thousands of existing "real" LoopBack projects since the LoopBack documnation states "Note: We plan to convert all modules to use loopback-filter[s], so it will become LoopBack’s common “built-in” filtering mechanism." So changing the documentation in LoopBack will cause lots of people problems. Hope this helps. So what I mean to say in my ealier post is: |
Here is a snippet of code from the real LoopBack. Note the excludeUnknown param which is mapped to the "model.strict" property and the properties param, which is a list of the names of properties explicitly defined on the model:
|
Oh! Thanks for the clarification @ewrayjohnson. I certainly was confused :-) |
Hello @ewrayjohnson, sorry for joining this discussion late. You offered a code snippet to change the behavior of loopback-filters and make it consistent with "regular" loopback. Could you please turn it into a pull request that we can land? (Assuming this issue is still relevant.) |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the |
The fields filter in this library accepts array of field names where as the documentation says
The text was updated successfully, but these errors were encountered: