2016-06-16 22:07:38 +02:00
|
|
|
import mongoose from 'mongoose';
|
|
|
|
const Schema = mongoose.Schema;
|
2016-07-07 19:04:54 +02:00
|
|
|
const ObjectIdSchema = Schema.ObjectId;
|
|
|
|
const ObjectId = mongoose.Types.ObjectId;
|
2016-06-17 22:40:13 +02:00
|
|
|
import shortid from 'shortid';
|
2016-06-16 22:07:38 +02:00
|
|
|
|
2016-07-06 23:29:07 +02:00
|
|
|
const defaultSketch = `function setup() {
|
2016-06-29 18:52:16 +02:00
|
|
|
createCanvas(400, 400);
|
|
|
|
}
|
2016-07-06 23:29:07 +02:00
|
|
|
function draw() {
|
2016-06-29 18:52:16 +02:00
|
|
|
background(220);
|
|
|
|
}`
|
|
|
|
|
2016-07-06 21:09:05 +02:00
|
|
|
const defaultHTML =
|
|
|
|
`
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2016-07-11 21:22:29 +02:00
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.0/p5.min.js"></script>
|
2016-07-06 21:09:05 +02:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<script src="sketch.js"></script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
2016-06-17 20:11:52 +02:00
|
|
|
const fileSchema = new Schema({
|
2016-06-24 00:29:55 +02:00
|
|
|
name: { type: String, default: 'sketch.js' },
|
2016-06-29 18:52:16 +02:00
|
|
|
content: { type: String, default: defaultSketch }
|
2016-07-07 19:04:54 +02:00
|
|
|
}, { timestamps: true, _id: true });
|
2016-06-17 20:11:52 +02:00
|
|
|
|
2016-07-07 19:50:52 +02:00
|
|
|
fileSchema.virtual('id').get(function(){
|
|
|
|
return this._id.toHexString();
|
|
|
|
});
|
|
|
|
|
|
|
|
fileSchema.set('toJSON', {
|
|
|
|
virtuals: true
|
|
|
|
});
|
|
|
|
|
2016-06-16 22:07:38 +02:00
|
|
|
const projectSchema = new Schema({
|
2016-06-24 00:29:55 +02:00
|
|
|
name: { type: String, default: "Hello p5.js, it's the server" },
|
|
|
|
user: { type: Schema.Types.ObjectId, ref: 'User' },
|
2016-07-07 19:04:54 +02:00
|
|
|
files: {type: [ fileSchema ], default: [{ name: 'sketch.js', content: defaultSketch, _id: new ObjectId() }, { name: 'index.html', content: defaultHTML, _id: new ObjectId() }]},
|
2016-07-08 20:57:22 +02:00
|
|
|
_id: { type: String, default: shortid.generate },
|
|
|
|
selectedFile: Schema.Types.ObjectId
|
2016-06-24 00:29:55 +02:00
|
|
|
}, { timestamps: true });
|
2016-06-16 22:07:38 +02:00
|
|
|
|
2016-07-07 19:50:52 +02:00
|
|
|
projectSchema.virtual('id').get(function(){
|
|
|
|
return this._id;
|
|
|
|
});
|
|
|
|
|
|
|
|
projectSchema.set('toJSON', {
|
|
|
|
virtuals: true
|
|
|
|
});
|
|
|
|
|
2016-07-08 20:57:22 +02:00
|
|
|
projectSchema.pre('save', function createSelectedFile(next) {
|
|
|
|
const project = this;
|
|
|
|
if (!project.selectedFile) {
|
|
|
|
project.selectedFile = project.files[0]._id; // eslint-disable-line no-underscore-dangle
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export default mongoose.model('Project', projectSchema);
|