From 3d8b8e4c736e944c5d017a0d98e9b4e351342b3e Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Mon, 15 Jun 2015 16:47:50 -0700 Subject: [PATCH] rename flattenUser to loopbackMigration add stories migration to script --- seed/{flattenUser.js => loopbackMigration.js} | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) rename seed/{flattenUser.js => loopbackMigration.js} (78%) diff --git a/seed/flattenUser.js b/seed/loopbackMigration.js similarity index 78% rename from seed/flattenUser.js rename to seed/loopbackMigration.js index db107ade45..75f8910337 100644 --- a/seed/flattenUser.js +++ b/seed/loopbackMigration.js @@ -16,6 +16,14 @@ var providers = [ 'linkedin' ]; +// create async console.logs +function debug() { + var args = [].slice.call(arguments); + process.nextTick(function() { + console.log.apply(console, args); + }); +} + function createConnection(URI) { return Rx.Observable.create(function(observer) { MongoClient.connect(URI, function(err, database) { @@ -33,6 +41,7 @@ function createQuery(db, collection, options, batchSize) { cursor.batchSize(batchSize || 20); // Cursor.each will yield all doc from a batch in the same tick, // or schedule getting next batch on nextTick + debug('opening cursor for %s', collection); cursor.each(function (err, doc) { if (err) { return observer.onError(err); @@ -44,6 +53,7 @@ function createQuery(db, collection, options, batchSize) { }); return Rx.Disposable.create(function () { + debug('closing cursor for %s', collection); cursor.close(); }); }); @@ -132,18 +142,42 @@ var userIdentityCount = users // count how many times insert completes .count(); -Rx.Observable.merge( +var storyCount = dbObservable + .flatMap(function(db) { + return createQuery(db, 'stories', {}); + }) + .bufferWithCount(20) + .withLatestFrom(dbObservable, function(stories, db) { + return { + stories: stories, + db: db + }; + }) + .flatMap(function(dats) { + return insertMany(dats.db, 'stories', dats.stories, { w: 1 }); + }) + .count(); + +Rx.Observable.combineLatest( userIdentityCount, - userSavesCount + userSavesCount, + storyCount, + function(userIdentCount, userCount, storyCount) { + return { + userIdentCount: userIdentCount * 20, + userCount: userCount * 20, + storyCount: storyCount * 20 + }; + } ) .subscribe( - function(_count) { - count += _count * 20; + function(countObj) { + count = countObj; }, function(err) { console.error('an error occured', err, err.stack); }, function() { - console.log('finished with %s documents processed', count); + console.log('finished with ', count); } );