mongoose-simple-pagination
v1.0.0
Published
Mongoose simple pagination plugin written specially for typescript and async purposes
Downloads
1
Maintainers
Readme
mongoose-simple-pagination
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'sfind()
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.