javascript - Nestjs MongoDb SchemaInterface Information Duplication - Stack Overflow

I have been following the instructions for using MongoDb with Nestjs. I've got things working howe

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745518185a4631126.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信