added auto increment to tracks

parent 7968bb03
This diff is collapsed.
......@@ -23,6 +23,7 @@
"joi": "^17.9.1",
"jsonwebtoken": "^9.0.0",
"mongoose": "^7.0.1",
"mongoose-sequence": "^5.3.1",
"multer": "^1.4.5-lts.1",
"ts-node-dev": "^2.0.0"
},
......@@ -31,6 +32,7 @@
"@types/express": "^4.17.17",
"@types/jsonwebtoken": "^9.0.1",
"@types/mongoose": "^5.11.97",
"@types/mongoose-sequence": "^3.0.7",
"@types/multer": "^1.4.7"
}
}
......@@ -4,4 +4,5 @@ export default interface ITrack {
name: string;
album: ObjectId;
duration: string;
seq?: Number;
}
import {model, Schema} from 'mongoose';
import mongoose, {model, Schema} from 'mongoose';
import ITrack from '../interfaces/ITrack';
const TrackSchema = new Schema<ITrack>(
......@@ -19,9 +19,18 @@ const TrackSchema = new Schema<ITrack>(
required: [true, 'Duration is required'],
trim: true,
},
seq: {
type: Number,
},
},
{versionKey: false}
);
TrackSchema.pre('save', {document: true, query: false}, async function () {
const count = await TrackModel.count();
this.seq = count + 1;
});
const TrackModel = model<ITrack>('track', TrackSchema);
export default TrackModel;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment