Merge pull request #31087 from woutgg/mongoose-paginate-v2

Add types for mongoose-paginate-v2 (modified from mongoose-paginate)
This commit is contained in:
Sheetal Nandi
2019-01-03 11:07:04 -08:00
committed by GitHub
4 changed files with 166 additions and 0 deletions

60
types/mongoose-paginate-v2/index.d.ts vendored Normal file
View File

@@ -0,0 +1,60 @@
// Type definitions for mongoose-paginate-v2 1.0
// Project: https://github.com/aravindnc/mongoose-paginate-v2
// Definitions by: Linus Brolin <https://github.com/linusbrolin>
// simonxca <https://github.com/simonxca>
// woutgg <https://github.com/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<T> {
docs: T[];
total: number;
limit: number;
page?: number;
pages?: number;
offset?: number;
[customLabel: string]: T[] | number | undefined;
}
interface PaginateModel<T extends Document> extends Model<T> {
paginate(query?: object, options?: PaginateOptions, callback?: (err: any, result: PaginateResult<T>) => void): Promise<PaginateResult<T>>;
}
function model(
name: string,
schema?: Schema,
collection?: string,
skipInit?: boolean): PaginateModel<any>;
}
import mongoose = require('mongoose');
export function _(schema: mongoose.Schema): void;

View File

@@ -0,0 +1,80 @@
/**
* Created by Linus Brolin <https://github.com/linusbrolin/>.
*/
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<T extends Document> extends PaginateModel<T> {}
const UserModel: UserModel<User> = model<User>('User', UserSchema) as UserModel<User>;
//#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<User>) => {
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

View File

@@ -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"
]
}

View File

@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}