Skip to content

Latest commit

 

History

History
122 lines (106 loc) · 3.02 KB

File metadata and controls

122 lines (106 loc) · 3.02 KB

loopback-visible-properties-mixin

npm version Build Status Coverage Status

Loopback mixin hidden all models properties and allow setup what should be visibles.

Installation

npm install loopback-visible-properties-mixin --save

Usage

Add the mixins property to your server/model-config.json:

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "../node_modules/loopback-visible-properties-mixin",
      "../common/mixins"
    ]
  }
}

Add mixin params in in model definition. Example:

{
  "name": "Person",
  "properties": {
    "firstName": "string",
    "lastName": "string",
    "email": "string",
  },
  "relaions": {
    "siblings": {
      "type": "referencesMany",
      "model": "Person",
      "foreignKey": "siblingsIds"
    },
    "mother": {
      "type": "belongsTo",
      "model": "Person",
      "foreignKey": ""
    },
    "father": {
      "type": "belongsTo",
      "model": "Person",
      "foreignKey": ""
    },
    "couple": {
      "type": "belongsTo",
      "model": "Person",
      "foreignKey": ""
    },
    "children": {
      "type": "hasMany",
      "model": "Person",
      foreignKey: "motherId"
    },
  },
  "mixins": {
    "VisibleProperties": {
      "fields": {
        "firstName":   true,
        "lastName":    true,
        "email":       false,
        "mother":      true,
        "motherId":    false,
        "father":      true,
        "fatherId":    false,
        "siblings":    true,
        "siblingsIds": false,
        "children":    true
      }
    }
  }
}

You can setup this mixin with a array of visible properties too. Example:

{
  "name": "Person",
  ...
  "mixins": {
    "VisibleProperties": {
      "fields": [
        "firstName",
        "lastName",
        "mother",
        "father",
        "siblings",
        "children",
      ]
    }
  }
}

In the above definitions, the attributes id, couple and coupleId will not be visible in remote responses because they were ignored and email, motherId, fatherId and siblingsIds will not be visible because they were set false.

Troubles

If you have any kind of trouble with it, just let me now by raising an issue on the GitHub issue tracker here:

https://github.com/arondn2/loopback-visible-properties-mixin/issues

Also, you can report the orthographic errors in the READMEs files or comments. Sorry for that, I do not speak English.

Tests

npm test or npm run cover