title: How to provide server-side pagination with NestJS?
body: <p>Given a MEVN stack using Nestjs, MongoDB(mongoose) I am working to setup server-side pagination. My approach is to use the mongoose-aggregate-paginate-v2, but I have not been able to distill what I need from my research<sup>1</sup> to make this work within the framework of Nestjs(typescript) and mongoose. Thanks for the assist..</p>
  <p>Following documentation on <a href="https://docs.nestjs.com/recipes/mongodb#model-injection" rel="nofollow noreferrer">Nestjs mongoose models</a>, and <a href="https://www.npmjs.com/package/mongoose-aggregate-paginate-v2" rel="nofollow noreferrer">mongoose-aggregate-paginate-v2 setup</a>, I have the following:</p>
  <p><code>contact.provider.ts</code></p>
  <pre><code>import mongoose, { Connection, AggregatePaginateResult, model } from &quot;mongoose&quot;;
  import { ContactSchema } from &quot;./contact.schema&quot;;
  import aggregatePaginate from &quot;mongoose-aggregate-paginate-v2&quot;;
  import { IContact } from &quot;./interfaces/contact.interface&quot;;
  
  // notice plugin setup:
  ContactSchema.plugin(aggregatePaginate);
  
  // is this correct ?
  interface ContactModel&lt;T extends Document&gt; extends AggregatePaginateResult&lt;T&gt; {}
  
  // how to create model for factory use ?
  export const ContactModel: ContactModel&lt;any&gt; = model&lt;IContact&gt;('Contact', ContactSchema) as ContactModel&lt;IContact&gt;;
  
  export const contactProvider = [
    {
      provide: 'CONTACT_MODEL',
      useFactory: (connection: Connection) =&gt; {
        // how to instantiate model ?
        let model = connection.model&lt;ContactModel&lt;any&gt;&gt;('Contact', ContactSchema);
        return model;
      },
      inject: ['DATABASE_CONNECTION'],
    },
  ];
  </code></pre>
  <p>I am between reading the Nestjs documentation, mongoose documentation, and typescript documentation. Somewhere along this path there is a way to provide the aggregatePaginate method on my Contact model, so that I can call like:</p>
  <p><code>contact.service.ts</code></p>
  <pre><code>// Set up the aggregation
  const myAggregate = this.contactModel.aggregate(aggregate_options);
  const result = await this.contactModel.aggregatePaginate(myAggregate, options); // aggregatePaginate does not exist!
  </code></pre>
  <p>Review code in progress - available on <a href="https://github.com/abcox/toybox-api/tree/server-side-pagination" rel="nofollow noreferrer">this branch</a>.</p>
  <p>Research</p>
  <ol>
  <li><a href="https://stackoverflow.com/questions/34482136/mongoose-the-typescript-way">Mongoose the Typescript way…?</a></li>
  <li><a href="https://jasonching2005.medium.com/complete-guide-for-using-typescript-in-mongoose-with-lean-function-e55adf1189dc" rel="nofollow noreferrer">Complete Guide for using Typescript in Mongoose with lean() function</a></li>
  <li><a href="https://medium.com/@agentwhs/complete-guide-for-typescript-for-mongoose-for-node-js-8cc0a7e470c1" rel="nofollow noreferrer">Complete guide for Typescript with Mongoose for Node.js</a></li>
  <li><a href="https://github.com/MosesEsan/mesan-nodejs-crud-api-with-pagination-filtering-grouping-and-sorting-capabilities/blob/master/src/controllers/event.js" rel="nofollow noreferrer">MosesEsan/mesan-nodejs-crud-api-with-pagination-filtering-grouping-and-sorting-capabilities</a></li>
  <li><a href="https://medium.com/swlh/node-js-api-add-crud-operations-with-pagination-filtering-grouping-and-sorting-capabilities-55375ad0b774" rel="nofollow noreferrer">Node.js API: Add CRUD Operations With Pagination, Filtering, Grouping, and Sorting Capabilities.</a></li>
  <li><a href="https://www.mixmax.com/engineering/api-paging-built-the-right-way/" rel="nofollow noreferrer">API Paging Built The Right Way</a></li>
  <li><a href="https://stackoverflow.com/questions/49387454/mongoose-plugins-nestjs">SO: Mongoose Plugins nestjs</a></li>
  <li><a href="https://stackoverflow.com/questions/64889678/pagination-with-mongoose-and-nestjs">SO: Pagination with mongoose and nestjs</a></li>
  </ol>