Skip to content

3zrv/mongoose-simple-pager

Repository files navigation

npm version Coverage Status CI

mongoose-simple-pager

Pagination plugin for mongoose.js.

Requirements

Installation

$ npm install mongoose-simple-pager

Example usage

// userModel.js
import mongoose from "mongoose";
import paginate from "mongoose-simple-pager";

const userSchema = mongoose.Schema({
  firstName: {
    type: String,
    required: true,
  },
  lastName: {
    type: String,
    required: true,
  },
  userName: {
    type: String,
    required: true,
    unique: true,
  },
  roles: [
    {
      type: mongoose.SchemaTypes.ObjectId,
      ref: "roles",
    },
  ],
});

userSchema.plugin(paginate);
// controller.js
const User = mongoose.model("userModel");

const options = {
  limit: 5,
  page: 1,
};

return await User.paginate(options);

Output will be:

{
  "data": [
    {
      "obj1": {...}
    },
    {
      "obj2": {...}
    },
    {
      "obj3": {...}
    },
    {
      "obj4": {...}
    },
    {
      "obj5": {...}
    }
  ],
  "pagination": {
    "total": 123
  }
}

Adding a query and populate options

const User = mongoose.model("userModel");

const options = {
  limit: 5,
  page: 1,
};

const query = {
  $or: [
    {
      userName: {
        $regex: "x",
        $options: "i",
      },
    },
  ],
};

return await User.paginate(options, "roles", query);

Todo

  • Accept aggregate queries
  • Migrate to typescript

Contributing

  1. Fork it ( https://github.com/0x3zra/mongoose-simple-pager/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

The MIT License (MIT)