-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.js
118 lines (105 loc) · 2.75 KB
/
load_data.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
var Question = require('./models/question');
var Email = require('./models/email');
var Color = require('./models/color');
var Person = require('./models/person');
var mongoose = require('mongoose');
var mongoDbUri = process.env.MONGOLAB_URI || 'mongodb://localhost/sample-mongo-scavenger-hunt';
var questionData = require('./data/questions.json');
var emailData = require('./data/enron.json');
mongoose.connect(mongoDbUri);
function loadQuestions(done){
var remaining = 0;
questionData.forEach(function(q){
var question = new Question(q);
remaining++;
question.save(function(err){
if (err) { console.log('err saving q:',err); }
remaining--;
});
});
var interval = setInterval(function(){
if (remaining > 0) {
console.log('remaining questions:',remaining);
} else {
clearInterval(interval);
done();
}
}, 500);
}
function loadEnron(done){
var remaining = 0;
emailData.forEach(function(e){
var email = new Email(e);
remaining++;
email.save(function(err){
if (err) { console.log('err saving e:',err); }
remaining--;
});
});
var interval = setInterval(function(){
if (remaining > 0) {
console.log('remaining emails:',remaining);
} else {
clearInterval(interval);
done();
}
}, 500);
}
var colorIds = [];
function loadColors(done){
var colorData = require('./data/colors.json');
var remaining = 0;
colorData.forEach(function(c){
var color = new Color(c);
remaining++;
color.save(function(err){
if (err) { console.log('err saving e:',err); }
colorIds.push(color.id);
remaining--;
});
});
var interval = setInterval(function(){
if (remaining > 0) {
console.log('remaining colors:',remaining);
} else {
clearInterval(interval);
done();
}
}, 500);
}
function loadPeople(done){
var personData = require('./data/celebs.json');
var remaining = 0;
personData.forEach(function(p){
var person = new Person(p);
person.colorId = colorIds[ Math.floor(Math.random()*colorIds.length)];
remaining++;
person.save(function(err){
if (err) { console.log('err saving e:',err); }
remaining--;
});
});
var interval = setInterval(function(){
if (remaining > 0) {
console.log('remaining persons:',remaining);
} else {
clearInterval(interval);
done();
}
}, 500);
}
mongoose.connection.db.on('open', function(){
loadQuestions(function(){
console.log('done with qs!');
loadEnron(function(){
console.log('done with enron!');
loadColors(function(){
console.log('done w/ colors');
loadPeople(function(){
console.log('done w/ people');
process.exit(1);
});
});
});
});
});