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

ts-mongoose-pagination

v1.1.2

Published

Typescript pagination plugin for mongoose

Downloads

230

Readme

ts-mongoose-pagination

npm version Dependency Status devDependency Status contributions welcome Downloads HitCount

Typescript pagination plugin for Mongoose

NPM

Installation

npm install ts-mongoose-pagination

or

yarn add ts-mongoose-pagination

Usage

Add plugin for a mongoose schema to inject a paginate method for pagination:

import { mongoosePagination } from "ts-mongoose-pagination";

const userSchema = new Schema({
  username: String,
  accounts: [{ type: ObjectId, ref: "Account" }]
});
userSchema.plugin(mongoosePagination);
const User: PaginateModel<TUser> = mongoose.model("User", userSchema);

//User.paginate()

Model.paginate([query conditions], [options], [callback])

Parameters

  • [query] {Object} - Query conditions. Documentation
  • [options] {Object}
    • [select] {Object | String} - Fields to return (by default returns all fields). Documentation
    • [sort] {Object | String} - Sort order. Documentation
    • [populate] {Object | String} - Paths which should be populated with other documents. Documentation
    • [lean=false] {Boolean} - Should return plain javascript objects instead of Mongoose documents Documentation
    • [page=1] {Number}, if undefined, will return all docs without pagination
    • [perPage=10] {Number}, number of docs per page, default is 10
  • [callback(err, result)] - If specified the callback is called once pagination results are retrieved or when an error has occurred

Return value

Promise fulfilled with an IPaginateResult:

interface IPaginateResult<T> {
  data: T[];
  pagination: IPagination;
}

interface IPagination {
  hasPrevPage: boolean;
  hasNextPage: boolean;
  prevPage: number | null;
  nextPage: number | null;
  perPage: number;
  page?: number | null;
  totalPages?: number;
}

Tests

Coverage

--------------|----------|----------|----------|----------|-------------------|
File          |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files     |    96.43 |       70 |       80 |    96.43 |                   |
 src          |    97.73 |       75 |      100 |    97.73 |                   |
  index.ts    |    97.73 |       75 |      100 |    97.73 |               116 |
--------------|----------|----------|----------|----------|-------------------|

Run tests

  1. Set up local mongo db
  2. Run:
yarn
yarn test

Examples

Detailed examples could be found in Pagination.test.ts

Paginate with

await Model.paginate({})
});

More advanced example

var conditions = {};
var options = {
  select: "title date author",
  sort: { date: -1 },
  populate: "account",
  lean: true,
  perPage: 5
};

User.paginate(conditions, options).then(result => {
  // ...
});

Explaination for some choices made

  1. Why remove the offset in the options? Think about the scenario when we use offset and limit(refer to the implementation in mongoose-paginate)

    User.paginate(conditions, {offset:50, limit: 10}).then(result => {
    // ...

    why not just use:

    User.find(conditions, { offset: 50, limit: 10 }).then(result => {
      // ...
    });

Acknowledgement

Thanks for the insparation from the following mongoose pagination js implementation. mongoose-paginate mongoose-paginate-v2

License

MIT