'Hide nested properties in loopback model

I am creating a loopback model (model1) which refers to another model and want to hide some properties of the nested model (model2). Also, I just want them to be hidden in this model1. How can I solve this issue ?

I have tried the property hidden, which works fine with the current model1 properties but not with the nested model2 properties.

{
  "name": "Model1",
  "base": "PersistedModel",
  "idInjection": true,
 ...
  "properties": {
    "pet": {
      "type": "Model2",
    }
 ...
}

{
  "name": "Model2",
 ...
  "properties": {
    "name": {
      "type": "string",
    }
 ...
}

I want here to hide the property "name" from Model2 in the Model1 explorer

Thanks !



Solution 1:[1]

Did you check include-with-filter documentation. I hope this should solve your problem or else share some more descriptive code that you have tried so far.

Here's another example that uses the predefined model relations to include selective fields from the related models.

{"include": [  
  {"relation":"relationName", 
   "scope":{"fields": ["Field1","Field2"]} 
  },
  {"relation":"relation2Name", 
   "scope":{"fields": ["Field3","Field4"]} 
  },
]}

Note: you won't be able to hide Ids

Solution 2:[2]

I want here to hide the property "name" from Model2 in the Model1 explorer

In order to do that you just simply add this line to your model2.json:

"protected": ["name"],

You are looking for the protected property, the protected fields will not be present if the object is nested inside another.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Lorkis