Skip to content
Open
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
8 changes: 4 additions & 4 deletions lib/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = function Bulk(collection, ordered) {
//Runs operations only one at a time
function executeOperations(operations, callback) {
callback = arguments[arguments.length - 1];
asyncish(() => {
asyncish().then(() => {
operations.reduce((promiseChain, operation) => {
return promiseChain.then(() => {
executedOps.push(operation);
Expand All @@ -41,7 +41,7 @@ module.exports = function Bulk(collection, ordered) {
callback(null, executedOps);
})
.catch(callback)
});
}).catch(e => callback(e));
if (typeof callback !== 'function') {
return new Promise(function (resolve, reject) {
callback = function (e, r) { e ? reject(e) : resolve(r) };
Expand All @@ -58,13 +58,13 @@ module.exports = function Bulk(collection, ordered) {
promises.push(operation.fnc.apply(this, operation.args));
}

asyncish(() => {
asyncish().then(() => {
Promise.all(promises)
.then((operations) => {
callback(null, operations)
})
.catch(callback);
});
}).catch(e => callback(e));

if (typeof callback !== 'function') {
return new Promise(function (resolve, reject) {
Expand Down
37 changes: 19 additions & 18 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ module.exports = function Collection(db, state) {
debug('deleteMany %j', filter);

const opts = find_options(arguments);
asyncish(function () {
asyncish().then(function () {
cursor(state.documents || [], opts).toArray((err, docsToRemove) => {
if (err) return callback(err);
debug('docs', docsToRemove);
if (docsToRemove.length) {
debug(state.documents.length);
Expand All @@ -76,7 +77,7 @@ module.exports = function Collection(db, state) {
}
callback(null, { result: { n: docsToRemove.length, ok: 1 }, deletedCount: docsToRemove.length, connection: db });
});
});
}).catch(e => callback(e));

if (typeof callback !== 'function') {
return new Promise(function(resolve, reject) {
Expand All @@ -89,7 +90,7 @@ module.exports = function Collection(db, state) {

debug('deleteOne %j', filter);

asyncish(function () {
asyncish().then(function () {
var deletionIndex = _.findIndex(state.documents || [], filter);
var docs = deletionIndex === -1 ? [] : state.documents.splice(deletionIndex, 1);

Expand All @@ -98,7 +99,7 @@ module.exports = function Collection(db, state) {
state.persist();
}
callback(null, { result: { n: docs.length, ok: 1 }, deletedCount: docs.length, connection: db });
});
}).catch(e => callback(e));
if (typeof callback !== 'function') {
return new Promise(function (resolve, reject) {
callback = function (e, r) { e ? reject(e) : resolve(r) };
Expand All @@ -121,9 +122,9 @@ module.exports = function Collection(db, state) {
if (!opts.callback)
return crsr;

asyncish(function () {
asyncish().then(function () {
opts.callback(null, crsr);
});
}).catch(e => callback(e));
},
findAndModify: NotImplemented,
findAndRemove: NotImplemented,
Expand All @@ -138,7 +139,7 @@ module.exports = function Collection(db, state) {

crsr.next().then(function (doc) {
opts.callback(null, doc);
});
}).catch(e => callback(e));
},
findOneAndDelete: NotImplemented,
findOneAndReplace: NotImplemented,
Expand Down Expand Up @@ -168,7 +169,7 @@ module.exports = function Collection(db, state) {
callback(null, findResult);
});
})
.catch(callback);
.catch(e => callback(e));

if (typeof callback!=='function') {
return new Promise(function (resolve,reject) {
Expand Down Expand Up @@ -204,7 +205,7 @@ module.exports = function Collection(db, state) {
//The observed behavior of `mongodb` is that documents
// are committed until the first error. No information
// about the successful inserts are return :/
asyncish(function () {
asyncish().then(function () {
var insertedIds = [];
for (var i = 0; i < docs.length; i++) {
var doc = docs[i];
Expand All @@ -230,7 +231,7 @@ module.exports = function Collection(db, state) {
connection: {},
ops: _.cloneDeepWith(docs, cloneObjectIDs)
});
});
}).catch(e => callback(e));
if (typeof callback !== 'function') {
return new Promise(function (resolve, reject) {
callback = function (e, r) { e ? reject(e) : resolve(r) };
Expand Down Expand Up @@ -275,14 +276,14 @@ module.exports = function Collection(db, state) {

debug('remove %j', selector);

asyncish(function () {
asyncish().then(function () {
var docs = _.remove(state.documents || [], selector);
if (docs.length) {
if (debug.enabled) debug("removed: " + docs.map(function (doc) { return doc._id; }));
state.persist();
}
callback(null, {result:{n:docs.length,ok:1}, ops:docs, connection:db});
});
}).catch(e => callback(e));
if (typeof callback !== 'function') {
return new Promise(function (resolve, reject) {
callback = function (e, r) { e ? reject(e) : resolve(r) };
Expand Down Expand Up @@ -312,7 +313,7 @@ module.exports = function Collection(db, state) {
var action = (options.upsert?"upsert: ":"update: ");
debug('%s.%s %j', name, action, selector);

asyncish(function () {
asyncish().then(function () {
var docs = state.documents || [];
if (options.multi) {
docs = docs.filter(sift(selector))
Expand Down Expand Up @@ -354,7 +355,7 @@ module.exports = function Collection(db, state) {

state.persist();
callback(null, opResult);
});
}).catch(e => callback(e));

if (typeof callback !== 'function') {
return new Promise(function (resolve, reject) {
Expand All @@ -381,7 +382,7 @@ module.exports = function Collection(db, state) {
var action = (options.upsert ? "upsert: " : "update: ");
debug('%s.%s %j', name, action, selector);

asyncish(function () {
asyncish().then(function () {
var docs = (state.documents || []).filter(sift(selector));
if (!Array.isArray(docs)) docs = [docs];
debug('%s.%s %j', name, action, docs);
Expand Down Expand Up @@ -417,7 +418,7 @@ module.exports = function Collection(db, state) {

state.persist();
callback(null, opResult);
});
}).catch(e => callback(e));

if (typeof callback !== 'function') {
return new Promise(function (resolve, reject) {
Expand All @@ -444,7 +445,7 @@ module.exports = function Collection(db, state) {
var action = (options.upsert ? "upsert: " : "update: ");
debug('%s.%s %j', name, action, selector);

asyncish(function () {
asyncish().then(function () {
var docs = first(selector, state.documents || []) || [];
if (!Array.isArray(docs)) docs = [docs];
debug('%s.%s %j', name, action, docs);
Expand Down Expand Up @@ -480,7 +481,7 @@ module.exports = function Collection(db, state) {

state.persist();
callback(null, opResult);
});
}).catch(e => callback(e));

if (typeof callback !== 'function') {
return new Promise(function (resolve, reject) {
Expand Down
16 changes: 8 additions & 8 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ var Cursor = module.exports = function(documents, opts) {
if(typeof callback !== 'function')
return Promise.resolve(getDocs(applySkipLimit).length);

asyncish(function () {
asyncish().then(function () {
callback(null, getDocs(applySkipLimit).length)
});
}).catch(callback);
},

project: function (toProject) {
Expand Down Expand Up @@ -109,9 +109,9 @@ var Cursor = module.exports = function(documents, opts) {
if(typeof callback !== 'function')
return Promise.resolve(doc);

asyncish(function () {
asyncish().then(function () {
callback(null, doc);
});
}).catch(callback);
},

_triggerStream: function (filteredDocuments) {
Expand Down Expand Up @@ -163,9 +163,9 @@ var Cursor = module.exports = function(documents, opts) {
if(!callback)
return Promise.resolve(done());

asyncish(function () {
asyncish().then(function () {
callback(null, done())
});
}).catch(callback);
},

forEach: function (iterator, callback) {
Expand All @@ -182,9 +182,9 @@ var Cursor = module.exports = function(documents, opts) {
if(!callback)
return Promise.resolve(done());

asyncish(function () {
asyncish().then(function () {
callback(null, done())
});
}).catch(callback);
},

on: function (event, fn) {
Expand Down
8 changes: 4 additions & 4 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var Db = module.exports = function(dbname, server) {
if(typeof options !== 'object') options = {};

debug('createCollection("%s")', name);
asyncish(function () {
asyncish().then(function () {
var collection = getCollection(name);
if(collection.documents) {
debug('createCollection("%s") - collection exists', name);
Expand All @@ -67,7 +67,7 @@ var Db = module.exports = function(dbname, server) {
collection.persist(options.autoIndexId);

callback(null, collection);
});
}).catch(e => callback(e));

if(typeof callback!=='function') {
return new Promise(function (resolve) {
Expand Down Expand Up @@ -167,14 +167,14 @@ var Db = module.exports = function(dbname, server) {
},
logout: NotImplemented,
open: function(callback) {
asyncish(function () {
asyncish().then(function () {
if(!open) {
debug('%s open', dbname);
//keep the process running like a live connection would
open = setInterval(function () {}, 600000);
}
callback(null, iface);
});
}).catch(e => callback(e));
if(typeof callback!=='function') {
return new Promise(function (resolve) {
callback = function (e, r) { resolve(r) };
Expand Down
6 changes: 4 additions & 2 deletions mongo-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ module.exports = {
get max_delay() { return delay; },
set max_delay(n) { delay = Number(n) || 0; },
// pretend we are doing things async
asyncish: function asyncish(callback) {
setTimeout(callback, Math.random()*(delay));
asyncish: function asyncish() {
return new Promise(function (resolve) {
setTimeout(resolve, Math.random()*(delay));
});
},
get find_options() { return require('./lib/find_options.js') },
get MongoClient() { return require('./lib/mongo_client.js') },
Expand Down
30 changes: 29 additions & 1 deletion test/mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('mock tests', function () {
connected_db.close().then(done).catch(done)
});


describe('databases', function() {
it('should list collections', function(done) {
var listCollectionName = "test_databases_listCollections_collection";
Expand Down Expand Up @@ -1381,4 +1380,33 @@ describe('mock tests', function () {
});
});
});

describe('entire module', function () {
before(function (done) {
collection.insertOne({
items: [
{
description: 'a description'
}
]
}, done);
});

after(function (done) {
collection.remove({}, done);
});

it('should not throw uncatchable errors', function (done) {
// this call should make ModifyJs throw an error
collection.findOneAndUpdate(
{ 'items.description': 'a description' },
{ $set: { 'items.$.description': 'a new description' } },
function (err) {
if (!err) return done(new Error('Expected to catch an error'));
err.message.should.equal('The positional operator did not find the match needed from the query');
done();
}
);
});
});
});