admin管理员组文章数量:1023815
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 badges1 Answer
Reset to default 7You 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.
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 badges1 Answer
Reset to default 7You 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.
本文标签: javascriptNestjs MongoDb SchemaInterface Information DuplicationStack Overflow
版权声明:本文标题:javascript - Nestjs MongoDb SchemaInterface Information Duplication - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745518184a2154181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论