Add pagination option. Rework of #41228 (#41381)

* 💄 apply project Prettier

* Add pagination option. Rework of #41228

Thx to @RyannGalea
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-01-07 00:30:32 +01:00 committed by Armando Aguirre
parent 1e125b70f4
commit 8519e88609
2 changed files with 74 additions and 79 deletions

View File

@ -11,72 +11,74 @@
// 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 CustomLabels {
totalDocs?: string;
limit?: string;
page?: string;
totalPages?: string;
docs?: string;
nextPage?: string;
prevPage?: string;
}
interface ReadOptions {
pref: string;
tags?: any[];
}
interface ReadOptions {
pref: string;
tags?: any[];
}
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 | QueryPopulateOptions;
lean?: boolean;
leanWithId?: boolean;
offset?: number;
page?: number;
limit?: number;
read?: ReadOptions;
}
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 | QueryPopulateOptions;
lean?: boolean;
leanWithId?: boolean;
offset?: number;
page?: number;
limit?: number;
read?: ReadOptions;
/* If pagination is set to `false`, it will return all docs without adding limit condition. (Default: `true`) */
pagination?: boolean;
}
interface QueryPopulateOptions {
/** space delimited path(s) to populate */
path: string;
/** optional fields to select */
select?: any;
/** optional query conditions to match */
match?: any;
/** optional model to use for population */
model?: string | Model<any>;
/** optional query options like sort, limit, etc */
options?: any;
/** deep populate */
populate?: QueryPopulateOptions | QueryPopulateOptions[];
}
interface QueryPopulateOptions {
/** space delimited path(s) to populate */
path: string;
/** optional fields to select */
select?: any;
/** optional query conditions to match */
match?: any;
/** optional model to use for population */
model?: string | Model<any>;
/** optional query options like sort, limit, etc */
options?: any;
/** deep populate */
populate?: QueryPopulateOptions | QueryPopulateOptions[];
}
interface PaginateResult<T> {
docs: T[];
total: number;
limit: number;
page?: number;
pages?: number;
offset?: number;
[customLabel: string]: T[] | number | undefined;
}
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>>;
}
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>;
function model(name: string, schema?: Schema, collection?: string, skipInit?: boolean): PaginateModel<any>;
}
import mongoose = require('mongoose');

View File

@ -2,28 +2,21 @@
* Created by Linus Brolin <https://github.com/linusbrolin/>.
*/
import {
Schema,
model,
PaginateModel,
PaginateOptions,
PaginateResult,
Document
} from 'mongoose';
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;
email: string;
username: string;
password: string;
}
const UserSchema: Schema = new Schema({
email: String,
username: String,
password: String
email: String,
username: String,
password: String,
});
UserSchema.plugin(mongoosePaginate);
@ -40,11 +33,12 @@ 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.sort = { username: descending ? -1 : 1 };
options.collation = { locale: 'en_US', strength: 1 };
options.pagination = false;
options.populate = '';
options.populate = {
path: '',
path: '',
};
options.lean = true;
options.leanWithId = false;
@ -58,11 +52,10 @@ router.get('/users.json', (req: Request, res: Response) => {
totalPages: 'totalPagesCustom',
docs: 'docsCustom',
nextPage: 'nextPageCustom',
prevPage: 'prevPageCustom'
prevPage: 'prevPageCustom',
};
UserModel
.paginate({}, options, (err: any, value: PaginateResult<User>) => {
UserModel.paginate({}, options, (err: any, value: PaginateResult<User>) => {
if (err) {
console.log(err);
return res.status(500).send(err);