# loopback-include-through-mixin

> A mixin to enable including Through model properties

Latest version **1.1.2** (published 2019-05-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install loopback-include-through-mixin
pnpm add loopback-include-through-mixin
yarn add loopback-include-through-mixin
bun add loopback-include-through-mixin
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2019-05-29 |
| First published | 2016-11-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | João Ribeiro |
| Maintainers | jonnybgod |
| Keywords | loopback, include, through, mixin |

## Links

- npm: https://www.npmjs.com/package/loopback-include-through-mixin
- Repository: https://github.com/JonnyBGod/loopback-include-through-mixin
- Homepage: https://github.com/JonnyBGod/loopback-include-through-mixin#readme
- Issues: https://github.com/JonnyBGod/loopback-include-through-mixin/issues
- npm.io page: https://npm.io/package/loopback-include-through-mixin

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2019-05-29
- 1.1.1 — 2018-04-15
- 1.1.0 — 2017-11-25
- 1.0.5 — 2017-06-16
- 1.0.4 — 2017-06-08
- 1.0.3 — 2017-04-25
- 1.0.2 — 2016-12-11
- 1.0.1 — 2016-12-08
- 1.0.0 — 2016-11-30

## README

# loopback-include-through-mixin

[![NPM version][npm-image]][npm-url] [![NPM downloads][npm-downloads-image]][npm-downloads-url]
[![devDependency Status](https://david-dm.org/JonnyBGod/loopback-include-through-mixin/dev-status.svg)](https://david-dm.org/JonnyBGod/loopback-include-through-mixin#info=devDependencies)
[![Build Status](https://img.shields.io/travis/JonnyBGod/loopback-include-through-mixin/master.svg?style=flat)](https://travis-ci.org/JonnyBGod/loopback-include-through-mixin)

[![MIT license][license-image]][license-url]
[![Gitter Chat](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/loopback-include-through-mixin/Lobby)

## Features

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

## Installation

```bash
npm install loopback-include-through-mixin --save
```

## How to use

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

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

or

```json
{
  "_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.

```json
{
  "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:

```json
{
  "where": "...",
  "include": "...",
  "includeThrough": true
}
```

```json
{
  "where": "...",
  "include": "...",
  "includeThrough": {
    "fields": "type"
  }
}
```

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

```json
{
  "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:

```json
{
  "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](LICENSE)

[npm-image]: https://img.shields.io/npm/v/loopback-include-through-mixin.svg
[npm-url]: https://npmjs.org/package/loopback-include-through-mixin
[npm-downloads-image]: https://img.shields.io/npm/dm/loopback-include-through-mixin.svg
[npm-downloads-url]: https://npmjs.org/package/loopback-include-through-mixin
[bower-image]: https://img.shields.io/bower/v/loopback-include-through-mixin.svg
[bower-url]: http://bower.io/search/?q=loopback-include-through-mixin
[dep-status-image]: https://img.shields.io/david/angulartics/loopback-include-through-mixin.svg
[dep-status-url]: https://david-dm.org/angulartics/loopback-include-through-mixin
[license-image]: http://img.shields.io/badge/license-MIT-blue.svg
[license-url]: LICENSE
[slack-image]: https://loopback-include-through-mixin.herokuapp.com/badge.svg
[slack-url]: https://loopback-include-through-mixin.herokuapp.com

---
_Source: https://npm.io/package/loopback-include-through-mixin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
