3
votes

I would like to filter from REST API by nested data. For example this object:

[
  {
    "name": "Handmade Soft Fish",
    "tags": "Rubber, Rubber, Salad",
    "categories": [
      {
        "name": "women",
        "id": 2,
        "parent_id": 0,
        "permalink": "/women"
      },
      {
        "name": "kids",
        "id": 3,
        "parent_id": 0,
        "permalink": "/kids"
      }
    ]
  },
  {
    "name": "Tasty Rubber Soap",
    "tags": "Granite, Granite, Chair",
    "categories": [
      {
        "name": "kids",
        "id": 3,
        "parent_id": 0,
        "permalink": "/kids"
      }
    ]
  }
]

is comming by GET /api/products?filter[include]=categories and i would like to get only products which has category name "women". How do this?

4

4 Answers

3
votes

LoopBack does not support filters based on related models.

This is a limitation that we have never had bandwidth to solve, unfortunately :(

For more details, see the discussion and linked issues here:

1
votes

Maybe you want to get this data by the Category REST API. For example:

GET /api/categories?filter[include]=products&filter[where][name]=woman

The result will be a category object with all products related. To this, will be necessary declare this relation on the models.

0
votes

Can you share how it looks like without filter[include]=categorie, please ?

[edit] after a few questions in comment, I'd build a remote method : in common/models/myModel.js (inside the function) :

function getItems(filter, categorieIds = []) {
    return new Promise((resolve, reject) => {
        let newInclude;
        if (filter.hasOwnProperty(include)){
            if (Array.isArray(filter.include)) {
                newInclude = [].concat(filter.include, "categories")
            }else{
                if (filter.include.length > 0) {
                    newInclude = [].concat(filter.include, "categories");
                }else{
                    newInclude = "categories";
                }
            }
        }else{
            newInclude = "categories";
        }

        myModel.find(Object.assign({}, filter, {include: newInclude}))
        .then(data => {
            if (data.length <= 0) return resolve(data);
            if (categoriesIds.length <= 0) return resolve(data);

            // there goes your specific filter on categories
            const tmp = data.filter(
                item => item.categories.findIndex(
                    categorie => categorieIds.indexOf(categorie.id) > -1
                ) > -1
            );
            return resolve(tmp);
        })
    }
}
myModel.remoteMethod('getItems', {
    accepts: [{
        arg: "filter",
        type: "object",
        required: true
    }, {
        arg: "categorieIds",
        type: "array",
        required: true
    }],
    returns: {arg: 'getItems', type: 'array'}
});

I hope it answers your question...

0
votes
Try like this.It has worked for me.

  const filter = {
          where: {
            'categories.name': {
              inq: ['women']**strong text**
            }
          }
        };

Pass this filter to request as path parameters and the request would be like bellow

GET /api/categoriesfilter=%7B%22where%22:%7B%categories.name%22:%7B%22inq%22:%5B%women%22%5D%7D%7D%7D