|
| 1 | +import connector from "#models/databaseUtil"; |
| 2 | + |
| 3 | +const studentSchema = { |
| 4 | + ERPID: { type: String, required: true }, |
| 5 | + name: { type: String, required: true }, |
| 6 | + joiningYear: { type: Number, required: true }, |
| 7 | + branch: { type: connector.Schema.Types.ObjectId, ref: "Department", required: true }, |
| 8 | + division: { type: String, enum: ["A", "B", "C"], default: "A" }, |
| 9 | + rollNo: { type: Number, required: true }, |
| 10 | + coursesOpted: [{ type: connector.Schema.Types.ObjectId, ref: "Course", required: true }], |
| 11 | +}; |
| 12 | + |
| 13 | +// eslint-disable-next-line no-unused-vars |
| 14 | + |
| 15 | +const Student = connector.model("Student", studentSchema); |
| 16 | + |
| 17 | +// CRUD OPERATIONS |
| 18 | + |
| 19 | +async function remove(filter) { |
| 20 | + const deleteResult = await Student.deleteMany(filter); |
| 21 | + return deleteResult.acknowledged; |
| 22 | +} |
| 23 | + |
| 24 | +async function create(studentData) { |
| 25 | + const { |
| 26 | + ERPID, name, joiningYear, branch, division, rollNo, coursesOpted, |
| 27 | + } = studentData; |
| 28 | + const student = new Student({ |
| 29 | + ERPID, |
| 30 | + name, |
| 31 | + joiningYear, |
| 32 | + branch, |
| 33 | + division, |
| 34 | + rollNo, |
| 35 | + coursesOpted, |
| 36 | + }); |
| 37 | + const studentDoc = await student.save(); |
| 38 | + return studentDoc; |
| 39 | +} |
| 40 | + |
| 41 | +async function read(filter, limit = 1) { |
| 42 | + const studentDoc = await Student.find(filter).limit(limit); |
| 43 | + return studentDoc; |
| 44 | +} |
| 45 | + |
| 46 | +async function update(filter, updateObject, options = { multi: true }) { |
| 47 | + const updateResult = await Student.updateMany(filter, { $set: updateObject }, options); |
| 48 | + return updateResult.acknowledged; |
| 49 | +} |
| 50 | + |
| 51 | +export default { |
| 52 | + create, read, update, remove, |
| 53 | +}; |
0 commit comments