@frabbit/mongoose-paginate

中文文档
7.2.6发布于 2023/10/24MITTypeScriptESM + CJS零依赖近 30 天下载 2064

Mongoose paginate plugin with full typescript support and support for latest mongoose package and types.

原始使用文档
文档目录4 节
  1. Features
  2. Install
  3. Usage
  4. License

@frabbit/mongoose-paginate npm

Mongoose paginate plugin with full typescript support and support for latest mongoose package and types.

Main idea is to add pagination options to find() and aggregate() without duplicating other params. Mongoose seems to be adding more typescript support, so old pagination plugins gets old.

Features

Major version is same as mongoose version

Provides 2 new methods on model

async function findPaginated(
  filter: FilterQuery<T> = {}, // same as  mongoose.find 
  pagination: PaginationOptions<T>, // plugin's param
  projection?: ProjectionType<T> | null, // same as  mongoose.find
  options?: QueryOptions<T> | null // same as  mongoose.find
): Promise<PaginatedResult<T>> {
}
async function aggregatePaginated(
  pipeline: PipelineStage[], // same as mongoose.aggregate
  paginate: PaginationOptions<T>, // plugin's param
  options?: AggregateOptions // same as mongoose.aggregate
): Promise<PaginatedResult<T>> {
}

the result is:

export class PaginatedResult<T> {
  /**
   * Documents on page
   */
  items: T[] = []

  /**
   * total documents count
   */
  total: number = 0

  /**
   * page
   */
  page: number = 1
  /**
   * total pages count
   */
  totalPages?: number = undefined

  /**
   * items per page
   */
  limit?: number = undefined

  /**
   * has previous page
   */
  hasPrevPage?: Boolean = false
  /**
   * has next page
   */
  hasNextPage?: Boolean = false

  /**
   * previous page number
   */
  prevPage?: number = undefined
  /**
   * next page number
   */
  nextPage?: number = undefined

  /**
   * The starting index/serial/chronological number of first document in current page.
   * (Eg: if page=2 and limit=10, then pagingCounter will be 11)
   */
  pagingCounter?: number = undefined

}

Install

npm i -D @frabbit/mongoose-paginate

Usage

import {mongoosePaginate, PaginatedModel, PaginatedResult} from "@frabbit/mongoose-paginate";

interface Some {
  field: string
}

type SomeDocument = Document & Some

const someSchema = new Schema({
  field: String,
});

someSchema.plugin(mongoosePaginate);

const someModel: PaginatedModel<SomeDocument>
("Some", someSchema);

const items: PaginatedResult<SomeDocument> = await someModel.findPaginated({}, {limit: 10, page: 1})
const items2: PaginatedResult<Some> = await someModel.findPaginated({}, {limit: 10, page: 1}, {field: 1}, {lean: true})

License

MIT License © 2023-PRESENT frenchrabbit

版本与发布记录

@frabbit/mongoose-paginate 共发布 11 个版本,首次发布于 2023/10/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
7.2.6latest2023/10/243 年前
7.2.52023/10/243 年前
7.2.32023/10/243 年前
7.2.22023/10/243 年前
7.2.12023/10/243 年前
7.2.02023/10/233 年前

关于 @frabbit/mongoose-paginate 的常见问题

@frabbit/mongoose-paginate 是什么?

Mongoose paginate plugin with full typescript support and support for latest mongoose package and types.

如何安装 @frabbit/mongoose-paginate?

在项目目录运行 npm install @frabbit/mongoose-paginate,或使用 pnpm add / yarn add / bun add。

@frabbit/mongoose-paginate 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@frabbit/mongoose-paginate 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@frabbit/mongoose-paginate 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.14.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@frabbit/mongoose-paginate 可以商用吗?

@frabbit/mongoose-paginate 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@frabbit/mongoose-paginate 的下载量有多少?

根据 npm 官方下载统计,@frabbit/mongoose-paginate 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,064 次,日均约 69 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@frabbit/mongoose-paginate 还在维护吗?

最新版本 7.2.6 发布于 2023/10/24(3 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/10/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。