尝试了解nestjs/mongoose应用中的模型 [英] Trying to understand models in a nestjs/mongoose app

查看:406
本文介绍了尝试了解nestjs/mongoose应用中的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Nestjs的新手.我现在一直在玩一个非常简单的mongodb/mongoose db,其中包含书籍,作者和类型.我开始对我实际需要的模型(例如书籍)有了模糊的印象.

I am a newbie to nestjs. I have now been playing with a very simple mongodb/mongoose db with books, authors and genres. I started to have a blurry image of what models I actually need for, let's say, books.

目前我有2种型号:

export interface Book extends Document {
  id?: string;
  name: string;
  year?: string;
  authorId: string;
  genreId: string;
}

  • 需要注入服务构造函数@InjectModel('Book') private readonly bookModel: Model<Book>并调用book.save()
    • needed to inject into a service constructor @InjectModel('Book') private readonly bookModel: Model<Book> and to call book.save()
    • export class CreateBookDto {
        @IsString()
        @IsNotEmpty()
        readonly name: string;
      
        @IsOptional()
        @IsNumberString()
        @Min(1000)
        @Max(3000)
        readonly year: string;
      
        @IsMongoId()
        readonly authorId: string;
      
        @IsMongoId()
        readonly genreId: string;
      }
      

      • 据我了解-在创建过程中需要验证对象
      • (当然,还有另一个文件中的猫鼬模式)

        (plus, of course, a mongoose Schema in yet another file)

        现在,我遇到了一个问题,试图序列化书本答复,我开始认为我可能还需要另一个模型-Book类,该类允许我重命名属性,排除,公开等.该类基本上可以替换接口,除了

        Now I bumped into a problem trying to serialize the book response and I started to think that I probably need yet another model - a Book class, which would allow me to rename properties, exclude, expose, et al. The class could basically replace the interface, except that

        export class Book extends mongoose.Document

        似乎没有用.

        现在,闻起来好像是我对设计有误解-我不敢相信必须在代码库中维护3个模型.

        Now, it smells like I am misunderstanding the design - I cannot believe 3 models would be necessary to maintain in the codebase.

        有人可以向我介绍一下吗?-我可以只讲一个单一的课程吗?,它可以满足所有目的:提供类型,验证和序列化.如果是这样,如何使.save()部分起作用?

        Can someone shed light on me please - can I get away with just one single class which would satisfy all the purposes: providing the type, validation and serialization. If so, how to make the .save() part work?

        推荐答案

        我开始使用Nest.js时遇到了同样的问题...

        I came up with same issue when started to use Nest.js...

        在这里,我发现这篇重要的文章是如何使用Nest.js,MongoDB和Vue.js构建博客" https://morioh.com/p/74ffc8a798bb ,但仍然没有明确的答案关于如何添加 Nest.js序列化用于猫鼬.

        Here I found this big article re 'How To Build a Blog with Nest.js, MongoDB, and Vue.js' https://morioh.com/p/74ffc8a798bb , but still no clear answer re how to add a Nest.js Serialization for mongoose.

        然后,我想到了这篇文章 https://medium.com/@contactsunny/hide-properties-of-mongoose-objects-in-node-js-json-responses-a5437a5dbec2 至少可以说明一些问题并显示使用'UserSchema.methods.toJSON'的可能方向

        Then I came up with this article https://medium.com/@contactsunny/hide-properties-of-mongoose-objects-in-node-js-json-responses-a5437a5dbec2 which at least sheds some light and shows a possible direction with using 'UserSchema.methods.toJSON'

        这篇关于尝试了解nestjs/mongoose应用中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