I’m relatively new to using Nest.js and I’m encountering an issue with implementing cascade delete functionality through pre middleware. To give you a clear example, I have two models: one related to enrollments and another related to students.
The scenario I’m dealing with is that when a student is deleted, I’d like to cascade delete all the enrollments associated with that particular student.
Student Schema:
export type StudentSchema = HydratedDocument<iStudent>;
@Schema({
toJSON: {
virtuals: true,
transform: function (doc: any, ret: any) {
delete ret._id;
delete ret.__v;
return ret;
},
},
})
export class Student {
@Prop()
firstName: string;
@Prop()
lastName: string;
@Prop()
birthDate: Date;
@Prop()
isCustomer: boolean;
@Prop()
email: string;
@Prop()
phone: string;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: "Tutor" })
tutor: Tutor;
}
const StudentSchema = SchemaFactory.createForClass(Student);
StudentSchema.pre(["find", "findOne"], function () {
this.populate("tutor");
});
StudentSchema.virtual('fullname').get(function() {
return `${this.firstName} ${this.lastName}`
});
export { StudentSchema };
In this schema, I have defined a Student class with various properties, and I’ve set up virtuals and pre middleware for population.
Enrollment Schema
export type EnrollmentSchema = HydratedDocument<iEnrollment>;
@Schema({
toJSON: {
virtuals: true,
transform: function (doc: any, ret: any) {
delete ret._id;
delete ret.__v;
return ret;
},
},
})
export class Enrollment {
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: "Student" })
student: Student;
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: "Course" })
course: Course;
@Prop()
createdAt: Date;
@Prop()
status: string;
}
const EnrollmentSchema =
SchemaFactory.createForClass(Enrollment);
EnrollmentSchema.pre(["find", "findOne"], function () {
this.populate('student course');
});
export { EnrollmentSchema };
In this schema, I have an Enrollment class with properties related to students, courses, and other data. I’ve also configured virtuals and pre middleware for population.
My goal is to automatically delete all the associated enrollments when a student is deleted. I’d appreciate any guidance on how to implement this cascade delete functionality using Nest.js.
Thank you in advance for your assistance! 😊
2
Answers
You can cascade delete all the enrollments associated with that particular student like this example
You can use this
post
hook which will trigger whenever you useStudent.findByIdAndDelete(id)
andStudent.findOneAndDelete({key:'value'})