p5.js-web-editor/server/models/project.js
Andrew Nicolaou d2cad7438e
Test create project server code (prep for #1095)
* upgrades jest to latest version

* tests Project model with mockingoose

* extracts createProject controller to own file

* tests createProject controller with sinon-mongoose
2019-06-12 11:27:28 +02:00

56 lines
1.4 KiB
JavaScript

import mongoose from 'mongoose';
import shortid from 'shortid';
import slugify from 'slugify';
// Register User model as it's referenced by Project
import './user';
const { Schema } = mongoose;
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 }
);
fileSchema.virtual('id').get(function getFileId() {
return this._id.toHexString();
});
fileSchema.set('toJSON', {
virtuals: true
});
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) {
const project = this;
project.slug = slugify(project.name, '_');
return next();
});
export default mongoose.model('Project', projectSchema);