Skip to content

feat (mongoose): use mongoose-bird to promisify the mongoose API #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"lodash": "~2.4.1",<% if(filters.jade) { %>
"jade": "~1.2.0",<% } %><% if(filters.html) { %>
"ejs": "~0.8.4",<% } %><% if(filters.mongoose) { %>
"mongoose": "~3.8.8",<% } %><% if(filters.auth) { %>
"mongoose": "~3.8.8",
"mongoose-bird": "~0.0.1",
<% } %><% if(filters.auth) { %>
"jsonwebtoken": "^0.3.0",
"express-jwt": "^0.1.3",
"passport": "~0.2.0",
Expand Down Expand Up @@ -46,7 +48,7 @@
"grunt-contrib-watch": "~0.6.1",<% if(filters.coffee) { %>
"grunt-contrib-coffee": "^0.10.1",<% } %><% if(filters.jade) { %>
"grunt-contrib-jade": "^0.11.0",<% } %><% if(filters.less) { %>
"grunt-contrib-less": "^0.11.0",<% } %>
"grunt-contrib-less": "^0.11.4",<% } %>
"grunt-google-cdn": "~0.4.0",
"grunt-newer": "~0.7.0",
"grunt-ng-annotate": "^0.2.3",
Expand Down
12 changes: 6 additions & 6 deletions app/templates/server/api/thing/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,47 @@ describe('Thing API Router:', function() {
describe('GET /api/things', function() {

it('should route to thing.controller.index', function() {
return router.get.withArgs('/', 'thingCtrl.index').should.have.been.calledOnce;
router.get.withArgs('/', 'thingCtrl.index').should.have.been.calledOnce;
});

});<% if(filters.mongoose) { %>

describe('GET /api/things/:id', function() {

it('should route to thing.controller.show', function() {
return router.get.withArgs('/:id', 'thingCtrl.show').should.have.been.calledOnce;
router.get.withArgs('/:id', 'thingCtrl.show').should.have.been.calledOnce;
});

});

describe('POST /api/things', function() {

it('should route to thing.controller.create', function() {
return router.post.withArgs('/', 'thingCtrl.create').should.have.been.calledOnce;
router.post.withArgs('/', 'thingCtrl.create').should.have.been.calledOnce;
});

});

describe('PUT /api/things/:id', function() {

it('should route to thing.controller.update', function() {
return router.put.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
router.put.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
});

});

describe('PATCH /api/things/:id', function() {

it('should route to thing.controller.update', function() {
return router.patch.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
router.patch.withArgs('/:id', 'thingCtrl.update').should.have.been.calledOnce;
});

});

describe('DELETE /api/things/:id', function() {

it('should route to thing.controller.destroy', function() {
return router.delete.withArgs('/:id', 'thingCtrl.destroy').should.have.been.calledOnce;
router.delete.withArgs('/:id', 'thingCtrl.destroy').should.have.been.calledOnce;
});

});<% } %>
Expand Down
114 changes: 74 additions & 40 deletions app/templates/server/api/thing/thing.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,57 @@
* DELETE /things/:id -> destroy
*/

'use strict';
'use strict';<% if (filters.mongoose) { %>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The <% if (filters.mongoose) { %> that you mention was moved to this line

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pentateu I moved it to include lodash in the conditional since its not needed when NOT using mongoose


var _ = require('lodash');<% if (filters.mongoose) { %>
var Thing = require('./thing.model');<% } %>
var _ = require('lodash');
var Thing = require('./thing.model');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kingcody I noticed that you have removed the <% if (filters.mongoose) { %> from around the var Thing = require('./thing.model'); ... and in some other places also.

This is required so when the mongoose option is turned off in the generator this line is not generated in the final output.


function handleError(res, statusCode){
statusCode = statusCode || 500;
return function(err){
res.send(statusCode, err);
};
}

function responseWithResult(res, statusCode){
statusCode = statusCode || 200;
return function(entity){
if(entity){
return res.json(statusCode, entity);
}
};
}

function handleEntityNotFound(res){
return function(entity){
if(!entity){
res.send(404);
return null;
}
return entity;
};
}

function saveUpdates(updates){
return function(entity){
var updated = _.merge(entity, updates);
return updated.saveAsync()
.then(function () {
return updated;
});
};
}

function removeEntity(res){
return function (entity) {
if(entity){
return entity.removeAsync()
.then(function() {
return res.send(204);
});
}
};
}<% } %>

// Get list of things
exports.index = function(req, res) {<% if (!filters.mongoose) { %>
Expand All @@ -34,56 +81,43 @@ exports.index = function(req, res) {<% if (!filters.mongoose) { %>
name : 'Deployment Ready',
info : 'Easily deploy your app to Heroku or Openshift with the heroku and openshift subgenerators'
}
]);<% } %><% if (filters.mongoose) { %>
Thing.find(function (err, things) {
if(err) { return handleError(res, err); }
return res.json(200, things);
});<% } %>
]);<% } if (filters.mongoose) { %>
Thing.findAsync()
.then(responseWithResult(res))
.catch(handleError(res));<% } %>
};<% if (filters.mongoose) { %>

// Get a single thing
exports.show = function(req, res) {
Thing.findById(req.params.id, function (err, thing) {
if(err) { return handleError(res, err); }
if(!thing) { return res.send(404); }
return res.json(thing);
});
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(responseWithResult(res))
.catch(handleError(res));
};

// Creates a new thing in the DB.
exports.create = function(req, res) {
Thing.create(req.body, function(err, thing) {
if(err) { return handleError(res, err); }
return res.json(201, thing);
});
Thing.createAsync(req.body)
.then(responseWithResult(res, 201))
.catch(handleError(res));
};

// Updates an existing thing in the DB.
exports.update = function(req, res) {
if(req.body._id) { delete req.body._id; }
Thing.findById(req.params.id, function (err, thing) {
if (err) { return handleError(res, err); }
if(!thing) { return res.send(404); }
var updated = _.merge(thing, req.body);
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, thing);
});
});
if(req.body._id) {
delete req.body._id;
}
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(responseWithResult(res))
.catch(handleError(res));
};

