项目作者: LucaIaconelli

项目描述 :
Typescript pagination plugin for mongoose
高级语言: TypeScript
项目地址: git://github.com/LucaIaconelli/mongoose-paginate-ts.git
创建时间: 2020-05-08T10:48:01Z
项目社区:https://github.com/LucaIaconelli/mongoose-paginate-ts

开源协议:MIT License

下载


npm version

mongoose-paginate-ts

Typescript pagination (with page or cursor) plugin for Mongoose

NPM

Installation

  1. npm install mongoose-paginate-ts

Usage

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

  1. import { mongoosePagination, Pagination } from "mongoose-paginate-ts";
  2. type User = mongoose.Document & {
  3. username: String,
  4. accounts: [mongoose.Types.ObjectId]
  5. };
  6. const userSchema = new Schema({
  7. username: String,
  8. accounts: [{ type: ObjectId, ref: "Account" }]
  9. });
  10. userSchema.plugin(mongoosePagination);
  11. const User: Pagination<User> = mongoose.model<User, Pagination<User>>("User", userSchema);
  12. //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:

  1. class PaginationModel {
  2. totalDocs: number | undefined;
  3. limit: number | undefined = 0;
  4. totalPages: number | undefined;
  5. page: number | undefined;
  6. pagingCounter: number | undefined;
  7. hasPrevPage: Boolean | undefined = false;
  8. hasNextPage: Boolean | undefined = false;
  9. prevPage: number | undefined;
  10. nextPage: number | undefined;
  11. hasMore: Boolean | undefined = false;
  12. docs: any[] = [];
  13. }

Examples

Paginate with

  1. User.paginate({}).then((error: Error, result: any) => {
  2. // ...
  3. });
  4. var results = await User.paginate({})

More advanced example

  1. var options = {
  2. query: {},
  3. select: "title date author",
  4. sort: { date: -1 },
  5. populate: "account",
  6. limit: 5
  7. };
  8. User.paginate(options).then((error: Error, result: any) => {
  9. // ...
  10. });
  11. var results = await User.paginate(options)

License

MIT