Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#65410 [mongoose-aggregate-paginate-v2] Add…
Browse files Browse the repository at this point in the history
… 'useFacet' to PaginateOptions by @tiagojsag
  • Loading branch information
tiagojsag authored and Desplandis committed Jul 3, 2023
1 parent 5bf520d commit 720fe8e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions types/mongoose-aggregate-paginate-v2/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ declare module 'mongoose' {
pagination?: boolean | undefined;
allowDiskUse?: boolean | undefined;
countQuery?: object | undefined;
useFacet?: boolean | undefined;
}

interface QueryPopulateOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
* Adapted to mongoose-aggregate-paginate-v2 by Alexandre Croteau <https://github.com/acrilex1>
*/

import {
Schema,
model,
Aggregate,
AggregatePaginateModel,
PaginateOptions,
AggregatePaginateResult,
} from 'mongoose';
import { Schema, model, Aggregate, AggregatePaginateModel, PaginateOptions, AggregatePaginateResult } from 'mongoose';
import mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
import { Router, Request, Response } from 'express';

Expand Down Expand Up @@ -103,4 +96,31 @@ router.get('/stats/hobbies.json', async (req: Request, res: Response) => {
return res.status(500).send(err);
}
});

// Handle useFacet option
router.get('/stats/hobbies.json', async (req: Request, res: Response) => {
const aggregate: Aggregate<HobbyStats[]> = UserModel.aggregate()
.unwind('$hobbies')
.group({
_id: '$hobbies',
count: { $count: {} },
})
.addFields({ hobby: '$_id' })
.project({ _id: 0 })
.sort({ count: -1 });

const options: PaginateOptions = {
page: 1,
limit: 10,
useFacet: false,
};

try {
const value: AggregatePaginateResult<HobbyStats> = await UserModel.aggregatePaginate(aggregate, options);
return res.json(value);
} catch (err) {
console.log(err);
return res.status(500).send(err);
}
});
//#endregion

0 comments on commit 720fe8e

Please sign in to comment.