Mongodb NestJS与mongoose模式、接口和dto方法问题

Mongodb NestJS与mongoose模式、接口和dto方法问题,mongodb,typescript,mongoose,nestjs,Mongodb,Typescript,Mongoose,Nestjs,我是nestJS和mongoDB的新手,我不清楚为什么我们需要为每个要保存在mongoDB中的集合声明DTO、模式和接口。例如,我有一个收藏(不幸的是,我把它命名为collection,但这并不重要),这是我的DTO: export class CollectionDto { readonly description: string; readonly name: string; readonly expiration: Date; } 接口: import { Document

我是nestJS和mongoDB的新手,我不清楚为什么我们需要为每个要保存在mongoDB中的集合声明DTO、模式和接口。例如,我有一个收藏(不幸的是,我把它命名为
collection
,但这并不重要),这是我的DTO:

export class CollectionDto {
  readonly description: string;
  readonly name: string;
  readonly expiration: Date;
}
接口:

import { Document } from 'mongoose';

export interface Collection extends Document {
  readonly description: string;
  readonly name: string;
  readonly expiration: Date;
}
和模式:

import * as mongoose from 'mongoose';

export const CollectionSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  description: {
    type: String,
    required: false,
  },
  expiration: {
    type: String,
    required: true,
  }
});

我的疑问是,我们真的需要三个几乎内容相同的物体吗?乍一看很奇怪

您不能真正将其视为一个真实对象,而是将其视为数据库中的一个字段


虽然实际上它们都是封装在mongoose.Schema构造函数下的属性。

我在普通nodejs基础上使用了很多mongoose,并且我开始使用NestJS。Mongoose定义了两件事,以便您可以使用mongodb创建、查询、更新和删除文档:模式和模型。您已经有了模式,对于普通猫鼬中的模型,应该是:

import * as mongoose from 'mongoose';

export const CollectionSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  description: {
    type: String,
    required: false,
  },
  expiration: {
    type: String,
    required: true,
  }
});
const Collection = mongoose.model('collections', CollectionSchema);
这里收集的是猫鼬模型。到目前为止还不错

在NestJs中,如果要遵循API最佳实践,将使用DTO(数据传输对象)。文档中的NestJs提到使用类比使用接口更可取,所以这里不需要接口。定义Mongoose模式时,还可以定义模型/模式:

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type CollectionDocument = Collection & Document;

@Schema()
export class Collection {
  @Prop()
  name: string;

  @Prop()
  description: number;

  @Prop()
  expiration: string;
}

export const CollectionSchema = SchemaFactory.createForClass(Collection);
对于您的服务和控制器,您同时使用(型号和DTO):

从“猫鼬”导入{Model};
从'@nestjs/common'导入{Injectable};
从'@nestjs/mongoose'导入{InjectModel};
从“./schemas/Collection.schema”导入{Collection,CollectionDocument};
从“./dto/collection.dto”导入{CollectionDto};
@可注射()
导出类集合服务{
构造函数(@InjectModel(Collection.name)private collectionModel:Model){}
异步创建(createColDto:CollectionDto):承诺{
const createdCollection=newthis.collectionModel(CollectionDto);
返回createdCollection.save();
}
异步findAll():承诺{
返回此.collectionModel.find().exec();
}
}
在此之后,您可以使用户大摇大摆地查看API的自动文档。

import { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Collection, CollectionDocument } from './schemas/collection.schema';
import { CollectionDto } from './dto/collection.dto';

@Injectable()
export class CollectionService {
  constructor(@InjectModel(Collection.name) private collectionModel: Model<CollectionDocument>) {}

  async create(createColDto: CollectionDto): Promise<Collection> {
    const createdCollection = new this.collectionModel(CollectionDto);
    return createdCollection.save();
  }

  async findAll(): Promise<Collection[]> {
    return this.collectionModel.find().exec();
  }
}