forked from repeatingbeats/foosboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jakefile
101 lines (81 loc) · 2.58 KB
/
Jakefile
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
const PRODUCTION_URI = 'http://foosboard.soundhacks.com';
const DEV_DB_URI = 'mongodb://localhost/foosboard-development';
var request = require('request'),
mongoose = require('mongoose'),
async = require('async')
;
var models = {};
[ 'Player', 'Result' ].forEach(function (model) {
require('./models/' + model.toLowerCase());
models[model] = mongoose.model(model);
});
mongoose.connect(DEV_DB_URI);
/*
* Clear Player and Result data from the development database
*/
task('clear', [], function() {
function clear_collection(collection) {
return function(callback) {
models[collection].remove({}, function(err) {
callback(err);
});
};
}
var clearFuncs = [];
for (var model in models) {
clearFuncs.push(clear_collection(model));
}
async.parallel(clearFuncs, function (err, empty_result) {
if (err) throw err.message;
console.log('cleared Player and Result data from ' + DEV_DB_URI);
complete();
});
}, true);
/*
* Retrieve all results from production and seed the foosboard-development db
* locally. Doing it with an actual production request so any dev can seed their
* local db without having access to the prod db.
*/
task('seed', [ 'clear' ], function() {
console.log('retriving results from ' + PRODUCTION_URI);
request({
uri: PRODUCTION_URI + '/results'
}, function (err, res, body) {
if (err) throw err.message;
var results = JSON.parse(body),
playerNames = [];
function checkName(name) {
if (playerNames.indexOf(name) < 0) {
playerNames.push(name);
}
}
results.forEach(function (result) {
checkName(result.winner);
checkName(result.loser);
});
var savePlayerFuncs = playerNames.map(function (name) {
return function(callback) {
var player = new models.Player({
name: name, wins: 0, losses: 0, goals_for: 0, goals_against: 0
});
player.save(function (err) { callback(err); });
};
});
async.parallel(savePlayerFuncs, function(err, empty_result) {
if (err) throw err.message;
var saveResultFuncs = results.map(function (result) {
return function(callback) {
result.date = new Date(result.utc);
delete result.utc;
var newResult = new models.Result(result);
newResult.save(function (err) { callback(err) });
}
});
async.series(saveResultFuncs, function (err, empty_result) {
if (err) console.log(err.message);
console.log('seeded data into ' + DEV_DB_URI);
return complete();
});
});
});
}, true);