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

url-db-paging

v1.2.3

Published

A library to help creating paging results through URLs

Downloads

5

Readme

url-db-paging

Build Status npm version

A library to help creating paging results through URLs.

Features

  • Build a paging query with a search provider based in some URL. Which will use offset fields due performance reasons.
  • Build REST JSON output with link to the next and previous pages.

Search providers

Do you want another provider? Please open an issue or even better send us a pull request.

Usage

Options

  • query: request query string
  • defaultSortField: default primary sort field
  • idField: data's id field
  • root: base url
  • limit: limit results per page
  • queryBuilderType: query builder type (mongoose, solr or knex)
var express = require('express'),
    Paging = require('url-db-paging'),
    User = require('./models/user');

var app = express();

app.get('/', function(req, res, next){
  var userQuery = User.find();

  var limit = parseInt(query.limit, 10) || 20;

  var paging = new Paging({
    query: req.query,               // query string
    defaultSortField: '-created',   // default sort field
    defaultSortField: {column: '-created', json: 'createdAt'}, // define the column to sort and the JSON value of createdAt
    idField: '_id',                 // data's id field
    idField: {column: '_id', json: 'id'}, // define the id column in database and
    root: 'http://service/users',   // base url
    limit: limit,                   // limit per page
    queryBuilderType: 'mongoose'    // query builder type
  });

  //OR: When your JSON data it's different than the column/property in yours database you can define those changes like:
  var paging = new Paging({
    query: req.query,               // query string
    defaultSortField: {column: '-created', json: 'createdAt'} // define the column to sort and the JSON value of createdAt
    idField: {column: '_id', json: 'id'} // define the id column in database and
    root: 'http://service/users',   // base url
    limit: limit,                   // limit per page
    queryBuilderType: 'mongoose'    // query builder type
  });

  // add sort db query
  paging.addSortDbQuery(userQuery);

  userQuery
    .sort(paging.getSortQuery())    // sort using paging
    .limit(paging.getLimitQuery())  // limit using paging
    .exec()
    .then(result, next);

  function result(users) {
    var data = paging.buildPagingResult(users); // add paging into data result
    return res.status(200).send(data);
  }
});


module.exports = app;

Output Result (Example)

{
  list:[
    { ... },
    { ... },
    { ... }
  ],
  _links: {
    previous: {
      href: "http://service/users?limit=5&sort=-created&offset_date=2014-07-31T12%3A05%3A24.865Z&offset_id=53da3104d14bdb2500cc203d&dir=backward"
    },
    next: {
      href: "http://service/users?limit=5&sort=-created&offset_date=2014-07-31T12%3A05%3A24.854Z&offset_id=53da3104d14bdb2500cc2035&dir=forward"
    }
  }
}

Contributing

It is required to use editorconfig and please write and run specs before pushing any changes:

npm test