GithubHelp home page GithubHelp logo

nvdnkpr / loopback-counts-mixin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from exromany/loopback-counts-mixin

0.0 2.0 0.0 136 KB

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

License: MIT License

JavaScript 100.00%

loopback-counts-mixin's Introduction

loopback-counts-mixin

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

INSTALL

npm install --save loopback-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-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-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

loopback-counts-mixin's People

Contributors

exromany avatar

Watchers

Navid Nikpour 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.