Created
July 2, 2024 07:28
-
-
Save Gaurav8757/110fb877fae3d0b898e2b38ce580e3e1 to your computer and use it in GitHub Desktop.
In MongoDB, added serial No.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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