sails-hook-blueprint-aggregate

0.9.0 • Public • Published

sails-hook-blueprint-aggregate

Adds blueprint api method to aggregate records in database.

This is useful for example in pagination when you need to calculate number of pages.

Installation

In Sails.js v0.11+ installed hooks are run automatically. Therefore, simply install the hook via npm:

npm install sails-hook-blueprint-aggregate

Usage

Aggregation

GET /:model/aggregate?where={:CRITERIA}&groupBy=:Attribute&sum=:Attribute&average=:Attribute&min=:Attribute&max=:Attribute&sort=:Attribute

Examples

Query Response
Model/aggregate?groupBy=valid [ { "valid": false }, { "valid": true } ]
Model/aggregate?groupBy=valid&sum=test [ { "valid": false, "test": 720 }, { "valid": true, "test": 199 } ]
Model/aggregate?groupBy=valid&average=test { "valid": false, "test": 45 }, { "valid": true, "test": 66.3333333333333 }
Model/aggregate?min=test [ { "test": 2 } ]

Count

GET /:model/count?where={:CRITERIA}&groupBy=:Attribute

Examples

Query Response
Model/count [ { "count": 4 } ]
Model/aggregate?groupBy=valid [ { "false": 3, "true": 1 } ]
Model/count?groupBy=["valid","test"] [ { "false,24": 2, "true,31": 1, "false,87": 1 } ]

ToDo

  • [ ] Make :model/count use Model.count, currently this is not possible as Model.count is prone to SQL-injections

Acknowledgement

This module is based on:

thank you!

Package Sidebar

Install

npm i sails-hook-blueprint-aggregate

Weekly Downloads

0

Version

0.9.0

License

MIT

Last publish

Collaborators

  • enteee