英文:
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 '@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.
答案1
得分: 1
在这种情况下,您可以使用继承。
将以下内容更改为:
export class Mark extends Common
希望这有所帮助
英文:
In this case, you can use inheritance.
change
export class Mark extends AbstractDocument
to
export class Mark extends Common
Hope this helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论