loopback-filter-by-relations-mixin

1.0.2 • Public • Published

loopback-filter-by-relations-mixin

NPM version NPM downloads Build Status Coverage Status

Features

  • filter items by properties in related models
  • supports 'hasMany', 'hasManyThrough', 'belongsTo' relations
  • works with nested relations
  • use as mixin

Installation

npm install loopback-filter-by-relations-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",
      "../common/mixins",
      "../node_modules/loopback-filter-by-relations-mixin"
    ]
  }
}

To use with your Models add the mixins attribute to the definition object of your model config.

 
{
  "name": "Customer",
  "mixins": {
    "FilterByRelations": true
  },
  "relations": {
    "orders": {
      "type": "hasMany",
      "model": "Order",
      "foreignKey": "customerId"
    }
  },
  "properties": {
    "id": "Number",
    "name": "String",
    "age": "Number"
  }
} 
{
  "name": "Order",
  "mixins": {
    "FilterByRelations": true
  },
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    }
  },
  "properties": {
    "id": "Number",
    "name": "String",
    "price": "Number",
    "customerId": "Number"
  }
}

Then use in you queries like:

  {
    "where": {
      "<RelationName>": "<WhereCondition>"
    }
  }
Customer.find({
  where: {
    orders: {
      price: { gt: 1000 }
    }
  }
}
Order.find({
  where: {
    customer: {
      age: { gte: 18 }
    }
  }
});

License

MIT

Package Sidebar

Install

npm i loopback-filter-by-relations-mixin

Weekly Downloads

26

Version

1.0.2

License

MIT

Unpacked Size

7.28 kB

Total Files

7

Last publish

Collaborators

  • yuranevmer