Skip to content

Instantly share code, notes, and snippets.

@abrkn
Created October 29, 2012 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abrkn/3970781 to your computer and use it in GitHub Desktop.
Save abrkn/3970781 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose')
, secrets = require('../secrets')
, monk = require('monk')
, db = monk(secrets.db)
, _ = require('underscore')
, users = db.get('users');
module.exports = db;
_.extend(users, {
take: function(userId, amount, callback) {
users.update({
_id: userId,
balance: { $gte: amount }
}, {
$inc: { balance: -amount }
}, function(err, updates) {
callback(err ? err : updates ? null : new Error('failed to reserve amount'));
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment