Socket
Book a DemoInstallSign in
Socket

mongoose-simple-pagination

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-simple-pagination

Mongoose simple pagination plugin written specially for typescript and async purposes

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

mongoose-simple-pagination

npm GitHub package.json version GitHub Workflow Status GitHub

Mongoose simple pagination plugin written specially for typescript and async purposes.

Why?

I created this package because I met typing problems in alternative libraries such as mongoose-paginate-v2. This package is for people who looking for typescript solution with typings.

Installation

npm i mongoose-simple-pagination

Usage

Add plugin to a schema and then use model paginate method:

import { Document, model, Schema } from 'mongoose';

import { mongooseSimplePagination, PaginationModel } from 'mongoose-simple-pagination';

interface Product extends Document {
  // ...
}

const productSchema = new Schema<Product>(
  {
    // ...
  }
);

productSchema.plugin(mongoosePaginate);

export const ProductModel = model<Product, PaginationModel<Product>>(
  'Product',
  productSchema,
  'products'
);

(async () => {
  const pagination = await ProductModel.paginate();
  const products = pagination.documents; // Our products array.
  const hasNextPage = pagination.hasNextPage; // true.
})();

API

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

Returns promise

Parameters

  • [filter] {FilterQuery} - Filter query criteria. Documentation
  • [options] {Options}
    • [collation] {CollationDocument} - Specify the collation. Documentation
    • [lean=false] {boolean | any} - Should return plain javascript objects instead of Mongoose document object.
    • [page=1] {number}
    • [perPage=20] {number}
    • [populate] {string | PopulateOptions | PopulateOptions[]} - Paths which should be populated with other documents. Documentation
    • [projection=null] {any | null} - Get/set the query projection. Documentation
    • [queryOptions={}] {QueryOptions | null} - Query options passed to Mongoose's find() function. Documentation
    • [select=''] {string | any} - Fields to return (by default returns all fields) . Documentation
    • [sort=''] {string | any} - Sort order. Documentation

Return value

Promise fulfilled with Pagination object having properties:

  • documents {T[]} - Array of documents.
  • hasNextPage {bool} - Availability of next page.
  • hasPreviousPage {bool} - Availability of previous page.
  • nextPage {number} - Next page number if available or NULL.
  • page {number} - Current page number.
  • perPage {number} - Number of documents per page.
  • previousPage {number} - Previous page number if available or NULL.
  • totalDocuments {number} - Total number of documents in collection that match a query.
  • totalPages {number} - Total number of pages.

License

MIT

Keywords

mongoose

FAQs

Package last updated on 02 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts