Skip to content

Instantly share code, notes, and snippets.

@michalmikolajczyk
Last active August 29, 2015 13:57
Show Gist options
  • Save michalmikolajczyk/9647468 to your computer and use it in GitHub Desktop.
Save michalmikolajczyk/9647468 to your computer and use it in GitHub Desktop.
// this is NodeJS connecting to SQL Server using tedious http://pekim.github.io/tedious/
// which supports only one active db input at a time
// So I run the connections through a helper functions that disables input until the request is done
// It also checks availability by running itself recursively.
//
// Used in backend NodeJS app for an AngularJS Angular UI Calendar (FullCalendar) app
//
//
// below excerpt fromsendMail.js module:
var connection = require('../public/javascripts/dbConn.js');
exports.simpleWait = var simpleWait = function(request) {
if (GLOBAL.wait) {
setTimeout(function() {
simpleWait(request);
}, 50);
return;
}
GLOBAL.wait = true;
connection.execSql(request);
};
//
// below excerpt from events.js module:
var Request = require('tedious').Request;
/*jshint multistr: true */
var request = new Request("INSERT INTO dbo.events \
VALUES('" + mapping.title + "', \
'" + mapping.startdt + "', \
'" + mapping.enddt + "', \
'" + mapping.category + "', \
'" + (mapping.coordinator ? mapping.coordinator.pid : "") + "', \
'" + mapping.description + "', \
'', \
'" + mapping.repeat + "', \
'0', \
'" + mapping.authorFirstName + "', \
'" + mapping.authorLastName + "', \
'" + mapping.authorPid + "', \
'" + mapping.datePublished + "', \
'" + mapping.address + "'\
)", function(err, rowCount, rows) {
if (err)
throw err;
// open db for new connections:
GLOBAL.wait = false;
if (rows) {
// business logic here
}
});
connection.simpleWait(request);
res.send('Yeah!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment