GithubHelp home page GithubHelp logo

Comments (17)

superkhau avatar superkhau commented on September 26, 2024

@crandmck ^

from loopback-filters.

crandmck avatar crandmck commented on September 26, 2024

@superkhau Did this change when loopback-filters was added? It was almost certainly originally the way it's documented.

Does the Node API only accept the array form and not the {propertyName: <true|false>, propertyName: <true|false>, ... } form?

So, IIUC, this example should be:
{ fields: [{id: true}, {make: true}, {model: true}] }
Is that right?

from loopback-filters.

superkhau avatar superkhau commented on September 26, 2024

I believe it takes both, gotta verify though:

function selectFields(fields) {
.

@githistory Can you provide a sample repo for me to verify with?

from loopback-filters.

FraserChapman avatar FraserChapman commented on September 26, 2024

@crandmck @superkhau

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.

from loopback-filters.

crandmck avatar crandmck commented on September 26, 2024

@FraserChapman Could you please provide a sample repo, so we can reproduce? I know that's what @superkhau will ask! :-)

from loopback-filters.

ewrayjohnson avatar ewrayjohnson commented on September 26, 2024

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:

 // field selection
    if (filter.fields) {
       var fieldMap = filter.fields;
       var fieldSet = [];
       for(var key in fieldMap) {
        if (fieldMap.hasOwnProperty(key)) {
          if (fieldMap[key]) {
            fieldSet.push(key);
          }
        }
      }

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
It states: "Note: We plan to convert all modules to use loopback-filter[s], so it will become LoopBack’s common “built-in” filtering mechanism."
However that all such efforts seems to have been abandoned as the code (Copyright IBM) has not been updated since Dec 9, 2016 and this was reported on way back in Jan 25, 2016, with no one taking it seriously.

from loopback-filters.

crandmck avatar crandmck commented on September 26, 2024

@Amir-61
@superkhau
@loay

from loopback-filters.

crandmck avatar crandmck commented on September 26, 2024

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?

from loopback-filters.

ewrayjohnson avatar ewrayjohnson commented on September 26, 2024

from loopback-filters.

crandmck avatar crandmck commented on September 26, 2024

You said:

The documentation says the pattern is { fields: {id: true, make: true, model: true} } but only something like { fields : [ 'id', 'make', 'model' ] } will ever work.

So I changed fields filter accordingly to have (in the example) { fields: ["id", "make", "model"] }.

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.

from loopback-filters.

ewrayjohnson avatar ewrayjohnson commented on September 26, 2024

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:
The real LoopBack documentation says the pattern is { fields: {id: true, make: true, model: true} } but for loopback-filters, only something like { fields : [ 'id', 'make', 'model' ] } will ever work.

from loopback-filters.

ewrayjohnson avatar ewrayjohnson commented on September 26, 2024

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:

/**
 * Normalize fields to an array of included properties
 * @param {String|String[]|Object} fields Fields filter
 * @param {String[]} properties Property names
 * @param {Boolean} excludeUnknown To exclude fields that are unknown properties
 * @returns {String[]} An array of included property names
 */
function fieldsToArray(fields, properties, excludeUnknown) {
  if (!fields) return;

  // include all properties by default
  var result = properties;
  var i, n;

  if (typeof fields === 'string') {
    result = [fields];
  } else if (Array.isArray(fields) && fields.length > 0) {
    // No empty array, including all the fields
    result = fields;
  } else if ('object' === typeof fields) {
    // { field1: boolean, field2: boolean ... }
    var included = [];
    var excluded = [];
    var keys = Object.keys(fields);
    if (!keys.length) return;

    for (i = 0, n = keys.length; i < n; i++) {
      var k = keys[i];
      if (fields[k]) {
        included.push(k);
      } else if ((k in fields) && !fields[k]) {
        excluded.push(k);
      }
    }
    if (included.length > 0) {
      result = included;
    } else if (excluded.length > 0) {
      for (i = 0, n = excluded.length; i < n; i++) {
        var index = result.indexOf(excluded[i]);
        result.splice(index, 1);
      }
    }
  }

  var fieldArray = [];
  if (excludeUnknown) {
    for (i = 0, n = result.length; i < n; i++) {
      if (properties.indexOf(result[i]) !== -1) {
        fieldArray.push(result[i]);
      }
    }
  } else {
    fieldArray = result;
  }
  return fieldArray;
}

from loopback-filters.

crandmck avatar crandmck commented on September 26, 2024

Oh! Thanks for the clarification @ewrayjohnson. I certainly was confused :-)
I'll back out my change to the LB docs.

from loopback-filters.

bestvow avatar bestvow commented on September 26, 2024

image
image
image
Do not add everything if some fields you don't want to see even a flase ?

from loopback-filters.

bajtos avatar bajtos commented on September 26, 2024

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.)

from loopback-filters.

stale avatar stale commented on September 26, 2024

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.

from loopback-filters.

stale avatar stale commented on September 26, 2024

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 CODEOWNERS file at the top-level of this repository.

from loopback-filters.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.