43 lines
878 B
JavaScript
43 lines
878 B
JavaScript
const mongoose = require('mongoose');
|
|
const uniqueValidator = require('mongoose-unique-validator');
|
|
|
|
const schema = 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 },
|
|
);
|
|
|
|
schema.plugin(uniqueValidator, { message: 'is already taken' });
|
|
|
|
schema.pre('validate', function (next) {
|
|
if (!this.slug) {
|
|
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);
|
|
|
|
module.exports = TrackData;
|