52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
var mongoose = require('mongoose');
|
|
var uniqueValidator = require('mongoose-unique-validator');
|
|
var slug = require('slug');
|
|
|
|
var JumpFileSchema = new mongoose.Schema({
|
|
slug: { type: String, lowercase: true, unique: true },
|
|
name: String,
|
|
path: String,
|
|
type: {
|
|
type: String,
|
|
enum : ['csv', 'image', 'kml', 'video'],
|
|
default: 'video'
|
|
},
|
|
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
|
|
}, {timestamps: true, toJSON: {virtuals: true}});
|
|
|
|
JumpFileSchema.plugin(uniqueValidator, { message: 'is already taken' });
|
|
|
|
JumpFileSchema.pre('validate', function (next) {
|
|
if (!this.slug) {
|
|
this.slugify();
|
|
}
|
|
next();
|
|
});
|
|
|
|
JumpFileSchema.methods.slugify = function () {
|
|
this.slug = slug(`${this.type}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36) + (Math.random() * Math.pow(36, 6) | 0).toString(36);
|
|
};
|
|
|
|
JumpFileSchema.methods.toJSONFor = function(user){
|
|
return {
|
|
slug: this.slug,
|
|
name: this.name,
|
|
path: this.path,
|
|
type: this.type,
|
|
createdAt: this.createdAt,
|
|
updatedAt: this.updatedAt,
|
|
author: this.author.toProfileJSONFor(user)
|
|
};
|
|
};
|
|
|
|
JumpFileSchema.methods.toJSONForJump = function(){
|
|
return {
|
|
slug: this.slug,
|
|
name: this.name,
|
|
path: this.path,
|
|
type: this.type
|
|
};
|
|
};
|
|
|
|
mongoose.model('JumpFile', JumpFileSchema);
|