-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
158 lines (146 loc) · 5.38 KB
/
db.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var mongoose = require('mongoose')
var mongoosePaginate = require('mongoose-paginate')
var async = require('async')
var colors = require('colors')
var faker = require('faker')
var _ = require('lodash')
var bcrypt = require('bcrypt')
var crypto = require('crypto')
var validator = require('validator')
var ValidationError = mongoose.Error.ValidationError
var Schema = mongoose.Schema
const SALT_WORK_FACTOR = 10
var validateEmail = function(email) {
if (email) return validator.isEmail(email)
return true
}
var validateURL = function(url) {
if (url) return validator.isURL(url)
return true
}
function omitUndefined(value) {
return value === undefined ? '' : value
}
var UserSchema = new Schema({
username: { type: String, maxlength: 30, required: true, unique: true },
password: { type: String, maxlength: 128, required: true },
first_name: { type: String, maxlength: 30, default: '', set: omitUndefined },
last_name: { type: String, maxlength: 30, default: '', set: omitUndefined },
email: { type: String, maxlength: 100, required: false, validate: [validateEmail, 'Please use a valid email address.'], default: '', set: omitUndefined},
is_staff: { type: Boolean, default: false },
is_active: { type: Boolean, default: true },
date_joined: { type: Date, default: Date.now, required: false },
token: { type: String, maxlength: 128, required: false }
})
UserSchema.post('validate', function(doc, next) {
if (doc.is_staff && !doc.is_active) {
var error = new ValidationError(this)
error.errors.__all__ = {'message': 'Staff member requires active user.'}
next(error)
} else {
next()
}
})
UserSchema.pre("save", function(next) {
var user = this
if (!user.isModified('password')) return next()
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next(err)
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err)
user.password = hash
next()
})
})
})
UserSchema.pre("save", function(next) {
var user = this
if (user.is_staff && user.is_active) {
if (!user.token) {
crypto.randomBytes(48, function (ex, buf) {
var token = buf.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
user.token = token.toString().slice(1, 40);
next()
})
} else {
next()
}
} else {
user.token = ""
next()
}
})
UserSchema.methods.comparePassword = function(candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) return cb(err)
cb(null, isMatch)
})
}
UserSchema.plugin(mongoosePaginate)
var User = mongoose.model('User', UserSchema)
var SectionSchema = new Schema({
name: { type: String, maxlength: 100, required: true, unique: true },
slug: { type: String, maxlength: 100 },
position: { type: Number }
})
SectionSchema.plugin(mongoosePaginate)
var Section = mongoose.model('Section', SectionSchema)
var CategorySchema = new Schema({
section: { type: Schema.Types.ObjectId, ref: 'Section', required: true },
name: { type: String, maxlength: 100, required: true },
slug: { type: String, maxlength: 100 },
position: { type: Number }
})
CategorySchema.plugin(mongoosePaginate)
var Category = mongoose.model('Category', CategorySchema)
var TagSchema = new Schema({
name: { type: String, maxlength: 200, required: true, unique: true },
slug: { type: String, maxlength: 100 }
})
TagSchema.plugin(mongoosePaginate)
var Tag = mongoose.model('Tag', TagSchema)
var EntrySchema = new Schema({
title: { type: String, maxlength: 200, required: true },
status: { type: String, enum: ['Draft', 'Online'], default: 'Draft', required: true },
date: { type: Date, required: true },
sticky: { type: Boolean, default: false },
section: { type: Schema.Types.ObjectId, ref: 'Section', required: true },
category: { type: Schema.Types.ObjectId, ref: 'Category', null: true, default: null },
tags: [ {type : Schema.Types.ObjectId, ref: 'Tag'} ],
image: { type: String, maxlength: 200, default: '', set: omitUndefined },
summary: { type: String, maxlength: 500, default: '', set: omitUndefined },
body: { type: String, maxlength: 5000, default: '', set: omitUndefined },
owner: { type: Schema.Types.ObjectId },
locked: { type: Boolean, default: false },
createdate: { type: Date },
updatedate: { type: Date }
})
EntrySchema.pre('save', function(next) {
this.updatedate = Date.now()
if (!this.createdate) {
this.createdate = Date.now()
}
next()
})
EntrySchema.plugin(mongoosePaginate)
var Entry = mongoose.model('Entry', EntrySchema)
var EntryLinkSchema = new Schema({
entry: { type: Schema.Types.ObjectId, required: true },
url: { type: String, maxlength: 200, required: true, validate: [validateURL, 'Please use a valid URL.'], },
title: { type: String, maxlength: 200, required: true },
description: { type: String, maxlength: 200, default: '', set: omitUndefined },
position: { type: Number }
})
EntryLinkSchema.plugin(mongoosePaginate)
var EntryLink = mongoose.model('EntryLink', EntryLinkSchema)
module.exports = {
models: {
User,
Section,
Category,
Tag,
Entry,
EntryLink
},
url: 'mongodb://localhost/blogapp',
}