-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (54 loc) · 1.82 KB
/
app.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
/*consider this as fire&forget*/
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var url = 'mongodb://user:pw@ip:port/db_name';
console.log(void(0));
var insertDocuments = function(db, insertions, callback) {
// Get the documents collection
var collection = db.collection('test');
// Insert some documents
collection.insertMany(insertions, function(err, result) {
assert.equal(err, null);
assert.equal(insertions.length, result.result.n);
assert.equal(insertions.length, result.ops.length);
console.log("Inserted " + insertions.length + " documents into the document collection");
callback(result);
});
}
function pushToDb(array) {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
switch (process.argv[3]) {
case "--push":
case "-p":
insertDocuments(db, array, function() {
db.close();
});
break;
case "--debug":
case "-d":
console.log(array);
db.close();
break;
default:
console.log("You are missing an CLI argument. Valid example: 'node app.js codes.json --debug'");
db.close();
}
});
}
try {
var codes = require('./' + process.argv[2]);
var array = Object.keys(codes).map(function(k) {
return codes[k]
});
pushToDb(array, true);
} catch (err) {
switch (err.code) {
case "MODULE_NOT_FOUND":
console.log("The JSON: '" + process.argv[2] + "' was not found");
break;
default:
console.log(err);
}
}