forked from ciqulover/CMS-of-Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
69 lines (57 loc) · 1.6 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
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
init = require('./init'),
userSchema = new Schema({
name: String,
password: String,
}),
articleSchema = new Schema({
title: String,
date: Date,
content: String,
}),
linkSchema = new Schema({
name: String,
href: String,
}),
User = mongoose.model('User', userSchema),
Article = mongoose.model('Article', articleSchema),
Link = mongoose.model('Link', linkSchema),
admin = new User(init.admin),
visitor = new User(init.visitor),
article = new Article(init.article),
link = new Link(init.link)
mongoose.connect('mongodb://localhost/platform')
mongoose.set('debug', true)
var db = mongoose.connection
db.on('error', function () {
console.log('error')
})
db.once('open', function () {
console.log('opened')
User.find(null, function (err, doc) {
if (err) {
console.log(err)
} else {
if (!doc[0]) {
visitor.save(function (err) {
if (err)return console.log(err)
})
link.save(function (err) {
if (err)return console.log(err)
})
admin.save(function (err) {
if (err)return console.log(err)
})
article.save(function (err) {
if (err)return console.log(err)
})
}
}
})
})
module.exports = {
User: User,
Article: Article,
Link: Link
}