Skip to content

Instantly share code, notes, and snippets.

@prestonp
Created October 16, 2014 18:40
Show Gist options
  • Save prestonp/6b9ab37ce8673e861986 to your computer and use it in GitHub Desktop.
Save prestonp/6b9ab37ce8673e861986 to your computer and use it in GitHub Desktop.
dirac middleware success hook
m.update = function(collection, options){
options = options || {};
return function(req, res){
collection = collection || req.collection;
collection.update(req.queryObj, req.body, req.queryOptions, function(error, results){
if (error) return console.log(error), res.status(400).send();
if (results && results.length == 0) return res.status(404).end();
if (!req.queryOptions.returning || req.queryOptions.returning.length == 0)
return res.status(204).end();
var val = !options.isMultiple ? results[0] : results;
if ( req.queryOptions.envelope ){
val = { data: val }
}
if (typeof options.success === 'function')
options.success(val);
res.status(200).json(val);
})
};
};
app.put('/api/orders/:id'
, m.restrict(['admin'])
, m.param('id')
, m.update( db.orders, { success: function(order) {
if ( order ) venter.emit('order:type:change', order);
}})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment