-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
40 lines (37 loc) · 1.43 KB
/
index.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
var util = require("./lib/util");
var fs = require("fs");
var sql = require('mssql');
module.exports = function (dbconfig, options) {
options = util.updateOptions(options);
try {
if (fs.existsSync(options.outputDirectory) == false) {
fs.mkdirSync(options.outputDirectory);
}
}
catch (err) {
return Promise.reject(err);
}
options.log && console.log("Connecting to the database:", dbconfig.database, "...");
return sql.connect(dbconfig).then(function () {
if (options.tables.length == 0) {
options.log && console.log("Fetching list of tables from DB", "...");
return util.getTableList(options.tables, options.ignoreList);
}
})
.then(function () {
options.log && console.log("Starting DB export from", dbconfig.database, "...");
var exportPromise = options.tables.map(function (table) {
return util.exportTable(table.name, options.outputDirectory).then(function () {
options.log && console.log(table.name + " CSV file exported!");
});
});
return Promise.all(exportPromise).then(function () {
options.log && console.log("All tables have been exported to:", options.outputDirectory);
sql.close();
});
})
.catch(function (err) {
sql.close();
throw (err);
});
}