// Deletes a thing from the DB.
exports.destroy = function(req, res) {
Thing.findById(req.params.id, function (err, thing) {
if(err) { return handleError(res, err); }
if(!thing) { return res.send(404); }
thing.remove(function(err) {
if(err) { return handleError(res, err); }
return res.send(204);
});
});
};

function handleError(res, err) {
return res.send(500, err);
}<% } %>
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
};<% } %>
2 changes: 1 addition & 1 deletion app/templates/server/api/thing/thing.model(mongoose).js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var mongoose = require('mongoose'),
var mongoose = require('mongoose-bird')(),
Schema = mongoose.Schema;

var ThingSchema = new Schema({
Expand Down
12 changes: 6 additions & 6 deletions app/templates/server/api/user(auth)/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,47 +47,47 @@ describe('User API Router:', function() {
describe('GET /api/users', function() {

it('should verify admin role and route to user.controller.index', function() {
return router.get.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index').should.have.been.calledOnce;
router.get.withArgs('/', 'authService.hasRole.admin', 'userCtrl.index').should.have.been.calledOnce;
});

});

describe('DELETE /api/users/:id', function() {

it('should verify admin role and route to user.controller.destroy', function() {
return router.delete.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy').should.have.been.calledOnce;
router.delete.withArgs('/:id', 'authService.hasRole.admin', 'userCtrl.destroy').should.have.been.calledOnce;
});

});

describe('GET /api/users/me', function() {

it('should be authenticated and route to user.controller.me', function() {
return router.get.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me').should.have.been.calledOnce;
router.get.withArgs('/me', 'authService.isAuthenticated', 'userCtrl.me').should.have.been.calledOnce;
});

});

describe('PUT /api/users/:id/password', function() {

it('should be authenticated and route to user.controller.changePassword', function() {
return router.put.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword').should.have.been.calledOnce;
router.put.withArgs('/:id/password', 'authService.isAuthenticated', 'userCtrl.changePassword').should.have.been.calledOnce;
});

});

describe('GET /api/users/:id', function() {

it('should be authenticated and route to user.controller.show', function() {
return router.get.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show').should.have.been.calledOnce;
router.get.withArgs('/:id', 'authService.isAuthenticated', 'userCtrl.show').should.have.been.calledOnce;
});

});

describe('POST /api/users', function() {

it('should route to user.controller.create', function() {
return router.post.withArgs('/', 'userCtrl.create').should.have.been.calledOnce;
router.post.withArgs('/', 'userCtrl.create').should.have.been.calledOnce;
});

});
Expand Down
Loading