如何使用 NestJS 提供服务器端分页?

编程入门 行业动态 更新时间:2024-10-28 20:28:36
本文介绍了如何使用 NestJS 提供服务器端分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

鉴于使用 Nestjs、MongoDB(mongoose) 的 MEVN 堆栈,我正在努力设置服务器端分页.我的方法是使用 mongoose-aggregate-paginate-v2,但我一直无法从我的研究中提炼出我需要的东西1 在 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 research1 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.

    我可以建议你自己制作一个自定义函数来做到这一点,或者使用 www.npmjs/package/mongoose-paginate 因为该插件不需要@types/mongoose.

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

    更多推荐

    如何使用 NestJS 提供服务器端分页?

    本文发布于:2023-11-22 23:52:29,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1619386.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:分页   如何使用   服务器端   NestJS

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!