obs-portal/models/TrackData.js

44 lines
908 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
const schema = new mongoose.Schema(
2020-11-20 10:02:30 +00:00
{
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
schema.plugin(uniqueValidator, { message: 'is already taken' });
2020-08-14 20:15:12 +00:00
schema.pre('validate', function (next) {
2020-11-20 10:02:30 +00:00
if (!this.slug) {
2020-08-14 20:15:12 +00:00
this.slugify();
}
next();
});
class TrackData extends mongoose.Model {
slugify() {
this.slug = 'td-' + String((Math.random() * Math.pow(36, 6)) | 0).toString(36);
}
}
mongoose.model(TrackData, schema);
2020-08-14 20:15:12 +00:00
module.exports = TrackData;