GithubHelp home page GithubHelp logo

fullcube / loopback-ds-paginate-mixin Goto Github PK

View Code? Open in Web Editor NEW
31.0 14.0 12.0 336 KB

A mixin to provide pagination for loopback Model properties

License: MIT License

JavaScript 100.00%
loopback loopback-mixin mit fullcube lb2

loopback-ds-paginate-mixin's Introduction

PAGINATION

Greenkeeper badge

Circle CI Coverage Status Dependencies semantic-release

This module is designed for the Strongloop Loopback framework. It provides a mixin that makes it easy to add paginate to an existing model

INSTALL

npm install --save loopback-ds-paginate-mixin

SERVER CONFIG

Add the mixins property to your server/model-config.json:

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "../node_modules/loopback-ds-paginate-mixin/lib",
      "../common/mixins"
    ]
  }
}

MODEL CONFIG

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

{
    "name": "Item",
    "properties": {
        "name": "String",
        "description": "String",
        "status": "String"
    },
    "mixins": {
        "Paginate": {
            "limit": "10"
        }
    }
}

USAGE

// The basic
var request = {
  skip: 0,
  limit: 15,
  where: {
    status: 'active'
  }
}

// Using a Promise
Item.paginate(request).then(function(result) {
  // The first 15 active items are in result
  console.log(result);
}).catch(function(err){
  // Handle errors here
  console.error(err);
});

// Using a callback
Item.paginate(request, function(err, result) {
  // Handle errors here if err !== null
  if(err) console.error(err);
  // The first 15 active items are in result
  console.log(result);
});

// You can override the limit on a per-request base
var options = {
  limit: 5
}
Item.paginate(request, options).then(function(result) {
  // The first 5 active items are in result
  console.log(result);
}).catch(function(err){
  // Handle errors here
  console.error(err);
});

TESTING

Run the tests in test.js

  npm test

Run with debugging output on:

  DEBUG='loopback-ds-paginate-mixin' npm test

loopback-ds-paginate-mixin's People

Contributors

beeman avatar greenkeeper[bot] avatar joe-at-startupmedia avatar josx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

loopback-ds-paginate-mixin's Issues

Unhandled rejection Error: Callback was already called.

Hi thanks for your mixin!
i'm receiving this exception:

Unhandled rejection Error: Callback was already called.
    at <path>/node_modules/async/dist/async.js:903:32
    at <path>/node_modules/async/dist/async.js:3858:13
    at interceptInvocationErrors (<path>/node_modules/strong-remoting/lib/remote-objects.js:713:22)
    at <path>/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25
    at <path>/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25
    at <path>/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25
    at <path>/node_modules/strong-remoting/lib/remote-objects.js:679:7
    at <path>/node_modules/strong-remoting/lib/http-context.js:305:7
    at <path>/node_modules/strong-remoting/lib/shared-method.js:277:11
    at tryCatcher (<path>/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (<path>/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (<path>/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._fulfillPromises (<path>/node_modules/bluebird/js/release/promise.js:668:14)
    at Promise._settlePromises (<path>/node_modules/bluebird/js/release/promise.js:694:18)
    at Async._drainQueue (<path>/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (<path>/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (<path>/node_modules/bluebird/js/release/async.js:17:14)

I have solved commenting this line

Thanks

Add page param

Can you please modify the code to accept a page param

query.page = query.page || 1;

var params = {
  skip: query.skip || ((query.page - 1) * query.limit),
  limit: query.limit
};

Error 500 on explorer

Just download and try it to use it!
I am using mongo and have and empty model, go to explorer and try it out.
Response error:

{
  "error": {
    "name": "TypeError",
    "status": 500,
    "message": "Cannot call method 'then' of undefined",
    "stack": "TypeError: Cannot call method 'then' of undefined\n    at Function.Paginate.Model.paginate (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-ds-paginate-mixin/index.js:120:23)\n    at SharedMethod.invoke (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:234:25)\n    at HttpContext.invoke (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback/node_modules/strong-remoting/lib/http-context.js:375:12)\n    at /home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:608:11\n    at execStack (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:448:7)\n    at /home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback/lib/application.js:326:13\n    at /home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback/lib/model.js:295:5\n    at /home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback/common/models/acl.js:437:23\n    at /home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/async/lib/async.js:254:17\n    at async.each (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/async/lib/async.js:121:20)"
  }
}

Also on server console have another error:

/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-connector-mongodb/node_modules/mongodb/lib/utils.js:97
    process.nextTick(function() { throw err; });
                                        ^
TypeError: object is not a function
    at allCb (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-datasource-juggler/lib/dao.js:910:7)
    at /home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-connector-mongodb/lib/mongodb.js:597:7
    at handleCallback (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-connector-mongodb/node_modules/mongodb/lib/utils.js:95:12)
    at /home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-connector-mongodb/node_modules/mongodb/lib/cursor.js:688:16
    at handleCallback (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-connector-mongodb/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:248:5)
    at setCursorNotified (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-connector-mongodb/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:457:3)
    at /home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-connector-mongodb/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:560:16
    at queryCallback (/home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback-connector-mongodb/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:224:5)
    at /home/josx/Desarrollo/crear/proyectos/DealsAndReviews/node_modules/loopback/node_modules/continuation-local-storage/context.js:74:17
    at b (domain.js:183:18)

Any clue to solve this?

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.