Code Monkey home page Code Monkey logo

loopback-include-through-mixin's Introduction

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

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] select relations 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

loopback-include-through-mixin's People

Contributors

chaitanya-bhagavan avatar jonnybgod avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.