npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bookshelf-page

v0.3.2

Published

Simple pagination for Bookshelf.js

Downloads

3,124

Readme

bookshelf-page v0.3.2

Important

Please note that this plugin will (hopefully) soon move into bookshelf proper, but there are still a few minor issues left to work out. Please track the progress of this plugin at:

https://github.com/tgriesser/bookshelf/issues/435 (discussion)

https://github.com/tgriesser/bookshelf/pull/1183 (pull request)

From v0.3.0, the API should not differ between this package and the final plugin once it moves into bookshelf proper. If you want to use this plugin now, just install it and then see the section on Upgrading below.

Version (npm)

Simple pagination for Bookshelf.js.

Easy to use, works with complex, nested, related queries, and sorts results - all included.

Installation

First, install bookshelf-page.

npm install -s bookshelf-page

Then, add the plugin to your bookshelf instance:

import config from './knexfile';
import knex from 'knex';
import bookshelf from 'bookshelf';
const ORM = bookshelf(knex(config));

// Add this line wherever you create your bookshelf instance
ORM.plugin('bookshelf-page');

export default ORM;

Upgrading

Once this plugin is accepted into bookshelf, you just need to change the line above to:

ORM.plugin('pagination')

and then you can remove bookshelf-page from your project.

Use

The plugin attaches two instance methods to the bookshelf Model object: orderBy and fetchPage.

Model#orderBy calls the underlying query builder's orderBy method, and is useful for ordering the paginated results.

Model#fetchPage works like Model#fetchAll, but returns a single page of results instead of all results, as well as the pagination metadata.

Model#orderBy

Specifies the column to sort on and sort order.

The order parameter is optional, and defaults to 'ASC'. You may also specify 'DESC' order by prepending a hyphen to the sort column name. orderBy("date", 'DESC') is the same as orderBy("-date").

Unless specified using dot notation (i.e., "table.column"), the default table will be the table name of the model orderBy was called on.

Example

    Car
    .forge()
    .orderBy('color', 'ASC').fetchAll()
    .then(function (rows) { // ...

Parameters

  • sort {string} Column to sort on. Required.
  • order {string} Ascending ('ASC') or descending ('DESC') order. Optional.

Model#fetchPage

Any options that may be passed to Model#fetchAll (such as withRelated) may also be passed in the options to fetchPage, as you can see in the example below.

To perform pagination, you must pass an options object with either page/pageSize or limit/offset keys. The following two calls are equivalent:

fetchPage({page: 10, pageSize: 20});
// OR
fetchPage({limit: 20, offset: 180});

By default, with no parameters or missing parameters, fetchPage will use an options object of {page: 1, pageSize: 10}

In the resulting pagination metadata, you will receive back the parameters used for pagination, i.e., either page/pageSize or offset/limit, respectively.

Below is an example showing the user of a JOIN query with sort/ordering, pagination, and related models.

Example

Calling fetchPage
Car
.query(function (qb) {
 qb.innerJoin('manufacturers', 'cars.manufacturer_id', 'manufacturers.id');
 qb.groupBy('cars.id');
 qb.where('manufacturers.country', '=', 'Sweden');
})
.orderBy('-productionYear') // Same as .orderBy('cars.productionYear', 'DESC')
.fetchPage({
 pageSize: 15, // Defaults to 10 if not specified
 page: 3, // Defaults to 1 if not specified

 // OR
 // limit: 15,
 // offset: 30,

 withRelated: ['engine'] // Passed to Model#fetchAll
})
.then(function (results) {
 console.log(results); // Paginated results object with metadata example below
})
Reading the pagination metadata

The fetchPage method attaches a pagination property to the resolved Collection instance.

{
 models: [<Car>], // Regular bookshelf Collection
 // other standard Collection attributes
 ...
 pagination: {
     rowCount: 53, // Total number of rows found for the query before pagination
     pageCount: 4, // Total number of pages of results
     page: 3, // The requested page number
     pageSize: 15, // The requested number of rows per page

     // OR, if limit/offset pagination is used instead of page/pageSize:
     // offset: 30, // The requested offset
     // limit: 15 // The requested limit
 }
}