diff --git a/server/models/project.js b/server/models/project.js index 23b93796..742c9667 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -4,14 +4,17 @@ import generateSlug from '../utils/generateSlug'; 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 }); +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(); @@ -21,14 +24,17 @@ 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 }); +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; @@ -38,7 +44,7 @@ projectSchema.set('toJSON', { virtuals: true }); -projectSchema.pre('save', function generateSlug(next) { +projectSchema.pre('save', function slugGenerator(next) { const project = this; project.slug = generateSlug(project.name, '_'); return next(); diff --git a/server/utils/generateSlug.js b/server/utils/generateSlug.js index 0e67b8a2..19177e17 100644 --- a/server/utils/generateSlug.js +++ b/server/utils/generateSlug.js @@ -5,7 +5,7 @@ * @param {String} string * @param {String} replacer (optional) character to replace invalid characters */ -export function generateSlug(string, replacer) { +function generateSlug(string, replacer) { // from here https://serverfault.com/a/242134 const INVALID_CHARS_REGEX = /[*/?:\\<>|"\u0000-\u001F]/g; const slug = string.replace(INVALID_CHARS_REGEX, replacer || '');