aravindnc / mongoose-aggregate-paginate-v2

A cursor based custom aggregate pagination library for Mongoose with customizable labels.
MIT License
131 stars 23 forks source link

Being able to pass query directly/throw error if wrong usage #14

Closed devolution2409 closed 2 months ago

devolution2409 commented 4 years ago

Hi,

As i was trying to use the package, i was mislead into thinking you could pass a query object, instead of a mongose.model.aggregate() object.

The documention on npm states: Model.aggregatePaginate([aggregateQuery], [options], [callback]) [aggregate-query] {Object} - Aggregate Query criteria.

So, i tried to call the aggregatePaginate method like so:


let test = Models.MarketOffer.aggregatePaginate(          [
      {$match: config},
       {$addFields: {item: {$arrayElemAt: ['$items', 0]}}},
      {$unset: 'items'},
      {
        $lookup: {
          from: 'items',
          localField: 'item._tpl',
          foreignField: '_id',
          as: 'infos'
        }
      },
      {
        $addFields: {
          info: { $arrayElemAt: ['$infos', 0] }
        }
      },
      { $unset: 'infos' },
      {
        $addFields: {
          name: '$info._props.Name',
          shortName: '$info._props.ShortName',
          description: '$info._props.Description'
        }
      },
      { $unset: 'info' }
    ], {page: params.page}
    );

which obviously didn't work.

One could add a test to the aggregateQuery object, testing if the following properties exist _pipeline _model options

If the test isn't succesful, possibly throw an error

Not really a real issue, more like QOL improvement.

I could try to set-up the pull request if you feel like it could be useful :)

aravindnc commented 4 years ago

@devolution2409 If you can do a pull-request then that'd be great.

aravindnc commented 4 years ago

@devolution2409 Did your issue got resolved?