Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Changes #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
69 changes: 37 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ 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;
}
if (!stream) {
stream = process.stdout;
}
var opt = opt || {};
var db;
var out = stream;
var endOfCollection = crypto.pseudoRandomBytes(8).toString('base64');
Expand Down Expand Up @@ -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);
});
},
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -310,6 +310,11 @@ module.exports = {
}
return finalCallback(new Error('Premature end of stream'));
});
},
disconnect: function(callback){
db.close();
db = null;
callback(null);
}
}, callback);
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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"
},
Expand Down