Last active
December 23, 2015 13:49
-
-
Save nanusdad/6644288 to your computer and use it in GitHub Desktop.
Using npm package mssql to query MS SQL db and generate JS code for mongo update load
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sql = require('mssql'); | |
//npm install tedious | |
//npm install mssql | |
config = { | |
driver: 'tedious', | |
server: 'db_server', | |
user: 'user', | |
password: 'password', | |
port: '1433', | |
options: { | |
debug: { | |
packet: true, | |
data: true, | |
payload: true, | |
token: false, | |
log: true | |
} | |
} | |
}; | |
var tblflds = [{ | |
table: "test_table", | |
field: "*" | |
}]; | |
sql.connect(config, function(err) { | |
// Query | |
var request = new sql.Request(); | |
tblflds.forEach(function(element) { | |
var sql = 'select ' + element.field + 'from ' + element.table; | |
//console.log(sql); | |
request.query(sql, function(err, recordset) { | |
if (err) { | |
console.log(err); | |
} else { | |
//console.dir(recordset); | |
recordset.forEach(function(element) { | |
// generate mongo update string with upsert true | |
var st_sql = | |
'db.' + element.table + '.update( { name: "' + | |
element.name + '" },' + | |
JSON.stringify(element) + | |
', { upsert: true });'; | |
console.log(st_sql); // redirect this to output file | |
}); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment