2016-06-16 20:07:38 +00:00
|
|
|
import mongoose from 'mongoose';
|
2016-06-17 20:40:13 +00:00
|
|
|
import shortid from 'shortid';
|
2018-02-07 21:00:09 +00:00
|
|
|
import slugify from 'slugify';
|
2016-06-16 20:07:38 +00:00
|
|
|
|
2018-05-05 00:59:43 +00:00
|
|
|
const { Schema } = mongoose;
|
2017-02-22 19:29:35 +00:00
|
|
|
|
2016-06-17 18:11:52 +00:00
|
|
|
const fileSchema = new Schema({
|
2016-06-23 22:29:55 +00:00
|
|
|
name: { type: String, default: 'sketch.js' },
|
2016-08-24 21:59:15 +00:00
|
|
|
content: { type: String, default: '' },
|
2016-08-23 04:10:42 +00:00
|
|
|
url: { type: String },
|
2016-11-17 16:15:35 +00:00
|
|
|
children: { type: [String], default: [] },
|
2016-08-25 21:32:27 +00:00
|
|
|
fileType: { type: String, default: 'file' },
|
2016-09-14 19:57:52 +00:00
|
|
|
isSelectedFile: { type: Boolean }
|
2016-07-07 17:04:54 +00:00
|
|
|
}, { timestamps: true, _id: true });
|
2016-06-17 18:11:52 +00:00
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
fileSchema.virtual('id').get(function getFileId() {
|
2016-11-17 16:15:35 +00:00
|
|
|
return this._id.toHexString();
|
2016-07-07 17:50:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
fileSchema.set('toJSON', {
|
2016-11-17 16:15:35 +00:00
|
|
|
virtuals: true
|
2016-07-07 17:50:52 +00:00
|
|
|
});
|
|
|
|
|
2016-06-16 20:07:38 +00:00
|
|
|
const projectSchema = new Schema({
|
2016-06-23 22:29:55 +00:00
|
|
|
name: { type: String, default: "Hello p5.js, it's the server" },
|
|
|
|
user: { type: Schema.Types.ObjectId, ref: 'User' },
|
2017-05-03 15:46:12 +00:00
|
|
|
serveSecure: { type: Boolean, default: false },
|
2016-11-17 16:15:35 +00:00
|
|
|
files: { type: [fileSchema] },
|
2018-02-07 21:00:09 +00:00
|
|
|
_id: { type: String, default: shortid.generate },
|
|
|
|
slug: { type: String }
|
2016-06-23 22:29:55 +00:00
|
|
|
}, { timestamps: true });
|
2016-06-16 20:07:38 +00:00
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
projectSchema.virtual('id').get(function getProjectId() {
|
2016-11-17 16:15:35 +00:00
|
|
|
return this._id;
|
2016-07-07 17:50:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
projectSchema.set('toJSON', {
|
2016-11-17 16:15:35 +00:00
|
|
|
virtuals: true
|
2016-07-07 17:50:52 +00:00
|
|
|
});
|
|
|
|
|
2018-02-07 21:00:09 +00:00
|
|
|
projectSchema.pre('save', function generateSlug(next) {
|
|
|
|
const project = this;
|
|
|
|
project.slug = slugify(project.name, '_');
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
export default mongoose.model('Project', projectSchema);
|