mongodb-restore-dump
Version:
restore mongodb bson dumps
91 lines (78 loc) • 1.94 kB
JavaScript
;
var fs = require('fs'),
MongoClient = require('mongodb').MongoClient,
BSON = require('bson');
module.exports = (options) => {
checkOptions(options);
return doRestoreCollection(options);
}
var doRestoreCollection = async ({
con,
uri,
database,
collection,
from,
clean = true,
limit,
}) => {
var serverConnection;
if (!con) {
serverConnection = (await MongoClient.connect(
uri,
{ useUnifiedTopology: true }
));
}
else {
serverConnection = con;
}
const db = serverConnection.db(database);
await db.createCollection(collection)
var dbCollection = db.collection(collection);
if (clean) {
await dbCollection.deleteMany({});
}
// FIXME: this will blow up on large collections
var buffer = fs.readFileSync(from);
var index = 0,
documents = [];
while (
buffer.length > index
&& (!limit || limit > documents.length)
) {
index = BSON.deserializeStream(
buffer,
index,
1,
documents,
documents.length
);
}
if (documents.length !== 0)
await dbCollection.insertMany(documents);
if (!con) {
serverConnection.close()
}
};
var checkOptions = ({
con,
uri,
database,
collection,
from
}) => {
if (!con && !uri) {
throw new Error('neither "con" nor "uri" option was given');
}
if (con && uri) {
throw new Error('you cannot use both "uri" and "con" option');
}
if (!database) {
throw new Error('missing "database" option');
}
if (!collection) {
throw new Error('missing "collection" option');
}
if (!from) {
throw new Error('missing "from" option');
}
}