-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,57 @@ | |
* DELETE /things/:id -> destroy | ||
*/ | ||
|
||
'use strict'; | ||
'use strict';<% if (filters.mongoose) { %> | ||
|
||
var _ = require('lodash');<% if (filters.mongoose) { %> | ||
var Thing = require('./thing.model');<% } %> | ||
var _ = require('lodash'); | ||
var Thing = require('./thing.model'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { %> | ||
|
@@ -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)); | ||
};<% } %> |
There was a problem hiding this comment.
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 lineThere was a problem hiding this comment.
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