A mixin to enable setting Through model properties
Switch branches/tags
Nothing to show
Clone or download
Latest commit 1c9f034 Apr 15, 2018

README.md

loopback-set-through-properties-mixin

NPM version NPM downloads devDependency Status Build Status

MIT license Gitter Chat

##Features

  • set though model properties with queries
  • use as mixin

##Installation

npm install loopback-set-through-properties-mixin --save

##How to use

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

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

To use with your Models add the mixins attribute to the definition object of your model config.

{
  "name": "app",
  "properties": {
    "name": {
      "type": "string",
    }
  },
  "relations": {
    "users": {
      "type": "hasMany",
      "model": "user",
      "foreignKey": "appId",
      "through": "userRole"
    }
  },
  "mixins": {
    "SetThroughProperties": true,
  }
}

Then use in you queries like:

request(server).post('/apps/1/users')
  .send({
    name: 'John',
    email: 'john@gmail.com',
    userRole: {
      type: 'collaborator'
    }
  })
  .expect(200);

Example of Through Model:

{
  "name": "userRole",
  "properties": {
    "type": {
      "type": "string",
      "required": true,
      "default": "owner",
      "description": "owner | administrator | collaborator"
    }
  },
  "validations": [],
  "relations": {
    "app": {
      "type": "belongsTo",
      "model": "app",
      "foreignKey": "appId"
    },
    "user": {
      "type": "belongsTo",
      "model": "user",
      "foreignKey": "userId"
    }
  }
}

License

MIT