2.0.1 • Published 4 years ago

paginate-mongoose-data v2.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
4 years ago

Paginate-mongoose-data

This package is use to paginate mongodb data.

NPM

Installation

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

Usage

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;

Contributing

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

Authors

Adeyemi Kayode - Initial work - paginate-mongoose-data

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

2.0.1

4 years ago

2.0.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago