loopback-custom-counts-mixin

1.0.2 • Public • Published

loopback-custom-counts-mixin

A mixin to enable get count of related models for a loopback Model.

fork from exromany/loopback-counts-mixin to accommodate b3rew/loopback-row-count-mixin

INSTALL

npm install --save loopback-custom-counts-mixin

There are 2 ways to enable mixin:

1) server.js

In your server/server.js file add the following line before the boot(app, __dirname); line.

...
var app = module.exports = loopback();
...
// Add Counts Mixin to loopback
require('loopback-custom-counts-mixin')(app);
 
boot(app, __dirname, function(err) {
  'use strict';
  if (err) throw err;
 
  // start the server if `$ node server.js`
  if (require.main === module)
    app.start();
});

2) mixin sources

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",
      "loopback/server/mixins",
      "../node_modules/loopback-custom-counts-mixin",
      "../common/mixins",
      "./mixins"
    ]
  }
}

CONFIG

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

{
  "name": "game",
  "properties": {
    "title": "string"
  },
  "relations": {
    "players": {
      "type": "hasMany",
      "model": "player"
    }
  },
  "mixins": {
    "Counts": true
  }
}

USAGE

In fetching data with counts filter, will added field named like relationnameCount

EXAMPLE

http://0.0.0.0:3000/api/games?filter={"counts":"players"}

will return list of games with field playersCount

[
  {
    "id": 1,
    "title": "First game",
    "playersCount": 1
  },
  {
    "id": 2,
    "title": "Last game",
    "playersCount": 42
  }
]

LICENSE

MIT

Package Sidebar

Install

npm i loopback-custom-counts-mixin

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

6.03 kB

Total Files

7

Last publish

Collaborators

  • b3rew