1.3.1 • Published 2 years ago
mongoose-paginate-ts v1.3.1
mongoose-paginate-ts
Typescript pagination (with page or cursor) plugin for Mongoose
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
1.3.1
2 years ago
1.3.0
2 years ago
1.2.7
3 years ago
1.2.6
3 years ago
1.2.5
3 years ago
1.2.4
3 years ago
1.2.3
3 years ago
1.2.2
3 years ago
1.2.1
3 years ago
1.2.0
4 years ago
1.1.8
4 years ago
1.1.7
4 years ago
1.1.6
4 years ago
1.1.5
4 years ago
1.1.4
4 years ago
1.1.3
4 years ago
1.1.2
4 years ago
1.1.1
4 years ago
1.1.0
5 years ago
1.0.14
5 years ago
1.0.13
5 years ago
1.0.12
5 years ago
1.0.11
5 years ago
1.0.9
5 years ago
1.0.8
5 years ago
1.0.10
5 years ago
1.0.7
5 years ago
1.0.6
5 years ago
1.0.5
5 years ago
1.0.4
5 years ago
1.0.3
5 years ago
1.0.2
5 years ago
1.0.1
5 years ago
1.0.0
6 years ago