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

pidgeot

v1.0.2

Published

Promise based mongoose configurable server-side paginator.

Downloads

8

Readme

Promise based express-mongoose configurable server-side paginator


/**
 * song_model.js
 **/
 
// create mongoose schema & model
const mongoose = require('mongoose');
const {Schema} = mongoose;

const songSchema = new Schema({
	name: String,
	artist: String,
	duration: Number
});

module.exports = mongoose.model('Song', songSchema);

/**
 * app.js
 */
 
const app = require('express')();
const Pidgeot = require('pidgeot');
const Song = require('./song_model');

app.get('/', (req, res) => {
    // initialize paginator instance
	const Paginator = Pidgeot({
		model: Song, 
		page: req.query.page, 
		query: {
			duration: {
				$gt: 200,
				$lte: 50
			}
		}, 
		limit: 20, 
		select: '-_id',
		sort: 'name', 
		fields: {
			collection: 'songs'
		}
	});

	// paginage
	Paginator.paginate()
	.then(data => {
		const {page, records, prev_page, next_page, num_records, num_pages} = data;
		console.log(`Page: ${page}`); // page given or 1
		console.log(`Songs: ${JSON.stringify(records)}`); // songs array
		console.log(`Prev: ${prev_page}`); // /?page={prev_page}&collection=songs
		console.log(`Next: ${next_page}`); // /?page={next_page}&collection=songs
		console.log(`Songs Count: ${num_records}`); // number of records returned 
		console.log(`Page Count: ${num_pages}`); // total number of pages
		// render template passing data
		res.render('songs_template.html', {
			page, 
			records, 
			prev_page, 
			next_page, 
			num_records, 
			num_pages
		});
	}).catch(err => {
		console.log(err.message);
		res.render('error_template.html', {message: 'Error.'});
	});
});

app.listen(8000);

Class attributes:

{
    model  :  // Mongoose model (required),
    path   :  // Route path (default '/'),
    page   :  // What page to paginate to (default 1),
    query  :  // DB query to filter records by (default {}),
    limit  :  // How many records should be shown on a page (default 50),
    select :  // Which model fields to select,
    sort   :  // How to sort the returned records (default null),
    lean   :  // Should records data return as lean array,
    fields :  // External query params to include (default {})
}

For fun, lets render a simple nunjucks template:

/**
 * app.js
 */
 
const app = require('express')();
const nunjucks = require('nunjucks');
const Pidgeot = require('pidgeot');
const Song = require('./song_model');

// config nunjucks
nunjucks.configure('views', {
	autoSpace: true,
	express: app,
	watch: true
});

// index route
app.get('/', (req, res) => {

    // initialize paginator instance
	const Paginator = Pidgeot({
		model: Song,
		page: req.query.page,
		query: {
			duration: {
				$gt: 200,
				$lte: 50
			}
		},
		limit: 20,
		select: '-_id',
		sort: 'name',
		fields: {
			collection: 'songs'
		}
	});

	// paginate (promise returned)
	Paginator.paginate()
        .then(data => {
            const {page, records, prev_page, next_page, num_records, num_pages} = data;
            // render template passing data
            res.render('songs_template.html', {
                page,
                records,
                prev_page,
                next_page,
                num_records,
                num_pages
            });
        }).catch(err => {
            console.log(err.message);
            res.render('error_template.html', {message: 'Error.'});
        });

});

app.listen(8000);
{% if records.length > 0 %}
<table class="table table-striped">
    <thead>
        <tr>
            <td>#</th>
            <td>Name</td>
            <td>Artist</td>
            <td>Duration</td>
        </tr>
    </thead>
    <tbody>
    {% for record in records %}
        <tr class="records">
            <td>{{ loop.index }}.</td>
            <td>{{ record.name }}</td>
            <td>{{ record.artist }}</td>
            <td>{{ record.duration }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
{% endif %}

<!-- pagination -->
{% if num_pages > 1 %}
<nav aria-label="Page navigation">
    <ul class="pager">
        {% if page > 1 %}
        <li class="previous">
            <a href="{{ prev_page }}"><span aria-hidden="true">&larr;</span> Prev</a>
        </li>
        {% endif %}
        {% if page < num_pages %}
        <li class="next">
            <a href="{{ next_page }}">Next <span aria-hidden="true">&rarr;</span></a>
        </li>
        {% endif %}
    </ul>
</nav>
{% endif %}

###Install on npm:

npm install pidgeot --save

View on npm