mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-03 19:51:53 +00:00
Added tests
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/// <reference path="./mongoose-paginate.d.ts" />
|
||||
/// <reference path="../mongoose/mongoose.d.ts" />
|
||||
/// <reference path="../express/express.d.ts" />
|
||||
|
||||
/**
|
||||
* Created by Linus Brolin <https://github.com/linusbrolin/>.
|
||||
*/
|
||||
|
||||
import {
|
||||
Schema,
|
||||
model,
|
||||
PaginateModel,
|
||||
PaginateOptions,
|
||||
PaginateResult
|
||||
} from 'mongoose';
|
||||
import * as mongoosePaginate from 'mongoose-paginate';
|
||||
import { Router, Request, Response } from 'express';
|
||||
|
||||
|
||||
//#region Test Models
|
||||
interface User {
|
||||
email: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
const UserSchema: Schema = new Schema({
|
||||
email: String,
|
||||
username: String,
|
||||
password: String
|
||||
});
|
||||
|
||||
UserSchema.plugin(mongoosePaginate);
|
||||
|
||||
type UserModel<T> = _UserModel<T> & PaginateModel<T>;
|
||||
interface _UserModel<T> {}
|
||||
|
||||
let UserModel: UserModel<User> = model<User>('User', UserSchema) as UserModel<User>;
|
||||
//#endregion
|
||||
|
||||
|
||||
//#region Test Paginate
|
||||
let router: Router = Router();
|
||||
|
||||
router.get('/users.json', function(req: Request, res: Response) {
|
||||
let descending: boolean = true;
|
||||
let options: PaginateOptions = <PaginateOptions>{};
|
||||
options.select = 'email username';
|
||||
options.sort = { 'username': (descending ? -1 : 1) };
|
||||
options.populate = '';
|
||||
options.lean = true;
|
||||
options.leanWithId = false;
|
||||
options.offset = 0;
|
||||
options.page = 1;
|
||||
options.limit = 10;
|
||||
|
||||
UserModel
|
||||
.paginate({}, options, (err: any, value: PaginateResult<User>) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
console.log('total: ' + value.total);
|
||||
console.log('limit: ' + value.limit);
|
||||
console.log('page: ' + value.page);
|
||||
console.log('pages: ' + value.pages);
|
||||
console.log('offset: ' + value.offset);
|
||||
console.log('docs: ');
|
||||
console.dir(value.docs);
|
||||
return res.json(value);
|
||||
});
|
||||
|
||||
});
|
||||
//#endregion
|
||||
Reference in New Issue
Block a user