Giter VIP home page Giter VIP logo

loopback-paginator's Introduction

Pagination mixin for LoopBack

Paginator adds an easy to use pagination to any of your models. See the example below.

Installation

$ npm i loopback-paginator --save

Config

Server Config

With [email protected] mixinSources have been implemented in a way which allows for loading this mixin without changes to the server.js file previously required. Just add "../node_modules/loopback-paginator" to the mixins property of your server/model-config.json.

{
  "_meta": {
    "mixins": [
      "loopback/common/mixins",
      "../node_modules/loopback-paginator/lib",
      "../common/mixins"
    ]
  }
}

Model Config

To use with your models just add Paginator: true to mixins in your model config and the default options will be used:

{
  "name": "Model",
  "properties": {
    "name": {
      "type": "string",
    }
  },
  "mixins": {
    "Paginator": true
  }

  ...

  // you can also overide the default values:
  "mixins": {
    "Paginator": {
      "limit": 5    // items per page, default: 10
    }
  }
}

Global Config

It is also possible to configure the mixin globally in your config.json. Just add paginator and use the same options as with the model above:

{
  "paginator": {
    "limit": 20    // items per page, default: 10
  }
}

Usage

When Paginator is added to a model, Model.find() will return an object with data and meta. data is an array with the queried items, limited to the number you defined in the mixin options (see Model Config). meta contains information about the requested page (see example below). You can specify the page as an URL parameter (e.g. ?page=3). If no page is specified it defaults to 1.

/GET https://example.com/api/items?page=3

{
  "data": [
    {
      "title": "Item 1",
      "description": "Cool first item.",
      "id": "c5075168-abe0-41c6-8052-e07745eade48"
    },
    {
      "title": "Item 2",
      "description": "Cool second item.",
      "id": "0cad55df-c59d-4195-bb4f-7a252bd4bbe8",
    }

    ...

  ],
  "meta": {
    "totalItemCount": 95, // total number of items
    "totalPageCount": 10, // total number of all pages
    "itemsPerPage": 10,   // numberof items per page
    "currentPage": 3,     // the current page
    "nextPage": 4,        // the next page, only present if there is another page
    "previousPage": 2     // the previous page, only present if currentPage != 1
  }
}

ToDo

  • allow to override limit with an URL parameter

License

MIT

Changelog

v0.9.6

  • Add global config

loopback-paginator's People

Contributors

mahnuh avatar vgrados2 avatar

Watchers

René Ras avatar James Cloos 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.