Code coverage report for mongopatch/source/index.js

Statements: 94.12% (64 / 68)      Branches: 80.77% (21 / 26)      Functions: 100% (11 / 11)      Lines: 94.12% (64 / 68)      Ignored: none     

All files » mongopatch/source/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231 1 1 1 1   1 1   1   1   1 32 8 8   8       1 16     1 16 16   16   16 16   16 16 16 16     16   16 16           16 8   16 16     16 16 16 16   16   16 16   16   16 8 8   8   8   8     16   16 16       16   16       16   16 8 8     16       16   16 16       16       16     16     1  
var util = require('util');
var mongojs = require('mongojs');
var stream = require('stream-wrapper');
var semver = require('semver');
var moment = require('moment');
 
var streams = require('./streams');
var log = require('./log');
 
var packageJson = require('../package.json');
 
var TMP_COLLECTION = '_mongopatch_tmp';
 
var emit = function(event, dest, src) {
	src.on(event, function() {
		var args = Array.prototype.slice.call(arguments);
		args.unshift(event);
 
		dest.emit.apply(dest, args);
	});
};
 
var name = function(collection) {
	return 'patch_' + moment().format('YYMMDD.HHmmss.SSS') + '_' + collection.toString();
};
 
var create = function(patch, options) {
	var applicationDb = mongojs(options.db);
	var logDb = options.logDb && mongojs(options.logDb);
 
	var that = stream.passThrough({ objectMode: true });
 
	that.db = applicationDb;
	that.id = null;
 
	that.update = function(collection, query, worker) {
		Eif(!worker) {
			worker = query;
			query = null;
		}
 
		collection = applicationDb.collection(collection);
 
		that.id = name(collection);
		that._update = {
			collection: collection,
			query: query || {},
			worker: worker
		};
	};
	that.after = function(callback) {
		that._after = callback;
	};
	that.version = function(version) {
		that._version = version;
	};
 
	var update = function() {
		var collection = that._update.collection;
		var query = that._update.query;
		var worker = that._update.worker;
 
		var logCollection = logDb && logDb.collection(that.id);
 
		var opts = { afterCallback: that._after, concurrency: options.parallel };
		var stream = streams.patch(collection, query, { concurrency: options.parallel }, worker);
 
		emit('error', that, stream);
 
		if(options.dryRun) {
			var tmpCollection = applicationDb.collection(TMP_COLLECTION);
			var tmpStream = logCollection ? streams.logged.tmp(logCollection, tmpCollection, opts) : streams.tmp(tmpCollection, opts);
 
			stream = stream.pipe(tmpStream);
		} else {
			var updateStream = logCollection ? streams.logged.update(logCollection, opts) : streams.update(opts);
 
			stream = stream.pipe(updateStream);
		}
 
		emit('error', that, stream);
 
		collection.count(query, function(err, count) {
			Iif(err) {
				return that.emit('error', err);
			}
 
			stream = stream.pipe(streams.progress(count));
 
			Iif(options.output) {
				stream = stream.pipe(log({ patch: that.id, total: count }));
			}
 
			stream = stream.pipe(that);
 
			stream.on('end', function() {
				applicationDb.close();
				logDb && logDb.close();
			});
 
			stream.resume();
		});
	};
 
	patch(that);
 
	setImmediate(function() {
		Iif(!that._version || !semver.eq(that._version, packageJson.version)) {
			return that.emit('error', new Error(util.format('Specified version (%s) does not match current system version (%s)',
				that._version, packageJson.version)));
		}
		Iif(!that._update) {
			return that.emit('error', new Error('Update missing'));
		}
 
		update();
	});
 
	return that;
};
 
module.exports = create;