Skip to content

Commit

Permalink
Normalize callback arguments. Same as shell.
Browse files Browse the repository at this point in the history
  • Loading branch information
kapetan committed Dec 25, 2013
1 parent 4898d82 commit 3e0fa8c
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 8 deletions.
35 changes: 31 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ var ensureCallback = function(args) {
return args;
};

var ensureTruncatedCallback = function(args) {
args = ensureCallback(args);
args[args.length - 1] = truncateCallback(args[args.length - 1]);
return args;
};

var getCallback = function(args) {
var callback = args[args.length-1];
return typeof callback === 'function' ? callback : noop;
};

var truncateCallback = function(callback) {
return function(err) {
callback(err);
};
};

// Proxy for the native cursor prototype that normalizes method names and
// arguments to fit the mongo shell.

Expand Down Expand Up @@ -152,7 +164,10 @@ Collection.prototype.findAndModify = function(options, callback) {
remove:!!options.remove,
upsert:!!options.upsert,
fields:options.fields
}, callback || noop]);
}, function(err, doc) {
// Ensure arity
(callback || noop)(err, doc);
}]);
};

Collection.prototype.group = function(group, callback) {
Expand All @@ -166,13 +181,25 @@ Collection.prototype.remove = function() {
if (arguments.length > 1 && arguments[1] === true) { // the justOne parameter
this.findOne(arguments[0], function(err, doc) {
if (err) return callback(err);
if (!doc) return callback(null, 0);
self._apply(DRIVER_COLLECTION_PROTO.remove, [doc, callback]);
if (!doc) return callback();
self._apply(DRIVER_COLLECTION_PROTO.remove, [doc, truncateCallback(callback)]);
});
return;
}

this._apply(DRIVER_COLLECTION_PROTO.remove, arguments.length === 0 ? [{}, noop] : ensureCallback(arguments));
this._apply(DRIVER_COLLECTION_PROTO.remove, arguments.length === 0 ? [{}, noop] : ensureTruncatedCallback(arguments));
};

Collection.prototype.insert = function() {
this._apply(DRIVER_COLLECTION_PROTO.insert, ensureTruncatedCallback(arguments));
};

Collection.prototype.save = function() {
this._apply(DRIVER_COLLECTION_PROTO.save, ensureTruncatedCallback(arguments));
};

Collection.prototype.update = function() {
this._apply(DRIVER_COLLECTION_PROTO.update, ensureTruncatedCallback(arguments));
};

Collection.prototype.getIndexes = function() {
Expand Down
46 changes: 46 additions & 0 deletions tests/test-find-and-modify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var assert = require('assert');
var insert = require('./insert');

insert([{
id: 1,
hello: 'you'
}, {
id: 2,
hello: 'other'
}], function(db, done) {
// Update and find the old document
db.a.findAndModify({
query: { id: 1 },
update: { $set: { hello: 'world' } },
},
function(err, doc) {
assert.ok(!err);
assert.equal(arguments.length, 2);
assert.equal(doc.id, 1);
assert.equal(doc.hello, 'you');

// Update and find the new document
db.a.findAndModify({
query: { id: 2 },
'new': true,
update: { $set: { hello: 'me' } }
}, function(err, doc) {
assert.ok(!err);
assert.equal(arguments.length, 2);
assert.equal(doc.id, 2);
assert.equal(doc.hello, 'me');

// Remove and find document
db.a.findAndModify({
query: { id: 1 },
remove: true
}, function(err, doc) {
assert.ok(!err);
assert.equal(arguments.length, 2);
assert.equal(doc.id, 1);

done();
});
});
});
});
1 change: 0 additions & 1 deletion tests/test-get-collection-names.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var assert = require('assert');
var insert = require('./insert');

Expand Down
10 changes: 7 additions & 3 deletions tests/test-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ var insert = require('./insert');

// Delete just one
insert([{
name:'Squirtle', type:'water'
name:'Squirtle', type:'water'
}, {
name:'Starmie' , type:'water'
name:'Starmie' , type:'water'
}, {
name:'Lapras' , type:'water'
name:'Lapras' , type:'water'
}], function(db, done) {
// Remove just one
db.a.remove({type:'water'}, true, function(err) {
assert.ok(arguments.length <= 1);

db.a.find({type:'water'}, function(err, docs) {
assert.equal(docs.length, 2);
assert.equal(docs[0].name, 'Starmie')

// Normal remove
db.a.remove({type:'water'}, function(err) {
assert.ok(arguments.length <= 1);

db.a.find({type:'water'}, function(err, docs) {
assert.equal(docs.length, 0);
done();
Expand Down
1 change: 1 addition & 0 deletions tests/test-update-and-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ insert([{
db.a.update({hello:'world'}, {$set:{hello:'verden'}}, function(err) {
assert.ok(!sync);
assert.ok(!err);
assert.ok(arguments.length <= 1);
done();
});
sync = false;
Expand Down
2 changes: 2 additions & 0 deletions tests/test-update-multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ insert([{
}], function(db, done) {
db.a.update({}, {$set:{updated:true}}, {multi:true}, function(err) {
assert.ok(!err);
assert.ok(arguments.length <= 1);

db.a.find(function(err, docs) {
assert.ok(!err);
assert.equal(docs.length, 2);
Expand Down
2 changes: 2 additions & 0 deletions tests/test-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ insert([{
}], function(db, done) {
db.a.update({hello:'world'}, {$set:{hello:'verden'}}, function(err) {
assert.ok(!err);
assert.ok(arguments.length <= 1);

db.a.findOne(function(err, doc) {
assert.ok(!err);
assert.equal(doc.hello, 'verden');
Expand Down

0 comments on commit 3e0fa8c

Please sign in to comment.