diff --git a/index.js b/index.js index c4cd58d..ac5e4ee 100644 --- a/index.js +++ b/index.js @@ -11,9 +11,10 @@ var buffertools = require('buffertools'); var crypto = require('crypto'); var reader = require('read-async-bson'); + module.exports = { // If you leave out "stream" it'll be stdout - dump: function(dbOrUri, stream, callback) { + dump: function(dbOrUri, stream, callback, opt) { if (arguments.length === 2) { callback = stream; stream = undefined; @@ -21,6 +22,7 @@ module.exports = { if (!stream) { stream = process.stdout; } + var opt = opt || {}; var db; var out = stream; var endOfCollection = crypto.pseudoRandomBytes(8).toString('base64'); @@ -50,6 +52,14 @@ module.exports = { return callback(err); } collections = _collections; + if (opt.only) + { + collections = _.filter(collections, function(collection){ return opt.only.indexOf(collection.collectionName) != -1; }); + } + if (opt.except) + { + collections = _.filter(collections, function(collection){ return opt.except.indexOf(collection.collectionName) == -1; }); + } return callback(null); }); }, @@ -73,34 +83,20 @@ module.exports = { }); }, getDocuments: function(callback) { - var cursor = collection.find({}, { raw: true }); - iterate(); - function iterate() { - return cursor.nextObject(function(err, item) { - if (err) { - return callback(err); - } - if (!item) { - write({ - // Ensures we don't confuse this with - // a legitimate database object - endOfCollection: endOfCollection - }); - return callback(null); - } - // v2: just a series of actual data documents - out.write(item); - - // If we didn't have the raw BSON document, - // we could do this instead, but it would be very slow - // write({ - // type: 'document', - // document: item - // }); - - return setImmediate(iterate); - }); - } + var cursor = collection.find({}, { raw: true }).stream(); + cursor.pipe(out, { end: false }); + cursor.on('end', function() { + write({ + // Ensures we don't confuse this with + // a legitimate database object + endOfCollection: endOfCollection + }); + return callback(null); + }); + + cursor.on('error', function(err) { + return callback(err); + }); }, }, callback); }, callback); @@ -109,7 +105,12 @@ module.exports = { write({ type: 'endDatabase' }); - return setImmediate(callback); + callback(null); + }, + disconnect: function(callback) { + db.close(); + db = null; + callback(null); } }, function(err) { if (err) { @@ -216,7 +217,6 @@ module.exports = { } return collection.insert(item.document, callback); } - // Just scan for the unique random string that // appears in the end-of-collection object. No need // to waste time parsing BSON for this. Also @@ -235,7 +235,7 @@ module.exports = { } return async.series({ flushIfNeeded: function(callback) { - if (insertQueueSize + item.length <= 16777216) { + if (insertQueueSize + item.length <= 16770000){ //16777216) { return setImmediate(callback); } return flush(callback); @@ -310,6 +310,11 @@ module.exports = { } return finalCallback(new Error('Premature end of stream')); }); + }, + disconnect: function(callback){ + db.close(); + db = null; + callback(null); } }, callback); } diff --git a/package.json b/package.json index 32491f5..d8a87b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongo-dump-stream", - "version": "0.3.2", + "version": "0.3.4", "description": "Dump and restore an entire MongoDB database via a single stream, without creating a folder of files", "main": "index.js", "scripts": { @@ -31,7 +31,7 @@ "bson": "^0.2.19", "buffertools": "^2.1.2", "lodash": "^3.3.1", - "mongodb": "^1.4.32", + "mongodb": "^2.2.19", "yargs": "^3.4.4", "read-async-bson": "^0.1.0" },