2016-06-16 20:07:38 +00:00
|
|
|
import mongoose from 'mongoose';
|
2016-06-17 20:40:13 +00:00
|
|
|
import shortid from 'shortid';
|
2019-02-10 01:27:03 +00:00
|
|
|
import slugify from 'slugify';
|
2016-06-16 20:07:38 +00:00
|
|
|
|
2019-06-12 09:27:28 +00:00
|
|
|
// Register User model as it's referenced by Project
|
|
|
|
import './user';
|
|
|
|
|
2018-05-05 00:59:43 +00:00
|
|
|
const { Schema } = mongoose;
|
2017-02-22 19:29:35 +00:00
|
|
|
|
2018-12-15 07:13:58 +00:00
|
|
|
const fileSchema = new Schema(
|
|
|
|
{
|
|
|
|
name: { type: String, default: 'sketch.js' },
|
|
|
|
content: { type: String, default: '' },
|
|
|
|
url: { type: String },
|
|
|
|
children: { type: [String], default: [] },
|
|
|
|
fileType: { type: String, default: 'file' },
|
|
|
|
isSelectedFile: { type: Boolean }
|
|
|
|
},
|
|
|
|
{ timestamps: true, _id: true, usePushEach: 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
|
|
|
});
|
|
|
|
|
2018-12-15 07:13:58 +00:00
|
|
|
const projectSchema = new Schema(
|
|
|
|
{
|
|
|
|
name: { type: String, default: "Hello p5.js, it's the server" },
|
|
|
|
user: { type: Schema.Types.ObjectId, ref: 'User' },
|
|
|
|
serveSecure: { type: Boolean, default: false },
|
|
|
|
files: { type: [fileSchema] },
|
|
|
|
_id: { type: String, default: shortid.generate },
|
|
|
|
slug: { type: String }
|
|
|
|
},
|
|
|
|
{ timestamps: true, usePushEach: 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
|
|
|
});
|
|
|
|
|
2019-02-10 01:27:03 +00:00
|
|
|
projectSchema.pre('save', function generateSlug(next) {
|
2018-02-07 21:00:09 +00:00
|
|
|
const project = this;
|
2019-02-10 01:27:03 +00:00
|
|
|
project.slug = slugify(project.name, '_');
|
2018-02-07 21:00:09 +00:00
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
export default mongoose.model('Project', projectSchema);
|