How to provide server-side pagination with NestJS?

血红的双手。 提交于 2021-02-08 11:03:18

问题


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 research1 to make this work within the framework of Nestjs(typescript) and mongoose. Thanks for the assist..

Following documentation on Nestjs mongoose models, and mongoose-aggregate-paginate-v2 setup, I have the following:

contact.provider.ts

import mongoose, { Connection, AggregatePaginateResult, model } from "mongoose";
import { ContactSchema } from "./contact.schema";
import aggregatePaginate from "mongoose-aggregate-paginate-v2";
import { IContact } from "./interfaces/contact.interface";

// notice plugin setup:
ContactSchema.plugin(aggregatePaginate);

// is this correct ?
interface ContactModel<T extends Document> extends AggregatePaginateResult<T> {}

// how to create model for factory use ?
export const ContactModel: ContactModel<any> = model<IContact>('Contact', ContactSchema) as ContactModel<IContact>;

export const contactProvider = [
  {
    provide: 'CONTACT_MODEL',
    useFactory: (connection: Connection) => {
      // how to instantiate model ?
      let model = connection.model<ContactModel<any>>('Contact', ContactSchema);
      return model;
    },
    inject: ['DATABASE_CONNECTION'],
  },
];

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:

contact.service.ts

// Set up the aggregation
const myAggregate = this.contactModel.aggregate(aggregate_options);
const result = await this.contactModel.aggregatePaginate(myAggregate, options); // aggregatePaginate does not exist!

Review code in progress - available on this branch.

Research

  1. Mongoose the Typescript way…?
  2. Complete Guide for using Typescript in Mongoose with lean() function
  3. Complete guide for Typescript with Mongoose for Node.js
  4. MosesEsan/mesan-nodejs-crud-api-with-pagination-filtering-grouping-and-sorting-capabilities
  5. Node.js API: Add CRUD Operations With Pagination, Filtering, Grouping, and Sorting Capabilities.
  6. API Paging Built The Right Way
  7. SO: Mongoose Plugins nestjs
  8. SO: Pagination with mongoose and nestjs

回答1:


There is a conflict between NestJs and mongoose-aggregate-paginate-v2 and mongoose-paginate-v2 because those plugins are using @types/mongoose, so, NestJS has conflicts if you use @types/mongoose.

I tell you this because i was trying the same thing and figure it out that is not posible to implement mongoo-aggreate-paginate-v2/mongoose-paginate-v2 till Nestjs solve the issues with @types/mongoose.

I can recommend you make a custom function by your own to do that or use https://www.npmjs.com/package/mongoose-paginate because that plugin does not require @types/mongoose.



来源:https://stackoverflow.com/questions/65669164/how-to-provide-server-side-pagination-with-nestjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!