Skip to content

Instantly share code, notes, and snippets.

@Gaurav8757
Created July 2, 2024 07:28
Show Gist options
  • Save Gaurav8757/110fb877fae3d0b898e2b38ce580e3e1 to your computer and use it in GitHub Desktop.
Save Gaurav8757/110fb877fae3d0b898e2b38ce580e3e1 to your computer and use it in GitHub Desktop.
In MongoDB, added serial No.
import mongoose from "mongoose";
const { Schema } = mongoose;
const claimSchema = new Schema({
sNo: { type: Number, unique: true },
date: { type: Date },
companyName: { type: String},
claimType: { type: String },
policyNo: { type: String },
insuredName: { type: String },
contactNo: { type: Number},
vehicleRegNo: { type: String },
vehicleType: { type: String },
policyExpiryDate: { type: Date },
intimationDate: { type: Date},
claimNo: { type: String},
advisor:{ type: String},
branch: { type: String },
claimStatus: { type: String},
claimAmount: { type: Number },
surveyorName: { type: String },
surveyorContactNo: { type: Number},
remarks: { type: String },
});
// Pre-save hook to auto increment sNo
claimSchema.pre('save', async function (next) {
if (this.isNew) {
const latestClaim = await mongoose.model('ClaimForm').findOne().sort({ sNo: -1 });
this.sNo = latestClaim ? latestClaim.sNo + 1 : 1;
}
next();
});
const ClaimForm = mongoose.model("ClaimForm", claimSchema);
export default ClaimForm;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment