JSPM

loopback-include-through-mixin

1.1.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 11
  • Score
    100M100P100Q64935F
  • License MIT

A mixin to enable including Through model properties

Package Exports

  • loopback-include-through-mixin

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (loopback-include-through-mixin) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

loopback-include-through-mixin

NPM version NPM downloads devDependency Status Build Status

MIT license Gitter Chat

Features

  • include though model properties with queries
  • setup default bahavior
  • use as mixin

Installation

npm install loopback-include-through-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-include-through-mixin",
      "../common/mixins"
    ]
  }
}

or

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback-include-through-mixin/lib",
      "../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": {
    "IncludeThrough": true,
  }
}

Then use in you queries like:

{
  "where": "...",
  "include": "...",
  "includeThrough": true
}
{
  "where": "...",
  "include": "...",
  "includeThrough": {
    "fields": "type"
  }
}

You can also set default behavior in your model definition with options.

{
  "name": "app",
  "properties": {
    "name": {
      "type": "string",
    }
  },
  "relations": {
    "users": {
      "type": "hasMany",
      "model": "user",
      "foreignKey": "appId",
      "through": "userRole"
    }
  },
  "mixins": {
    "IncludeThrough": {
      "relations": [
        "users"
      ],
      "fields": {
        "users": "type"
      }
    },
  }
}

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"
    }
  }
}

Options

option type description required
relations [String, Object] select relations, can be object with property "name" and "asProperty" false
relations[0].name String name of the relation false
relations[0].asProperty String rename the property that through model will be injected to, default is name of the through model false
fields Key/Value Object similar to filter fields. Key: relation; Value: fields filter. false
  • By setting relations in model definition it will return the though model for the specified relations by default
  • By passing includeThrough in you query filter it will override default fields

License

MIT