This repository has been archived by the owner on Nov 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.js
executable file
·98 lines (74 loc) · 2.07 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
#!/usr/bin/env node
'use strict';
require('log-timestamp');
var path = require('path');
var async = require('async');
var glob = require('glob');
var is = require('annois');
var sugar = require('mongoose-sugar');
var config = require('./config');
var csv = require('./lib/csv');
var schemas = require('./schemas');
if(require.main === module) {
main();
}
function main() {
console.log('Starting loading');
console.log('Connecting to database');
sugar.connect(sugar.parseAddress(config.mongo), function(err) {
if(err) {
return console.error(err);
}
console.log('Connected to database');
async.series([
registrations.bind(null, config.registrationPath),
csvs.bind(null, path.join(__dirname, 'csv'))
], function(err) {
if(err) {
return console.error(err);
}
console.log('Done loading');
sugar.disconnect();
});
});
}
function registrations(p, cb) {
var schema = schemas.registrations;
csv.readRegistrations(p, function(err, data, resumeCb) {
if(err) {
return cb(err);
}
load('registrations', schema, data, resumeCb);
}, cb);
}
function csvs(p, cb) {
glob(path.join(p, '*.csv'), function(err, paths) {
if(err) {
return cb(err);
}
async.eachSeries(paths, function(p, cb) {
var name = path.basename(p, path.extname(p));
var schema = schemas[name];
csv.read(p, function(err, data, resumeCb) {
if(err) {
return cb(err);
}
load(name, schema, data, resumeCb);
}, cb);
}, cb);
});
}
function load(name, schema, data, cb) {
console.log('loading', name, data.gid);
if(!is.defined(data.gid)) {
return cb();
}
sugar.getOrCreate(schema, {
gid: data.gid
}, function(err, d) {
if(err) {
return cb(err);
}
sugar.update(schema, d._id, data, cb);
});
}