Skip to content

Commit

Permalink
Merge pull request #1 from sdemurjian/master
Browse files Browse the repository at this point in the history
Write properties from recordset to csvStream
  • Loading branch information
adnan-kamili authored Sep 21, 2016
2 parents 29c7208 + bf96d8e commit 777d6dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Node.js module to export MS SQL database to CSV
ignoreList: ["sysdiagrams"], // tables to ignore
tables: [], // empty to export all the tables
outputDirectory: 'somedir',
log: true
log: true,
header: false // true to export column names as csv header
};

mssqlExport(dbconfig, options).then(function(){
Expand Down
18 changes: 17 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var options = {
ignoreList: ["sysdiagrams"],
tables: [],
outputDirectory: '.',
log: true
log: true,
header: false
};

module.exports = {
Expand Down Expand Up @@ -35,6 +36,18 @@ module.exports = {
var request = new sql.Request();
request.stream = true;
request.query('select * from ' + tableName);

if (options.header) {
request.on('recordset', function (columns) {
var keys = [];
for (var key in columns) {
if (columns.hasOwnProperty(key)) {
keys.push(key);
}
}
csvStream.write(keys);
});
}
request.on('row', function (row) {
// fix for null values getting replaced with empty string
for (var key in row) {
Expand Down Expand Up @@ -68,6 +81,9 @@ module.exports = {
if(opts.log !== 'undefined') {
options.log = opts.log;
}
if(opts.header !== 'undefined') {
options.header = opts.header;
}
return options;
}
}

0 comments on commit 777d6dc

Please sign in to comment.