loopback-set-through-properties-mixin

1.0.2 • Public • Published

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.20latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i loopback-set-through-properties-mixin

Weekly Downloads

0

Version

1.0.2

License

MIT

Last publish

Collaborators

  • jonnybgod