A mixin to enable get count of related models for a loopback Model.
npm install --save loopback-counts-mixin
There are 2 ways to enable mixin:
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-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();
});
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-counts-mixin",
"../common/mixins",
"./mixins"
]
}
}
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
}
}
In fetching data with counts
filter, will added field named like relationnameCount
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
}
]
MIT