-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmongodb-spore.js
More file actions
95 lines (83 loc) · 2.78 KB
/
mongodb-spore.js
File metadata and controls
95 lines (83 loc) · 2.78 KB
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
#!/usr/bin/env node
const commandLineArgs = require('command-line-args');
const optionsDefinitions = [
{name: 'src', alias: 's', type: String, multiple: true, defaultOption: true},
{name: 'help', alias: '?', type: Boolean},
{name: 'verbose', alias: 'v', type: Boolean, defaultValue: false},
{name: 'dropcollection', alias: 'd', type: Boolean, defaultValue: false},
];
const options = commandLineArgs(optionsDefinitions, {partial: true});
verboseOutput = require('./verbose-output');
vo = new verboseOutput(options.verbose);
vo.log("Command line options: " + JSON.stringify(options));
if(!options.src)
options.help = true;
if(options.help === true) {
const commandLineUsage = require("command-line-usage");
console.log(commandLineUsage([
{
header: 'mongodb-spore - a MongoDB Seeding tool',
content: 'Allows configurable bulk seeding of MongoDB databases'
},
{
header: 'Options',
optionList: [
{
name: 'src',
typeLabel: '[underline]{files}',
description: 'The input configuration file(s). Required'
},
{
name: 'help',
description: 'Display this usage guide'
}
]
}
])
);
return;
}
const YAML = require('yamljs');
const PATH = require('path');
var errHandler = function(err) {
console.log(err);
}
options.src.forEach((configFile) => {
vo.log('Processing configuration file ' + configFile);
var directoryName = PATH.dirname(configFile);
vo.log('directoryName: ' + directoryName);
var config = YAML.load(configFile);
vo.log('Configuration: ' + config);
var MongoClient = require('mongodb').MongoClient,
co = require('co'),
assert = require('assert');
co(function*() {
var db = yield MongoClient.connect(config.connection);
if(options.dropcollection)
{
yield db.dropCollection(config.collection).then(function(data) {
console.log(data)
vo.log('Dropped collection' + config.collection);
}, errHandler);
}
var collection = db.collection(config.collection);
config.data.files.forEach((dataFile) => {
vo.log('dataFile: ' + dataFile);
if(dataFile.startsWith('.\\'))
{
vo.log('dataFile starts with .\\: ');
dataFile = dataFile.replace('.\\', directoryName + '\\');
}
var data = require(dataFile);
collection.insertMany(data).then(function(data) {
console.log(data)
vo.log('Completed data file ' + dataFile);
}, errHandler);
})
db.close();
console.log(results);
}).catch((err)=> {
console.log(err.stack);
})
vo.log('Completed configuration file ' + configFile);
})