diff --git a/types/mongoose-paginate-v2/index.d.ts b/types/mongoose-paginate-v2/index.d.ts new file mode 100644 index 0000000000..ca8b7eac91 --- /dev/null +++ b/types/mongoose-paginate-v2/index.d.ts @@ -0,0 +1,60 @@ +// Type definitions for mongoose-paginate-v2 1.0 +// Project: https://github.com/aravindnc/mongoose-paginate-v2 +// Definitions by: Linus Brolin +// simonxca +// woutgg +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 +// +// Based on type declarations for mongoose-paginate 5.0.0. + +declare module 'mongoose' { + interface CustomLabels { + totalDocs?: string; + limit?: string; + page?: string; + totalPages?: string; + docs?: string; + nextPage?: string; + prevPage?: string; + } + + interface PaginateOptions { + /* tslint:disable-next-line: ban-types */ + select?: Object | string; + /* tslint:disable-next-line: ban-types */ + sort?: Object | string; + customLabels?: CustomLabels; + collation?: CollationOptions; + /* tslint:disable-next-line: ban-types */ + populate?: Object[] | string[] | Object | string; + lean?: boolean; + leanWithId?: boolean; + offset?: number; + page?: number; + limit?: number; + } + + interface PaginateResult { + docs: T[]; + total: number; + limit: number; + page?: number; + pages?: number; + offset?: number; + [customLabel: string]: T[] | number | undefined; + } + + interface PaginateModel extends Model { + paginate(query?: object, options?: PaginateOptions, callback?: (err: any, result: PaginateResult) => void): Promise>; + } + + function model( + name: string, + schema?: Schema, + collection?: string, + skipInit?: boolean): PaginateModel; +} + +import mongoose = require('mongoose'); +export function _(schema: mongoose.Schema): void; diff --git a/types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts b/types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts new file mode 100644 index 0000000000..200ca4e12a --- /dev/null +++ b/types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts @@ -0,0 +1,80 @@ +/** + * Created by Linus Brolin . + */ + +import { + Schema, + model, + PaginateModel, + PaginateOptions, + PaginateResult, + Document +} from 'mongoose'; +import mongoosePaginate = require('mongoose-paginate'); +import { Router, Request, Response } from 'express'; + +//#region Test Models +interface User extends Document { + email: string; + username: string; + password: string; +} + +const UserSchema: Schema = new Schema({ + email: String, + username: String, + password: String +}); + +UserSchema.plugin(mongoosePaginate); + +interface UserModel extends PaginateModel {} + +const UserModel: UserModel = model('User', UserSchema) as UserModel; +//#endregion + +//#region Test Paginate +const router: Router = Router(); + +router.get('/users.json', (req: Request, res: Response) => { + const descending = true; + const options: PaginateOptions = {}; + options.select = 'email username'; + options.sort = { username: (descending ? -1 : 1) }; + options.collation = { locale: 'en_US', strength: 1 }; + options.populate = ''; + options.lean = true; + options.leanWithId = false; + options.offset = 0; + options.page = 1; + options.limit = 10; + options.customLabels = { + totalDocs: 'totalDocsCustom', + limit: 'limitCustom', + page: 'pageCustom', + totalPages: 'totalPagesCustom', + docs: 'docsCustom', + nextPage: 'nextPageCustom', + prevPage: 'prevPageCustom' + }; + + UserModel + .paginate({}, options, (err: any, value: PaginateResult) => { + if (err) { + console.log(err); + return res.status(500).send(err); + } + + console.log('totalDocs: ' + value.totalDocsCustom); + console.log('limit: ' + value.limitCustom); + console.log('page: ' + value.pageCustom); + console.log('nextPage: ' + value.nextPageCustom); + console.log('prevPage: ' + value.prevPageCustom); + console.log('totalPages: ' + value.totalPagesCustom); + console.log('offset: ' + value.offset); + console.log('docs: '); + console.dir(value.docsCustom); + return res.json(value); + }); +}); +//#endregion diff --git a/types/mongoose-paginate-v2/tsconfig.json b/types/mongoose-paginate-v2/tsconfig.json new file mode 100644 index 0000000000..1e367240a9 --- /dev/null +++ b/types/mongoose-paginate-v2/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mongoose-paginate-v2-tests.ts" + ] +} diff --git a/types/mongoose-paginate-v2/tslint.json b/types/mongoose-paginate-v2/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/mongoose-paginate-v2/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}