Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/guypeer8/pidgeot
🐦Promise based mongoose configurable server-side paginator.
https://github.com/guypeer8/pidgeot
mongoose pagination paginator promise server-side-rendering
Last synced: about 1 month ago
JSON representation
🐦Promise based mongoose configurable server-side paginator.
- Host: GitHub
- URL: https://github.com/guypeer8/pidgeot
- Owner: guypeer8
- Created: 2017-01-16T07:44:42.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-05T18:24:07.000Z (almost 8 years ago)
- Last Synced: 2024-11-16T01:12:46.278Z (about 1 month ago)
- Topics: mongoose, pagination, paginator, promise, server-side-rendering
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/pidgeot
- Size: 6.84 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Promise based express-mongoose configurable server-side paginator
---
```js
/**
* 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);
```---
```js
/**
* 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:
```js
{
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](https://mozilla.github.io/nunjucks/) template:```js
/**
* 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);
``````html
{% if records.length > 0 %}
#
Name
Artist
Duration
{% for record in records %}
{{ loop.index }}.
{{ record.name }}
{{ record.artist }}
{{ record.duration }}
{% endfor %}
{% endif %}
{% if num_pages > 1 %}
{% endif %}
```
---
###Install on npm:
```bash
npm install pidgeot --save
```
[View on npm](https://www.npmjs.com/package/pidgeot)
---