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

mongoose-paginate-ts

v1.3.1

Published

Typescript pagination plugin for mongoose

Downloads

5,600

Readme

npm version

mongoose-paginate-ts

Typescript pagination (with page or cursor) plugin for Mongoose

NPM

Installation

npm install mongoose-paginate-ts

Usage

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

import { mongoosePagination, Pagination } from "mongoose-paginate-ts";
type User = mongoose.Document & {
  username: String,
  accounts: [mongoose.Types.ObjectId]
};
const userSchema = new Schema({
  username: String,
  accounts: [{ type: ObjectId, ref: "Account" }]
});
userSchema.plugin(mongoosePagination);
const User: Pagination<User> = mongoose.model<User, Pagination<User>>("User", userSchema);

//User.paginate()

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

Parameters

  • [options] {Object}
    • [query] {Object} - Query conditions. Documentation
    • [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
    • [page=1] {Number},
    • [limit=10] {Number}, number of docs per page, default is 10
    • [key=_id] {String}, cursor id pagination
    • [startingAfter] {String}, A cursor for use in pagination. startingAfter is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include startingAfter=obj_foo in order to fetch the next page of the list.
    • [endingBefore] {String}, A cursor for use in pagination. endingBefore is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, starting with obj_bar, your subsequent call can include endingBefore=obj_bar in order to fetch the previous page of the list.
    • [forceCountFunction=false] {Boolean} - Set this to true, if you need to support $geo queries.
    • [lean=true] {Boolean} - Set this to false, if you need to disable the lean.
  • [callback(err, result)] - The callback is called once pagination results are retrieved or when an error has occurred

Result value

Promise fulfilled with an Pagination:

class PaginationModel {
  totalDocs: number | undefined;
  limit: number | undefined = 0;
  totalPages: number | undefined;
  page: number | undefined;
  pagingCounter: number | undefined;
  hasPrevPage: Boolean | undefined = false;
  hasNextPage: Boolean | undefined = false;
  prevPage: number | undefined;
  nextPage: number | undefined;
  hasMore: Boolean | undefined = false;
  docs: any[] = [];
}

Examples

Paginate with

User.paginate({}).then((error: Error, result: any) => {
  // ...
});

var results = await User.paginate({})

More advanced example

var options = {
  query: {},
  select: "title date author",
  sort: { date: -1 },
  populate: "account",
  limit: 5
};

User.paginate(options).then((error: Error, result: any) => {
  // ...
});

var results = await User.paginate(options)

License

MIT