-
Notifications
You must be signed in to change notification settings - Fork 5
/
app-mongoose.js
170 lines (154 loc) · 4.78 KB
/
app-mongoose.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
159
160
161
162
163
164
165
166
167
168
169
170
var hljs = require('highlight.js');
var _ = require('lodash');
var express = require('express');
var mongoose = require('mongoose');
var deepPopulate = require('mongoose-deep-populate')(mongoose);
var app = express();
app.set('views', __dirname + '/public/views');
app.set('view engine', 'ejs');
app.engine('ejs', require('ejs').renderFile);
app.use(express.static(__dirname + '/public'));
getCode = function getCode(args) {
return hljs.highlight('javascript', args.callee.toString()).value;
}
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name: String,
email: String
}, { strict: 'throw'} );
var User = mongoose.model('User', UserSchema);
var CommentSchema = new Schema({
author : {type: Schema.Types.ObjectId, ref: 'User'},
title: String,
body: String
}, { strict: 'throw'} )
var Comment = mongoose.model('Comment', CommentSchema);
var PostSchema = new Schema({
title: String,
author: { type: Schema.Types.ObjectId, ref: 'User' },
comments: [{type: Schema.Types.ObjectId, ref: 'Comment'}],
body: String
}, { strict: 'throw'} );
PostSchema.plugin(deepPopulate, {
populate: {
'author': { select: 'name' },
'comments': { select: 'title author' },
'comments.author': { select: 'name' }
}
});
var Post = mongoose.model('Post', PostSchema);
var createComment = function(post, user, num) {
var comment = new Comment;
comment.author = user._id;
comment.title = 'comment' + num;
comment.body = 'comment' + num + ' body';
comment.save(function(err, comment) {
if (err) throw err;
post.comments.push(comment._id);
post.save();
console.log('Comment: ' + comment);
});
}
var createOrFindUser = function(name, email, callback) {
User.findOne({name: name} , function(err, user) {
if (err) throw err;
if (!user) {
var newUsr = new User({name: name, email: email});
newUsr.save(function(err, newUsr) {
if (err) throw err;
console.log(name + ': ', newUsr._id);
callback(newUsr);
});
} else {
callback(user);
}
});
}
app.theUserTester = null;
app.theUserPoster = null;
//Connect to mongo
app.use(function(req, res, next) {
if (mongoose.connection.readyState > 0) {
return next();
}
mongoose.connect('mongodb://localhost/mongotestdeeppop');
mongoose.connection.on('error', function(err) {
next(err);
});
mongoose.connection.on('open', function() {
createOrFindUser('Tester', '[email protected]', function(user) {
app.theUserTester = user;
});
createOrFindUser('Poster', '[email protected]', function(user) {
app.theUserPoster = user;
});
});
});
app.get('/', function (req, res, next) {
app.code = getCode(arguments);
var blog = new Post;
blog.title = 'test';
blog.body = 'test';
blog.author = app.theUserTester;
blog.save(function(err, post) {
if (err) throw err;
createComment(post, app.theUserPoster, 1);
createComment(post, app.theUserTester, 2);
Post.find(function(err, data) {
console.log('Post: ', data);
var out = JSON.stringify(data, null, 4);
res.render('data', {
page: 'home',
title: 'Mongoose - Creates a new model on each call',
code:app.code,
data: hljs.highlight('json', out).value});
});
});
});
app.get('/posts', function (req, res, next) {
app.code = getCode(arguments);
Post.find().exec(function(err, data) {
var out = JSON.stringify(data, null, 4);
res.render('data', {
page: 'posts',
title: 'Mongoose - Posts',
code:app.code,
data: hljs.highlight('json', out).value});
});
});
app.get('/refs', function (req, res, next) {
app.code = getCode(arguments);
Post.find().select('title author comments').deepPopulate('author comments.author').exec(function(err, data) {
var out = JSON.stringify(data, null, 4);
res.render('data', {
page: 'refs',
title: 'Mongoose - Post with deep populated data, only title for post',
code:app.code,
data: hljs.highlight('json', out).value});
});
});
app.get('/refsfull', function (req, res, next) {
app.code = getCode(arguments);
Post.find().deepPopulate('author comments.author').exec(function(err, data) {
var out = JSON.stringify(data, null, 4);
res.render('data', {
page: 'refsfull',
title: 'Mongoose - Post with deep populated data, all data for post',
code:app.code,
data: hljs.highlight('json', out).value});
});
});
app.get('/clear', function (req, res, next) {
Comment.remove(function(err, data) {
if (err) throw err;
Post.remove(function(err, data) {
if (err) throw err;
res.redirect('/');
});
});
});
var server = app.listen(3008, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
});