Created
June 16, 2015 20:00
-
-
Save myndzi/408a5e4125ca7082949c to your computer and use it in GitHub Desktop.
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
'use strict'; | |
var Promise = require('bluebird'); | |
var Connection = require('tedious').Connection ; | |
var Request = require('tedious').Request ; | |
var config = { | |
server: '152.16.26.58', | |
userName: 'sa', | |
password: '', | |
options: { database: 'bubba', | |
rowCollectionOnRequestCompletion: true | |
} | |
} ; | |
function executeStatement(conn, stmt) | |
{ | |
return conn.execSqlAsync(stmt) | |
.spread(function (rowCount, rows) { | |
console.log(rowCount + ' rows'); | |
jsonArray = [] | |
rows.forEach(function (columns) { | |
var rowObject ={}; | |
columns.forEach(function(column) { | |
console.log(column.value) ; | |
rowObject[column.metadata.colName] = column.value; | |
}); | |
jsonArray.push(rowObject) | |
}) ; | |
return jsonArray; | |
}); | |
} | |
function getConnection() | |
{ | |
var connection = new Connection(config); | |
return new Promise(function (resolve, reject) { | |
var connection = Promise.promisifyAll(new Connection(config)); | |
connection.on('connect', resolve.bind(null, null, connection)); | |
connection.on('error', reject.bind(null)); | |
connection.executeStatementAsync = executeStatement.bind(null, connection); | |
}).disposer(function (connection) { | |
return connection.close(); // connection.closeAsync if "connection.close" is an async function / takes a callback | |
}); | |
} | |
// actually do the things | |
Promise.using(getConnection(), function (connection) { | |
return Promise.try(function () { | |
return connection.executeStatementAsync('select last_name,first_name from background') | |
.tap(console.log); // log result | |
}).then(function () { | |
return connection.executeStatementAsync('select first_name from background') | |
.tap(console.log); // log result | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment