Last active
December 31, 2015 17:43
-
-
Save BerkeleyTrue/49128350c2d651040ea6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-process-exit */ | |
require('dotenv').load(); | |
var secrets = require('./config/secrets'), | |
mongodb = require('mongodb'), | |
Rx = require('rx'), | |
Observable = Rx.Observable, | |
MongoClient = mongodb.MongoClient; | |
const points = [ | |
{username: 'abhisekp', points: 200 }, | |
{username: 'HermanFassett', points: 110 }, | |
{username: 'luishendrix92 ', points: 100 }, | |
{username: 'qmikew1', points: 90 }, | |
{username: 'CaroleAnneHannon', points: 60 }, | |
{username: 'KingxBannana', points: 20 }, | |
{username: 'wildlifehexagon', points: 10 }, | |
{username: 'sludge256', points: 10 }, | |
{username: 'jbmartinez', points: 10 } | |
]; | |
/* testing | |
const points = [ | |
{ username: 'berkeleytrue', points: 20 } | |
]; | |
*/ | |
function update$(collection, query, update) { | |
return Observable.fromNodeCallback( | |
collection.update, | |
collection | |
)(query, update, { upsert: false }); | |
} | |
MongoClient.connect(secrets.db, function(err, database) { | |
if (err) { | |
throw err; | |
} | |
const users = database.collection('user'); | |
Observable.from(points) | |
.map( | |
({ username, points }) => ({ username: username.toLowerCase(), points }) | |
) | |
.flatMap(({ username, points }) => { | |
const brownie = { | |
timestamp: Date.now(), giver: 'saintpeter', reason: 'beta-tester' | |
}; | |
return Observable.repeat(brownie, points) | |
// make sure each object is unique | |
.map(brownie => Object.assign({}, brownie)) | |
.toArray() | |
.map(brownies => ({ username, brownies })); | |
}) | |
.flatMap(({ username, brownies }) => { | |
return update$( | |
// collection | |
users, | |
// query for { username: 'username' } | |
{ username }, | |
// push onto progressTimestaps each one of these delicious brownies | |
{ $push: { progressTimestamps: { $each: brownies } } } | |
) | |
.map(result => ({ username, result })) | |
// prevent error from crashing whole observable | |
.catch(err => Observable.just({ username, err })); | |
}) | |
.subscribe( | |
({ username, result }) => { | |
console.log(`${username} updated with ${result}`); | |
}, | |
err => { throw err; }, | |
() => process.exit(0) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment