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

57 lines
1.4 KiB
JavaScript
Raw Normal View History

import mongoose from 'mongoose';
2016-06-17 22:40:13 +02:00
import shortid from 'shortid';
import slugify from 'slugify';
// Register User model as it's referenced by Project
import './user';
2018-05-05 02:59:43 +02:00
const { Schema } = mongoose;
2018-12-15 08:13:58 +01: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 20:11:52 +02:00
fileSchema.virtual('id').get(function getFileId() {
return this._id.toHexString();
});
fileSchema.set('toJSON', {
virtuals: true
});
2018-12-15 08:13:58 +01: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 }
);
projectSchema.virtual('id').get(function getProjectId() {
return this._id;
});
projectSchema.set('toJSON', {
virtuals: true
});
projectSchema.pre('save', function generateSlug(next) {
2018-02-07 22:00:09 +01:00
const project = this;
project.slug = slugify(project.name, '_');
2018-02-07 22:00:09 +01:00
return next();
});
2016-06-24 00:29:55 +02:00
export default mongoose.model('Project', projectSchema);