问题描述
鉴于使用 Nestjs、MongoDB(mongoose) 的 MEVN 堆栈,我正在努力设置服务器端分页.我的方法是使用 mongoose-aggregate-paginate-v2,但我一直无法从我的研究中提炼出我需要的东西 在 Nestjs(typescript) 和 mongoose 的框架内完成这项工作.谢谢你的帮助..
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 to make this work within the framework of Nestjs(typescript) and mongoose. Thanks for the assist..
以下有关 Nestjs 猫鼬模型 和 mongoose-aggregate-paginate-v2 设置,我有以下内容:
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'],
},
];
我正在阅读 Nestjs 文档、mongoose 文档和 typescript 文档.沿着这条路径的某个地方有一种方法可以在我的 Contact 模型上提供 aggregatePaginate 方法,以便我可以调用:
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.
研究
- Mongoose 的 Typescript 方式......?
- 完整指南在 Mongoose 中使用带有精益()函数的打字稿
- Typescript 完整指南使用 Mongoose for Node.js
- MosesEsan/mesan-nodejs-crud-api-with-pagination-filtering-grouping-and-sorting-capabilities
- Node.js API:添加具有分页、过滤、分组和排序功能的 CRUD 操作.
- 以正确的方式构建 API 分页
- SO:猫鼬插件 nestjs
- SO:使用 mongoose 和 nestjs 进行分页
推荐答案
NestJs 和 mongoose-aggregate-paginate-v2 和 mongoose-paginate-v2 因为那些插件使用的是@types/mongoose,所以如果你使用@types/mongoose.
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.
我告诉你这个是因为我正在尝试同样的事情并弄清楚在 Nestjs 解决@types/mongoose 的问题之前不可能实现 mongoo-aggreate-paginate-v2/mongoose-paginate-v2.
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.
我可以建议你自己制作一个自定义函数来做到这一点,或者使用 https://www.npmjs.com/package/mongoose-paginate 因为该插件不需要@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.
这篇关于如何使用 NestJS 提供服务器端分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!