From 6a627a20fd6d953c7024b06be3c961cda5c1f4db Mon Sep 17 00:00:00 2001 From: Sukaato Date: Tue, 2 Mar 2021 08:57:00 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20types=20declarations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/index.ts | 66 +++++++++++++++----------------------------- src/module.ts | 28 ------------------- src/plugin.ts | 50 +++++++++++++++++++++++++++++++++ test/utils/schema.ts | 6 ++-- 5 files changed, 76 insertions(+), 76 deletions(-) delete mode 100644 src/module.ts create mode 100644 src/plugin.ts diff --git a/package.json b/package.json index 9645c07..0ca22b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongoose-plugin-paginate", - "version": "1.0.0-pre2", + "version": "1.0.0-pre3", "description": "Pagination plugin for mongoose", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index dc25863..c836e80 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,52 +1,30 @@ -import { Document, Model, PaginateOption, PaginateResult, Schema } from 'mongoose'; +import 'mongoose'; -const defaultOptions: PaginateOption = { - query: {}, - page: 1, - limit: 10 -} - -async function paginate(options: PaginateOption = defaultOptions): Promise> { - const model = this as Model; - const documentCount = await model.estimatedDocumentCount(); - const query = options.query ?? defaultOptions.query; - const page = parse(options?.page, defaultOptions.page); - const limit = parse(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); +declare module 'mongoose' { - if (options.populate) { - if (!Array.isArray(options.populate)) options.populate = [ options.populate ]; - - options.populate.forEach(populate => { - documentsQuery.populate(populate); - }); + export interface PaginateOption { + query?: FilterQuery; + page?: number; + limit?: number; + populate?: PopulateOptions | PopulateOptions[]; } - const documents = await documentsQuery.exec(); - - return { - docs: documents, - docsCount: documentCount, - page: page, - limit: limit, - pagesCount: pages, - hasPrev: hasPrev, - hasNext: hasNext, - }; -} -function parse(value: any, defaultValue: number = 1): number { - const number = +value; + export interface PaginateResult { + docs: T[]; + docsCount: number; + page: number, + limit: number; + pagesCount: number; + hasPrev: boolean; + hasNext: boolean; + } - if (isNaN(number) || number < 1) return defaultValue; - return number; -} + export interface PaginateModel extends Model { + paginate(): Promise>; + paginate(options: PaginateOption): Promise>; + } -export function mongoosePaginate(schema: Schema) { - schema.statics.paginate = paginate; + export function model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): PaginateModel; } -export default mongoosePaginate; \ No newline at end of file +export * from './plugin'; \ No newline at end of file diff --git a/src/module.ts b/src/module.ts deleted file mode 100644 index a3552de..0000000 --- a/src/module.ts +++ /dev/null @@ -1,28 +0,0 @@ -import 'mongoose'; - -declare module 'mongoose' { - - export interface PaginateOption { - query?: FilterQuery; - page?: number; - limit?: number; - populate?: PopulateOptions | PopulateOptions[]; - } - - export interface PaginateResult { - docs: T[]; - docsCount: number; - page: number, - limit: number; - pagesCount: number; - hasPrev: boolean; - hasNext: boolean; - } - - export interface PaginateModel extends Model { - paginate(): Promise>; - paginate(options: PaginateOption): Promise>; - } - - export function model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): PaginateModel; -} \ No newline at end of file diff --git a/src/plugin.ts b/src/plugin.ts new file mode 100644 index 0000000..70d3e4c --- /dev/null +++ b/src/plugin.ts @@ -0,0 +1,50 @@ +import { Document, Model, PaginateOption, PaginateResult, Schema } from 'mongoose'; + +const defaultOptions: PaginateOption = { + query: {}, + page: 1, + limit: 10 +} + +async function paginate(options: PaginateOption = defaultOptions): Promise> { + const model = this as Model; + const documentCount = await model.estimatedDocumentCount(); + const query = options.query ?? defaultOptions.query; + const page = parse(options?.page, defaultOptions.page); + const limit = parse(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: any, defaultValue: number = 1): number { + const number = +value; + + if (isNaN(number) || number < 1) return defaultValue; + return number; +} + +export function mongoosePaginate(schema: Schema) { + schema.statics.paginate = paginate; +} \ No newline at end of file diff --git a/test/utils/schema.ts b/test/utils/schema.ts index 3149f08..9f8b3ab 100644 --- a/test/utils/schema.ts +++ b/test/utils/schema.ts @@ -1,5 +1,5 @@ -import { Document, model, PaginateModel, Schema } from 'mongoose'; -import mongoosePaginate from '../../src'; +import { Document, Model, model, PaginateModel, Schema } from 'mongoose'; +import { mongoosePaginate } from '../../src'; export interface Tag extends Document { name: string; @@ -15,7 +15,7 @@ export const TagsSchema = new Schema({ name: { type: String }, description: { type: String } }); -export const TagModel = model('Tag', TagsSchema); +export const TagModel: Model = model('Tag', TagsSchema); export const VideoSchema = new Schema