added auto increment to tracks

parent 7968bb03
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
"joi": "^17.9.1", "joi": "^17.9.1",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"mongoose": "^7.0.1", "mongoose": "^7.0.1",
"mongoose-sequence": "^5.3.1",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"ts-node-dev": "^2.0.0" "ts-node-dev": "^2.0.0"
}, },
...@@ -31,6 +32,7 @@ ...@@ -31,6 +32,7 @@
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
"@types/jsonwebtoken": "^9.0.1", "@types/jsonwebtoken": "^9.0.1",
"@types/mongoose": "^5.11.97", "@types/mongoose": "^5.11.97",
"@types/mongoose-sequence": "^3.0.7",
"@types/multer": "^1.4.7" "@types/multer": "^1.4.7"
} }
} }
...@@ -4,4 +4,5 @@ export default interface ITrack { ...@@ -4,4 +4,5 @@ export default interface ITrack {
name: string; name: string;
album: ObjectId; album: ObjectId;
duration: string; duration: string;
seq?: Number;
} }
import {model, Schema} from 'mongoose'; import mongoose, {model, Schema} from 'mongoose';
import ITrack from '../interfaces/ITrack'; import ITrack from '../interfaces/ITrack';
const TrackSchema = new Schema<ITrack>( const TrackSchema = new Schema<ITrack>(
...@@ -19,9 +19,18 @@ const TrackSchema = new Schema<ITrack>( ...@@ -19,9 +19,18 @@ const TrackSchema = new Schema<ITrack>(
required: [true, 'Duration is required'], required: [true, 'Duration is required'],
trim: true, trim: true,
}, },
seq: {
type: Number,
},
}, },
{versionKey: false} {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); const TrackModel = model<ITrack>('track', TrackSchema);
export default TrackModel; 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