Stack Exchange
import schema from another schema in nest.js
I am working with Nest.js and MongoDB. There is a common schema defined.
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);

Need to use the above schema values in the below schema:
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);

The common schema is included in student module.
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 {}

After this I don't know what to do. I need the final data to be inserted as like below:
{
  _id: ObjectId('643f8a6773a92fb1552e4c89'),
  studentId: ObjectId('643f8a6773a92fb1552e4c90'),
  studentName: 'xxxyyy',
  studentNumber: 12345,
  grade: 2,
  subjectId: ObjectId('643f8df1885133b26167166c'),
  subjectName: 'English',
  totalMarks: 55
}

How to do that in Nest.js?
Need some valuable help.

In this case, you can use inheritance.
change
export class Mark extends AbstractDocument

to
export class Mark extends Common

Hope this helps :)
