obs-portal/models/TrackData.js

40 lines
910 B
JavaScript
Raw Normal View History

2020-11-20 10:02:30 +00:00
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const slug = require('slug');
2020-08-14 10:43:57 +00:00
2020-11-20 10:02:30 +00:00
const TrackDataSchema = new mongoose.Schema(
{
slug: { type: String, lowercase: true, unique: true },
points: [
{
date: String,
time: String,
latitude: Number,
longitude: Number,
course: Number,
speed: Number,
d1: Number,
d2: Number,
flag: Number,
private: Number,
},
],
},
{ timestamps: true },
);
2020-08-14 10:43:57 +00:00
2020-11-20 10:02:30 +00:00
TrackDataSchema.plugin(uniqueValidator, { message: 'is already taken' });
2020-08-14 20:15:12 +00:00
2020-11-20 10:02:30 +00:00
TrackDataSchema.pre('validate', function (next) {
if (!this.slug) {
2020-08-14 20:15:12 +00:00
this.slugify();
}
next();
});
2020-11-20 10:02:30 +00:00
TrackDataSchema.methods.slugify = function () {
this.slug = slug('td') + '-' + ((Math.random() * Math.pow(36, 6)) | 0).toString(36);
2020-08-14 20:15:12 +00:00
};
2020-08-14 10:43:57 +00:00
mongoose.model('TrackData', TrackDataSchema);