forked from linnovate/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.js
54 lines (47 loc) · 928 Bytes
/
article.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
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
required: true,
trim: true
},
content: {
type: String,
required: true,
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
/**
* Validations
*/
ArticleSchema.path('title').validate(function(title) {
return !!title;
}, 'Title cannot be blank');
ArticleSchema.path('content').validate(function(content) {
return !!content;
}, 'Content cannot be blank');
/**
* Statics
*/
ArticleSchema.statics.load = function(id, cb) {
this.findOne({
_id: id
}).populate('user', 'name username').exec(cb);
};
mongoose.model('Article', ArticleSchema);