This package is use to paginate mongodb data.
First install Node.js and MongoDB. Then:
Use the package manager npm to install paginate-mongoose-data.
npm install mongoose --save
npm install paginate-mongoose-data --save
import { paginate } from 'paginate-mongoose-data';
import { Users } from '../models/user';
const limit: number; // limit number of data to fetch
const currentpage: number; // current page of data to fetch
const criteria: object = {field: value};
const populateField = [
{
path: "userId",
select: "userName"
},
{
path: "categoryId",
select: "name",
}
];
paginate(Users); // Model is required, second parameter of object is optional
// Model and query object
paginate(Users, {
limit,
currentpage,
criteria
}); // parameters => Model, Paginate object
// Model and query object
paginate(Users, {
limit,
currentpage,
criteria,
populateField
}); // parameters => Model, Paginate object with populated fields
// How to populate nested object
const populateField = [
{
path: "userId",
select: "userName"
},
{
path: "categoryId",
select: "name",
populate: {
path: 'userId'
}
}
];
paginate(Users, { populateField }); // parameters => Model, Paginate object with populated fields
// Using ES6 imports
import { Users } from '../models/user'
import { Response, Request } from 'express';
import { paginate } from 'paginate-mongoose-data';
class UserController {
async list(req: Request, res: Response) {
try {
const {message , data, meta } = await paginate(Users);
logger.info(`${message} ${JSON.stringify(data)}`);
return res.status(200).send({ message, data, meta });
} catch (error) {
logger.error(`error occured unable to list users ${JSON.stringify(error)}`);
return res.status(500).send(responsesHelper.error(500, `${error}`));
}
};
}
// Using Nodejs require()
const Users = require('../models/user');
const Paginate = require('paginate-mongoose-data').paginate;
const list = async(req, res) => {
try {
const {message , data, meta } = await Paginate(Users);
logger.info(`${message} ${JSON.stringify(data)}`);
return res.status(200).send({ message, data, meta });
} catch (error) {
logger.error(`error occured unable to list users ${JSON.stringify(error)}`);
return res.status(500).send(responsesHelper.error(500, `${error}`));
}
}
module.exports = list;
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to run test.
npm test
Adeyemi Kayode - Initial work - paginate-mongoose-data
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details