├── .gitignore ├── LICENSE ├── README.md ├── counts.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Dependency directory 6 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 7 | node_modules 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 exromany 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # loopback-row-count-mixin 2 | A mixin to get total count of a model for pagination in a loopback Model. 3 | 4 | ## INSTALL 5 | 6 | ``` 7 | npm install --save loopback-row-count-mixin 8 | ``` 9 | 10 | ###### you can enable mixin by editing `server.js`: 11 | 12 | In your server/server.js file add the following line before the boot(app, __dirname); line. 13 | 14 | ```js 15 | ... 16 | var app = module.exports = loopback(); 17 | ... 18 | // Add Counts Mixin to loopback 19 | require('loopback-row-count-mixin')(app); 20 | 21 | boot(app, __dirname, function(err) { 22 | 'use strict'; 23 | if (err) throw err; 24 | 25 | // start the server if `$ node server.js` 26 | if (require.main === module) 27 | app.start(); 28 | }); 29 | ``` 30 | 31 | 32 | ## CONFIG 33 | 34 | To use with your Models add the `mixins` attribute to the definition object of your model config. 35 | 36 | ```json 37 | { 38 | "name": "player", 39 | "properties": { 40 | "name": "string", 41 | "type": "string", 42 | }, 43 | "mixins": { 44 | "RowCount": true 45 | } 46 | } 47 | ``` 48 | 49 | ## USAGE 50 | 51 | ### EXAMPLE 52 | 53 | ``` 54 | http://0.0.0.0:3000/api/players 55 | ``` 56 | 57 | will return list of players with field to help for you pagination 58 | 59 | ```json 60 | { 61 | "count": 2, 62 | "rows": [ 63 | { 64 | "id": 1, 65 | "title": "First player", 66 | "type": "" 67 | }, 68 | { 69 | "id": 2, 70 | "title": "Second player", 71 | "type": "" 72 | } 73 | ] 74 | } 75 | 76 | ``` 77 | 78 | ## LICENSE 79 | 80 | MIT 81 | 82 | -------------------------------------------------------------------------------- /counts.js: -------------------------------------------------------------------------------- 1 | module.exports = function RowCount (Model) { 2 | 'use strict'; 3 | 4 | // Model.afterRemote('findById', injectCounts); 5 | // Model.afterRemote('findOne', injectCounts); 6 | Model.afterRemote('find', injectCounts); 7 | 8 | function injectCounts (ctx, unused, next) { 9 | var resources = ctx.result; 10 | if (!Array.isArray(resources)) resources = [resources]; 11 | if (!resources.length) { 12 | ctx.result = { 13 | count: 0, 14 | rows: [] 15 | }; 16 | return next(); 17 | } 18 | var args = ctx.args && ctx.args.filter && typeof ctx.args.filter == 'string'?JSON.parse(ctx.args.filter):{}; 19 | args = ctx.args && ctx.args.filter && typeof ctx.args.filter == 'object'?ctx.args.filter:args; 20 | 21 | var filter = args && args.where ? args.where : {}; 22 | totalCount(filter, function(err, count){ 23 | if(!err){ 24 | ctx.result = { 25 | count: count, 26 | rows: resources 27 | }; 28 | }else{ 29 | console.log(err); 30 | return next(err); 31 | } 32 | 33 | return next(); 34 | }) 35 | } 36 | 37 | function totalCount(filter, done){ 38 | Model.count(filter, function(err, count) { 39 | if(!err) return done(null, count); 40 | else return done("Unable to count: "+ err) 41 | }) 42 | } 43 | }; 44 | 45 | 46 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function mixin (app) { 2 | app.loopback.modelBuilder.mixins.define('RowCount', require('./counts')); 3 | }; 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "loopback-row-count-mixin", 3 | "version": "0.1.7", 4 | "description": "A mixin to get total count of a model for a loopback Model.", 5 | "main": "index.js", 6 | "scripts": {}, 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/b3rew/loopback-row-count-mixin.git" 10 | }, 11 | "keywords": [ 12 | "loopback", 13 | "strongloop", 14 | "mixin", 15 | "pagination", 16 | "count" 17 | ], 18 | "author": "b3rew ", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/b3rew/loopback-row-count-mixin/issues" 22 | }, 23 | "homepage": "https://github.com/b3rew/loopback-row-count-mixin#readme", 24 | "devDependencies": {} 25 | } 26 | --------------------------------------------------------------------------------