Skip to content

Instantly share code, notes, and snippets.

@jazlalli
Last active December 12, 2015 10:09
Show Gist options
  • Save jazlalli/4757402 to your computer and use it in GitHub Desktop.
Save jazlalli/4757402 to your computer and use it in GitHub Desktop.
using node-sqlserver to connect to a database and execute a stored procedure
var environment = process.env.NODE_ENV || '';
var config = require('../config_' + environment),
sql = require('node-sqlserver'),
visit = {};
visit.log = function (message, callback) {
var visitparams = [],
encodedmsg = unescape(message.data),
msg = JSON.parse(encodedmsg);
sql.open(config.SQL.ConnectionString, function (error, conn) {
if (error) {
console.log('Unable to connect to remote SQL Server: ' + config.SQL.Server);
} else {
console.log('Connected to SQL Server: ' + config.SQL.Server);
// map data to array in the order the stored proc is expecting
visitparams.push(msg['visitguid'] || '');
visitparams.push(msg['url'] || '');
visitparams.push(msg['useragent'] || '');
visitparams.push(msg['referrer'] || '');
visitparams.push(msg['m'] || '');
visitparams.push(msg['cam'] || '');
visitparams.push(msg['k'] || '');
visitparams.push(msg['adg'] || '');
visitparams.push(msg['ver'] || '');
visitparams.push(msg['created'] || '');
// execute stored procedure to insert visit
conn.queryRaw('exec spInsertVisit ?,?,?,?,?,?,?,?,?,?', visitparams, function (error, results) {
if (error) {
console.log('Unable to store visit: ' + msg.visitguid);
} else {
console.log('Visit stored successfully!');
}
});
callback();
}
});
};
module.exports = visit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment