Skip to content

Commit 4631564

Browse files
committed
feat (mongoose): use mongoose-bird to promisify the mongoose API
1 parent 7045907 commit 4631564

13 files changed

+366
-196
lines changed

Diff for: app/templates/_package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"lodash": "~2.4.1",<% if(filters.jade) { %>
1616
"jade": "~1.2.0",<% } %><% if(filters.html) { %>
1717
"ejs": "~0.8.4",<% } %><% if(filters.mongoose) { %>
18-
"mongoose": "~3.8.8",<% } %><% if(filters.auth) { %>
18+
"mongoose": "~3.8.8",
19+
"mongoose-bird": "~0.0.1",
20+
<% } %><% if(filters.auth) { %>
1921
"jsonwebtoken": "^0.3.0",
2022
"express-jwt": "^0.1.3",
2123
"passport": "~0.2.0",

Diff for: app/templates/server/api/thing/thing.controller.js

+82-39
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,60 @@
99

1010
'use strict';
1111

12-
var _ = require('lodash');<% if (filters.mongoose) { %>
13-
var Thing = require('./thing.model');<% } %>
12+
var _ = require('lodash');
13+
<% if (filters.mongoose) { %>
14+
var Thing = require('./thing.model');
15+
<% } %>
16+
17+
function handleError(res, statusCode){
18+
statusCode = statusCode || 500;
19+
return function(err){
20+
res.send(statusCode, err);
21+
};
22+
}
23+
24+
function responseWithResult(res, statusCode){
25+
statusCode = statusCode || 200;
26+
return function(entity){
27+
if(entity){
28+
return res.json(statusCode, entity);
29+
}
30+
};
31+
}
32+
33+
function handleEntityNotFound(res){
34+
return function(entity){
35+
if(!entity){
36+
res.send(404);
37+
return null;
38+
}
39+
return entity;
40+
};
41+
}
42+
43+
function saveUpdates(updates){
44+
return function(entity){
45+
var updated = _.merge(entity, updates);
46+
return updated.saveAsync(function () {
47+
return updated;
48+
});
49+
};
50+
}
51+
52+
function removeEntity(res){
53+
return function (entity) {
54+
if(entity){
55+
return entity.removeAsync()
56+
.then(function() {
57+
return res.send(204);
58+
});
59+
}
60+
};
61+
}
1462

1563
// Get list of things
16-
exports.index = function(req, res) {<% if (!filters.mongoose) { %>
64+
exports.index = function(req, res) {
65+
<% if (!filters.mongoose) { %>
1766
res.json([
1867
{
1968
name : 'Development Tools',
@@ -34,56 +83,50 @@ exports.index = function(req, res) {<% if (!filters.mongoose) { %>
3483
name : 'Deployment Ready',
3584
info : 'Easily deploy your app to Heroku or Openshift with the heroku and openshift subgenerators'
3685
}
37-
]);<% } %><% if (filters.mongoose) { %>
38-
Thing.find(function (err, things) {
39-
if(err) { return handleError(res, err); }
40-
return res.json(200, things);
41-
});<% } %>
42-
};<% if (filters.mongoose) { %>
86+
]);
87+
<% } %>
88+
<% if (filters.mongoose) { %>
89+
Thing.findAsync()
90+
.then(responseWithResult(res))
91+
.catch(handleError(res));
92+
<% } %>
93+
};
94+
95+
<% if (filters.mongoose) { %>
4396

4497
// Get a single thing
4598
exports.show = function(req, res) {
46-
Thing.findById(req.params.id, function (err, thing) {
47-
if(err) { return handleError(res, err); }
48-
if(!thing) { return res.send(404); }
49-
return res.json(thing);
50-
});
99+
Thing.findByIdAsync(req.params.id)
100+
.then(handleEntityNotFound(res))
101+
.then(responseWithResult(res))
102+
.catch(handleError(res));
51103
};
52104

53105
// Creates a new thing in the DB.
54106
exports.create = function(req, res) {
55-
Thing.create(req.body, function(err, thing) {
56-
if(err) { return handleError(res, err); }
57-
return res.json(201, thing);
58-
});
107+
Thing.createAsync(req.body)
108+
.then(responseWithResult(res, 201))
109+
.catch(handleError(res));
59110
};
60111

61112
// Updates an existing thing in the DB.
62113
exports.update = function(req, res) {
63-
if(req.body._id) { delete req.body._id; }
64-
Thing.findById(req.params.id, function (err, thing) {
65-
if (err) { return handleError(res, err); }
66-
if(!thing) { return res.send(404); }
67-
var updated = _.merge(thing, req.body);
68-
updated.save(function (err) {
69-
if (err) { return handleError(res, err); }
70-
return res.json(200, thing);
71-
});
72-
});
114+
if(req.body._id) {
115+
delete req.body._id;
116+
}
117+
Thing.findByIdAsync(req.params.id)
118+
.then(handleEntityNotFound(res))
119+
.then(saveUpdates(req.body))
120+
.then(responseWithResult(res))
121+
.catch(handleError(res));
73122
};
74123

75124
// Deletes a thing from the DB.
76125
exports.destroy = function(req, res) {
77-
Thing.findById(req.params.id, function (err, thing) {
78-
if(err) { return handleError(res, err); }
79-
if(!thing) { return res.send(404); }
80-
thing.remove(function(err) {
81-
if(err) { return handleError(res, err); }
82-
return res.send(204);
83-
});
84-
});
126+
Thing.findByIdAsync(req.params.id)
127+
.then(handleEntityNotFound(res))
128+
.then(removeEntity(res))
129+
.catch(handleError(res));
85130
};
86131

87-
function handleError(res, err) {
88-
return res.send(500, err);
89-
}<% } %>
132+
<% } %>

Diff for: app/templates/server/api/thing/thing.model(mongoose).js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var mongoose = require('mongoose'),
3+
var mongoose = require('mongoose-bird')(),
44
Schema = mongoose.Schema;
55

66
var ThingSchema = new Schema({

Diff for: app/templates/server/api/user(auth)/user.controller.js

+71-40
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,37 @@ var passport = require('passport');
55
var config = require('../../config/environment');
66
var jwt = require('jsonwebtoken');
77

8-
var validationError = function(res, err) {
9-
return res.json(422, err);
8+
var validationError = function(res, statusCode) {
9+
statusCode = statusCode || 422;
10+
return function(err){
11+
res.json(statusCode, err);
12+
};
1013
};
1114

15+
function handleError(res, statusCode){
16+
statusCode = statusCode || 500;
17+
return function(err){
18+
res.send(statusCode, err);
19+
};
20+
}
21+
22+
function respondWith(res, statusCode){
23+
statusCode = statusCode || 200;
24+
return function(){
25+
res.send(statusCode);
26+
};
27+
}
28+
1229
/**
1330
* Get list of users
1431
* restriction: 'admin'
1532
*/
1633
exports.index = function(req, res) {
17-
User.find({}, '-salt -password', function (err, users) {
18-
if(err) return res.send(500, err);
19-
res.json(200, users);
20-
});
34+
User.findAsync({}, '-salt -password')
35+
.then(function (users) {
36+
res.json(200, users);
37+
})
38+
.catch(handleError(res));
2139
};
2240

2341
/**
@@ -27,11 +45,12 @@ exports.create = function (req, res, next) {
2745
var newUser = new User(req.body);
2846
newUser.provider = 'local';
2947
newUser.role = 'user';
30-
newUser.save(function(err, user) {
31-
if (err) return validationError(res, err);
32-
var token = jwt.sign({_id: user._id }, config.secrets.session, { expiresInMinutes: 60*5 });
33-
res.json({ token: token });
34-
});
48+
newUser.saveAsync()
49+
.then(function(user) {
50+
var token = jwt.sign({_id: user._id }, config.secrets.session, { expiresInMinutes: 60*5 });
51+
res.json({ token: token });
52+
})
53+
.catch(validationError(res));
3554
};
3655

3756
/**
@@ -40,22 +59,26 @@ exports.create = function (req, res, next) {
4059
exports.show = function (req, res, next) {
4160
var userId = req.params.id;
4261

43-
User.findById(userId, function (err, user) {
44-
if (err) return next(err);
45-
if (!user) return res.send(401);
46-
res.json(user.profile);
47-
});
62+
User.findByIdAsync(userId)
63+
.then(function (user) {
64+
if(!user) {
65+
return res.send(401);
66+
}
67+
res.json(user.profile);
68+
})
69+
.catch(function(err){
70+
return next(err);
71+
});
4872
};
4973

5074
/**
5175
* Deletes a user
5276
* restriction: 'admin'
5377
*/
5478
exports.destroy = function(req, res) {
55-
User.findByIdAndRemove(req.params.id, function(err, user) {
56-
if(err) return res.send(500, err);
57-
return res.send(204);
58-
});
79+
User.findByIdAndRemoveAsync(req.params.id)
80+
.then(respondWith(res, 204))
81+
.catch(handleError(res));
5982
};
6083

6184
/**
@@ -66,35 +89,43 @@ exports.changePassword = function(req, res, next) {
6689
var oldPass = String(req.body.oldPassword);
6790
var newPass = String(req.body.newPassword);
6891

69-
User.findById(userId, function (err, user) {
70-
user.authenticate(oldPass, function(authErr, authenticated) {
71-
if (authErr) res.send(403);
92+
User.findByIdAsync(userId)
93+
.then(function(user) {
94+
user.authenticate(oldPass, function(authErr, authenticated) {
95+
if (authErr) res.send(403);
7296

73-
if (authenticated) {
74-
user.password = newPass;
75-
user.save(function(err) {
76-
if (err) return validationError(res, err);
77-
res.send(200);
78-
});
79-
} else {
80-
res.send(403);
81-
}
97+
if (authenticated) {
98+
user.password = newPass;
99+
user.saveAsync()
100+
.then(respondWith(res, 200))
101+
.catch(validationError(res));
102+
} else {
103+
res.send(403);
104+
}
105+
});
106+
})
107+
.catch(function(authErr){
108+
res.send(403);
82109
});
83-
});
84110
};
85111

86112
/**
87113
* Get my info
88114
*/
89115
exports.me = function(req, res, next) {
90116
var userId = req.user._id;
91-
User.findOne({
92-
_id: userId
93-
}, '-salt -password', function(err, user) { // don't ever give out the password or salt
94-
if (err) return next(err);
95-
if (!user) return res.json(401);
96-
res.json(user);
97-
});
117+
118+
User.findOneAsync({ _id: userId }, '-salt -password')
119+
.then(function(user) { // don't ever give out the password or salt
120+
if (!user) {
121+
return res.json(401);
122+
}
123+
res.json(user);
124+
})
125+
.catch(function(err){
126+
return next(err);
127+
});
128+
98129
};
99130

100131
/**

Diff for: app/templates/server/api/user(auth)/user.model.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var mongoose = require('mongoose');
3+
var mongoose = require('mongoose-bird')();
44
var Schema = mongoose.Schema;
55
var crypto = require('crypto');<% if(filters.oauth) { %>
66
var authTypes = ['github', 'twitter', 'facebook', 'google'];<% } %>
@@ -53,7 +53,9 @@ UserSchema
5353
UserSchema
5454
.path('email')
5555
.validate(function(email) {<% if (filters.oauth) { %>
56-
if (authTypes.indexOf(this.provider) !== -1) return true;<% } %>
56+
if (authTypes.indexOf(this.provider) !== -1){
57+
return true;
58+
} <% } %>
5759
return email.length;
5860
}, 'Email cannot be blank');
5961

@@ -70,14 +72,19 @@ UserSchema
7072
.path('email')
7173
.validate(function(value, respond) {
7274
var self = this;
73-
this.constructor.findOne({email: value}, function(err, user) {
74-
if(err) throw err;
75-
if(user) {
76-
if(self.id === user.id) return respond(true);
77-
return respond(false);
78-
}
79-
respond(true);
80-
});
75+
return this.constructor.findOneAsync({email: value})
76+
.then(function(user) {
77+
if(user) {
78+
if(self.id === user.id) {
79+
return respond(true);
80+
}
81+
return respond(false);
82+
}
83+
return respond(true);
84+
})
85+
.catch(function(err){
86+
throw err;
87+
});
8188
}, 'The specified email address is already in use.');
8289

8390
var validatePresenceOf = function(value) {

0 commit comments

Comments
 (0)