mongoose-plugin-paginate
Advanced tools
Comparing version 1.0.0-pre2 to 1.0.0-pre3
@@ -1,3 +0,24 @@ | ||
import { Schema } from 'mongoose'; | ||
export declare function mongoosePaginate(schema: Schema): void; | ||
export default mongoosePaginate; | ||
import 'mongoose'; | ||
declare module 'mongoose' { | ||
interface PaginateOption<T extends Document> { | ||
query?: FilterQuery<T>; | ||
page?: number; | ||
limit?: number; | ||
populate?: PopulateOptions | PopulateOptions[]; | ||
} | ||
interface PaginateResult<T extends Document> { | ||
docs: T[]; | ||
docsCount: number; | ||
page: number; | ||
limit: number; | ||
pagesCount: number; | ||
hasPrev: boolean; | ||
hasNext: boolean; | ||
} | ||
interface PaginateModel<T extends Document> extends Model<T> { | ||
paginate(): Promise<PaginateResult<T>>; | ||
paginate(options: PaginateOption<T>): Promise<PaginateResult<T>>; | ||
} | ||
function model<T extends Document>(name: string, schema?: Schema<T>, collection?: string, skipInit?: boolean): PaginateModel<T>; | ||
} | ||
export * from './plugin'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mongoosePaginate = void 0; | ||
const defaultOptions = { | ||
query: {}, | ||
page: 1, | ||
limit: 10 | ||
}; | ||
async function paginate(options = defaultOptions) { | ||
var _a; | ||
const model = this; | ||
const documentCount = await model.estimatedDocumentCount(); | ||
const query = (_a = options.query) !== null && _a !== void 0 ? _a : defaultOptions.query; | ||
const page = parse(options === null || options === void 0 ? void 0 : options.page, defaultOptions.page); | ||
const limit = parse(options === null || options === void 0 ? void 0 : options.limit, defaultOptions.limit); | ||
const pages = parse(Math.ceil(documentCount / limit)); | ||
const hasPrev = page > 1; | ||
const hasNext = page < pages; | ||
const skip = limit * (page - 1); | ||
const documentsQuery = model.find(query).skip(skip).limit(limit); | ||
if (options.populate) { | ||
if (!Array.isArray(options.populate)) | ||
options.populate = [options.populate]; | ||
options.populate.forEach(populate => { | ||
documentsQuery.populate(populate); | ||
}); | ||
} | ||
const documents = await documentsQuery.exec(); | ||
return { | ||
docs: documents, | ||
docsCount: documentCount, | ||
page: page, | ||
limit: limit, | ||
pagesCount: pages, | ||
hasPrev: hasPrev, | ||
hasNext: hasNext, | ||
}; | ||
} | ||
function parse(value, defaultValue = 1) { | ||
const number = +value; | ||
if (isNaN(number) || number < 1) | ||
return defaultValue; | ||
return number; | ||
} | ||
function mongoosePaginate(schema) { | ||
schema.statics.paginate = paginate; | ||
} | ||
exports.mongoosePaginate = mongoosePaginate; | ||
exports.default = mongoosePaginate; | ||
require("mongoose"); | ||
__exportStar(require("./plugin"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "mongoose-plugin-paginate", | ||
"version": "1.0.0-pre2", | ||
"version": "1.0.0-pre3", | ||
"description": "Pagination plugin for mongoose", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
42436
88