最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nestjs MongoDb SchemaInterface Information Duplication - Stack Overflow

programmeradmin3浏览0评论

I have been following the instructions for using MongoDb with Nestjs. I've got things working however it seems to me there is a rather unnecessary duplication of information (not DRY). Specifically it seems that we are required to make Db schema and also interfaces that match the schema. In my own code this looks something like this:

import { Document, Schema } from 'mongoose';

export interface IBlogPost extends Document {
  subject: string;
  body: string;
  authorId: string;
}

export const BlogPostSchema = new Schema({
  subject: String,
  body: String,
  authorId: String,
});

The rest of my code is in this repo if you want more context. The official example code is here.

Am I doing something wrong or is this really required?

I have been following the instructions for using MongoDb with Nestjs. I've got things working however it seems to me there is a rather unnecessary duplication of information (not DRY). Specifically it seems that we are required to make Db schema and also interfaces that match the schema. In my own code this looks something like this:

import { Document, Schema } from 'mongoose';

export interface IBlogPost extends Document {
  subject: string;
  body: string;
  authorId: string;
}

export const BlogPostSchema = new Schema({
  subject: String,
  body: String,
  authorId: String,
});

The rest of my code is in this repo if you want more context. The official example code is here.

Am I doing something wrong or is this really required?

Share Improve this question edited Feb 17, 2019 at 19:16 Kim Kern 60.7k20 gold badges219 silver badges214 bronze badges asked Feb 17, 2019 at 10:23 peter554peter554 1,3781 gold badge12 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can check out the nest.js typegoose library. The library creates the schema definition from an annotated typescript class.

export class Cat extends Typegoose {
  @prop({ required: true })
  name: string;
}

Alternatively you can use typeorm with mongodb, which only needs one annotated typescript interface as well.

发布评论

评论列表(0)

  1. 暂无评论