forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.js
56 lines (45 loc) · 1.36 KB
/
collection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import mongoose from 'mongoose';
import shortid from 'shortid';
import slugify from 'slugify';
const { Schema } = mongoose;
const collectedProjectSchema = new Schema(
{
project: { type: Schema.Types.ObjectId, ref: 'Project' },
},
{ timestamps: true, _id: true, usePushEach: true }
);
collectedProjectSchema.virtual('id').get(function getId() {
return this._id.toHexString();
});
collectedProjectSchema.virtual('projectId').get(function projectId() {
return this.populated('project');
});
collectedProjectSchema.virtual('isDeleted').get(function isDeleted() {
return this.project == null;
});
collectedProjectSchema.set('toJSON', {
virtuals: true
});
const collectionSchema = new Schema(
{
_id: { type: String, default: shortid.generate },
name: { type: String, default: 'My collection' },
description: { type: String },
slug: { type: String },
owner: { type: Schema.Types.ObjectId, ref: 'User' },
items: { type: [collectedProjectSchema] }
},
{ timestamps: true, usePushEach: true }
);
collectionSchema.virtual('id').get(function getId() {
return this._id;
});
collectionSchema.set('toJSON', {
virtuals: true
});
collectionSchema.pre('save', function generateSlug(next) {
const collection = this;
collection.slug = slugify(collection.name, '_');
return next();
});
export default mongoose.model('Collection', collectionSchema);