p5.js-web-editor/server/models/project.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

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';
2018-05-05 00:59:43 +00:00
const { Schema } = mongoose;
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 },
children: { type: [String], default: [] },
fileType: { type: String, default: 'file' },
isSelectedFile: { type: Boolean }
2018-08-31 01:00:29 +00:00
}, { timestamps: true, _id: true, usePushEach: true });
2016-06-17 18:11:52 +00:00
fileSchema.virtual('id').get(function getFileId() {
return this._id.toHexString();
});
fileSchema.set('toJSON', {
virtuals: true
});
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' },
serveSecure: { type: Boolean, default: false },
files: { type: [fileSchema] },
2018-02-07 21:00:09 +00:00
_id: { type: String, default: shortid.generate },
slug: { type: String }
2018-08-31 01:00:29 +00:00
}, { timestamps: true, usePushEach: true });
projectSchema.virtual('id').get(function getProjectId() {
return this._id;
});
projectSchema.set('toJSON', {
virtuals: true
});
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);