2016-06-16 22:07:38 +02:00
|
|
|
import mongoose from 'mongoose';
|
2016-06-17 22:40:13 +02:00
|
|
|
import shortid from 'shortid';
|
2019-02-10 02:27:03 +01:00
|
|
|
import slugify from 'slugify';
|
2016-06-16 22:07:38 +02:00
|
|
|
|
2019-06-12 11:27:28 +02:00
|
|
|
// Register User model as it's referenced by Project
|
|
|
|
import './user';
|
|
|
|
|
2018-05-05 02:59:43 +02:00
|
|
|
const { Schema } = mongoose;
|
2017-02-22 20:29:35 +01:00
|
|
|
|
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
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
fileSchema.virtual('id').get(function getFileId() {
|
2016-11-17 17:15:35 +01:00
|
|
|
return this._id.toHexString();
|
2016-07-07 19:50:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
fileSchema.set('toJSON', {
|
2016-11-17 17:15:35 +01:00
|
|
|
virtuals: true
|
2016-07-07 19:50:52 +02:00
|
|
|
});
|
|
|
|
|
2018-12-15 08:13:58 +01:00
|
|
|
const projectSchema = new Schema(
|
|
|
|
{
|
2020-08-03 22:42:28 +02:00
|
|
|
name: { type: String, default: "Hello p5.js, it's the server", maxlength: 128 },
|
2018-12-15 08:13:58 +01:00
|
|
|
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 }
|
|
|
|
);
|
2016-06-16 22:07:38 +02:00
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
projectSchema.virtual('id').get(function getProjectId() {
|
2016-11-17 17:15:35 +01:00
|
|
|
return this._id;
|
2016-07-07 19:50:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
projectSchema.set('toJSON', {
|
2016-11-17 17:15:35 +01:00
|
|
|
virtuals: true
|
2016-07-07 19:50:52 +02:00
|
|
|
});
|
|
|
|
|
2019-02-10 02:27:03 +01:00
|
|
|
projectSchema.pre('save', function generateSlug(next) {
|
2018-02-07 22:00:09 +01:00
|
|
|
const project = this;
|
2019-08-30 20:26:57 +02:00
|
|
|
|
|
|
|
if (!project.slug) {
|
|
|
|
project.slug = slugify(project.name, '_');
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:00:09 +01:00
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2019-08-30 20:26:57 +02:00
|
|
|
/**
|
|
|
|
* Check if slug is unique for this user's projects
|
|
|
|
*/
|
|
|
|
projectSchema.methods.isSlugUnique = async function isSlugUnique(cb) {
|
|
|
|
const project = this;
|
|
|
|
const hasCallback = typeof cb === 'function';
|
|
|
|
|
|
|
|
try {
|
|
|
|
const docsWithSlug = await project.model('Project')
|
|
|
|
.find({ user: project.user, slug: project.slug }, '_id')
|
|
|
|
.exec();
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
isUnique: docsWithSlug.length === 0,
|
|
|
|
conflictingIds: docsWithSlug.map(d => d._id) || []
|
|
|
|
};
|
|
|
|
|
|
|
|
if (hasCallback) {
|
|
|
|
cb(null, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} catch (err) {
|
|
|
|
if (hasCallback) {
|
|
|
|
cb(err, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export default mongoose.model('Project', projectSchema);
|