在Nest.js中从另一个模式导入模式。

huangapple go评论91阅读模式
英文:

import schema from another schema in nest.js

问题

common.schema.ts:

  1. import { AbstractDocument, ObjectIdType } from '@app/common';
  2. import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
  3. import { SchemaTypes } from 'mongoose';
  4. @Schema({
  5. timestamps: true,
  6. versionKey: false,
  7. })
  8. export class Common extends AbstractDocument {
  9. @Prop({ type: SchemaTypes.ObjectId })
  10. studentId: ObjectIdType;
  11. @Prop()
  12. studentName: string;
  13. @Prop()
  14. studentNumber: string;
  15. @Prop()
  16. grade: string;
  17. }
  18. export const CommonSchema = SchemaFactory.createForClass(Common);

student.schema.ts:

  1. import { AbstractDocument, ObjectIdType } from '@app/common';
  2. import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
  3. import { SchemaTypes } from 'mongoose';
  4. @Schema({
  5. timestamps: true,
  6. versionKey: false,
  7. })
  8. export class Mark extends AbstractDocument {
  9. @Prop({ type: SchemaTypes.ObjectId })
  10. subjectId: ObjectIdType;
  11. @Prop()
  12. subjectName: string;
  13. @Prop()
  14. totalMarks: string;
  15. }
  16. export const MarkSchema = SchemaFactory.createForClass(Mark);

student.module.ts:

  1. import { Student, StudentSchema } from './student.schema';
  2. import { Common, CommonSchema } from '../common.schema';
  3. @Module({
  4. imports: [
  5. MongooseModule.forFeature([
  6. {
  7. name: Student.name,
  8. schema: StudentSchema,
  9. },
  10. {
  11. name: Common.name,
  12. schema: CommonSchema,
  13. },
  14. ])
  15. ],
  16. controllers: [StudentController],
  17. providers: [StudentService, StudentRepository],
  18. })
  19. export class StudentModule {}

You can insert data in Nest.js using Mongoose like this:

  1. import { Model } from 'mongoose';
  2. import { Injectable } from '@nestjs/common';
  3. import { InjectModel } from '@nestjs/mongoose';
  4. import { Common } from '../common.schema';
  5. import { Mark } from './student.schema';
  6. @Injectable()
  7. export class StudentService {
  8. constructor(
  9. @InjectModel(Common.name) private commonModel: Model<Common>,
  10. @InjectModel(Mark.name) private markModel: Model<Mark>,
  11. ) {}
  12. async createStudent(data: any) {
  13. // Insert data into the Common schema
  14. const common = await this.commonModel.create(data);
  15. // Insert data into the Mark schema
  16. const mark = await this.markModel.create(data);
  17. // Return the final data
  18. return {
  19. studentId: common.studentId,
  20. studentName: common.studentName,
  21. studentNumber: common.studentNumber,
  22. grade: common.grade,
  23. subjectId: mark.subjectId,
  24. subjectName: mark.subjectName,
  25. totalMarks: mark.totalMarks,
  26. };
  27. }
  28. }

This code assumes that you have a service called StudentService where you can create a new student record by inserting data into both the Common and Mark schemas, and then return the final data as shown in your example.

英文:

I am working with Nest.js and MongoDB. There is a common schema defined.

common.schema.ts:

  1. import { AbstractDocument, ObjectIdType } from &#39;@app/common&#39;;
  2. import { Prop, Schema, SchemaFactory } from &#39;@nestjs/mongoose&#39;;
  3. import { SchemaTypes } from &#39;mongoose&#39;;
  4. @Schema({
  5. timestamps: true,
  6. versionKey: false,
  7. })
  8. export class Common extends AbstractDocument {
  9. @Prop({ type: SchemaTypes.ObjectId })
  10. studentId: ObjectIdType;
  11. @Prop()
  12. studentName: string;
  13. @Prop()
  14. studentNumber: string;
  15. @Prop()
  16. grade: string;
  17. }
  18. export const CommonSchema = SchemaFactory.createForClass(Common);

Need to use the above schema values in the below schema:

student.schema.ts:

  1. import { AbstractDocument, ObjectIdType } from &#39;@app/common&#39;;
  2. import { Prop, Schema, SchemaFactory } from &#39;@nestjs/mongoose&#39;;
  3. import { SchemaTypes } from &#39;mongoose&#39;;
  4. @Schema({
  5. timestamps: true,
  6. versionKey: false,
  7. })
  8. export class Mark extends AbstractDocument {
  9. @Prop({ type: SchemaTypes.ObjectId })
  10. subjectId: ObjectIdType;
  11. @Prop()
  12. subjectName: string;
  13. @Prop()
  14. totalMarks: string;
  15. }
  16. export const MarkSchema = SchemaFactory.createForClass(Mark);

The common schema is included in student module.

student.module.ts:

  1. import { Student, StudentSchema } from &#39;./student.schema&#39;;
  2. import { Common, CommonSchema } from &#39;../common.schema&#39;;
  3. @Module({
  4. imports: [
  5. MongooseModule.forFeature([
  6. {
  7. name: Student.name,
  8. schema: StudentSchema,
  9. },
  10. {
  11. name: Common.name,
  12. schema: CommonSchema,
  13. },
  14. ])
  15. ],
  16. controllers: [StudentController],
  17. providers: [StudentService, StudentRepository],
  18. })
  19. export class StudentModule {}

After this I don't know what to do. I need the final data to be inserted as like below:

  1. {
  2. _id: ObjectId(&#39;643f8a6773a92fb1552e4c89&#39;),
  3. studentId: ObjectId(&#39;643f8a6773a92fb1552e4c90&#39;),
  4. studentName: &#39;xxxyyy&#39;,
  5. studentNumber: 12345,
  6. grade: 2,
  7. subjectId: ObjectId(&#39;643f8df1885133b26167166c&#39;),
  8. subjectName: &#39;English&#39;,
  9. totalMarks: 55
  10. }

How to do that in Nest.js?

Need some valuable help.

答案1

得分: 1

在这种情况下,您可以使用继承。
将以下内容更改为:

  1. export class Mark extends Common

希望这有所帮助 在Nest.js中从另一个模式导入模式。

英文:

In this case, you can use inheritance.
change

  1. export class Mark extends AbstractDocument

to

  1. export class Mark extends Common

Hope this helps 在Nest.js中从另一个模式导入模式。

huangapple
  • 本文由 发表于 2023年6月13日 17:47:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定