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

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

import schema from another schema in nest.js

问题

common.schema.ts:

import { AbstractDocument, ObjectIdType } from '@app/common';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { SchemaTypes } from 'mongoose';

@Schema({
  timestamps: true,
  versionKey: false,
})
export class Common extends AbstractDocument {
  @Prop({ type: SchemaTypes.ObjectId })
  studentId: ObjectIdType;

  @Prop()
  studentName: string; 

  @Prop()
  studentNumber: string; 

  @Prop()
  grade: string; 
}
export const CommonSchema = SchemaFactory.createForClass(Common);

student.schema.ts:

import { AbstractDocument, ObjectIdType } from '@app/common';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { SchemaTypes } from 'mongoose';

@Schema({
  timestamps: true,
  versionKey: false,
})
export class Mark extends AbstractDocument {
  @Prop({ type: SchemaTypes.ObjectId })
  subjectId: ObjectIdType;

  @Prop()
  subjectName: string; 

  @Prop()
  totalMarks: string; 
}

export const MarkSchema = SchemaFactory.createForClass(Mark);

student.module.ts:

import { Student, StudentSchema } from './student.schema';
import { Common, CommonSchema } from '../common.schema';

@Module({
  imports: [
    MongooseModule.forFeature([
      {
        name: Student.name,
        schema: StudentSchema,
      },
      {
        name: Common.name,
        schema: CommonSchema,
      },
    ])
  ],
  controllers: [StudentController],
  providers: [StudentService, StudentRepository],
})
export class StudentModule {}

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

import { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Common } from '../common.schema';
import { Mark } from './student.schema';

@Injectable()
export class StudentService {
  constructor(
    @InjectModel(Common.name) private commonModel: Model<Common>,
    @InjectModel(Mark.name) private markModel: Model<Mark>,
  ) {}

  async createStudent(data: any) {
    // Insert data into the Common schema
    const common = await this.commonModel.create(data);

    // Insert data into the Mark schema
    const mark = await this.markModel.create(data);

    // Return the final data
    return {
      studentId: common.studentId,
      studentName: common.studentName,
      studentNumber: common.studentNumber,
      grade: common.grade,
      subjectId: mark.subjectId,
      subjectName: mark.subjectName,
      totalMarks: mark.totalMarks,
    };
  }
}

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:

import { AbstractDocument, ObjectIdType } from &#39;@app/common&#39;;
import { Prop, Schema, SchemaFactory } from &#39;@nestjs/mongoose&#39;;
import { SchemaTypes } from &#39;mongoose&#39;;

@Schema({
  timestamps: true,
  versionKey: false,
})
export class Common extends AbstractDocument {
  @Prop({ type: SchemaTypes.ObjectId })
  studentId: ObjectIdType;

  @Prop()
  studentName: string; 

  @Prop()
  studentNumber: string; 

  @Prop()
  grade: string; 
}
export const CommonSchema = SchemaFactory.createForClass(Common);

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

student.schema.ts:

import { AbstractDocument, ObjectIdType } from &#39;@app/common&#39;;
import { Prop, Schema, SchemaFactory } from &#39;@nestjs/mongoose&#39;;
import { SchemaTypes } from &#39;mongoose&#39;;

@Schema({
  timestamps: true,
  versionKey: false,
})
export class Mark extends AbstractDocument {
  @Prop({ type: SchemaTypes.ObjectId })
  subjectId: ObjectIdType;

  @Prop()
  subjectName: string; 

  @Prop()
  totalMarks: string; 
}

export const MarkSchema = SchemaFactory.createForClass(Mark);

The common schema is included in student module.

student.module.ts:

import { Student, StudentSchema } from &#39;./student.schema&#39;;
import { Common, CommonSchema } from &#39;../common.schema&#39;;

@Module({
  imports: [
    MongooseModule.forFeature([
      {
        name: Student.name,
        schema: StudentSchema,
      },
      {
        name: Common.name,
        schema: CommonSchema,
      },
    ])
  ],
  controllers: [StudentController],
  providers: [StudentService, StudentRepository],
})
export class StudentModule {}

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

{
  _id: ObjectId(&#39;643f8a6773a92fb1552e4c89&#39;),
  studentId: ObjectId(&#39;643f8a6773a92fb1552e4c90&#39;),
  studentName: &#39;xxxyyy&#39;,
  studentNumber: 12345,
  grade: 2,
  subjectId: ObjectId(&#39;643f8df1885133b26167166c&#39;),
  subjectName: &#39;English&#39;,
  totalMarks: 55
}

How to do that in Nest.js?

Need some valuable help.

答案1

得分: 1

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

export class Mark extends Common

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

英文:

In this case, you can use inheritance.
change

export class Mark extends AbstractDocument

to

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:

确定