loopback-set-through-properties-mixin

A mixin to enable including Through model properties

Usage no npm install needed!

<script type="module">
  import loopbackSetThroughPropertiesMixin from 'https://cdn.skypack.dev/loopback-set-through-properties-mixin';
</script>

README

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