Last active
October 4, 2019 09:58
-
-
Save sdejean28/4d2d5116bd6e9dda1793a87110859552 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
/* | |
MICROSOFT SQL | |
*/ | |
var sql = require(‘mssql’); | |
var notifier = require(‘./notifier.js’) | |
var config = { | |
user: “username”, | |
password: “Pass@word”, | |
server: ‘mssql’, | |
port : 1433, | |
connectionTimeout : 10000 | |
} | |
var poolPromise; | |
getPool = () => { | |
return poolPromise; | |
} | |
const limit_reconnect = 2; /* or -1 for infinite */ | |
const delay_reconnect = 1000; // delay in milliseconds between multiple tries | |
var reconnect = 0; | |
notifier.on(‘mssql_connect’, () => { | |
new sql.ConnectionPool(config).connect() | |
.then(pool => { | |
poolPromise = pool; | |
notifier.emit(‘mssql_connected’); | |
}) | |
.catch(err => { | |
notifier.emit(‘mssql_not_connected’); | |
}); | |
}); | |
notifier.on(‘mssql_not_connected’, () => { | |
reconnect++; | |
if (limit_reconnect==-1 || reconnect<limit_reconnect) { | |
setTimeout(function () { | |
notifier.emit(‘mssql_connect’); | |
}, delay_reconnect) | |
} | |
else { | |
notifier.emit(‘mssql_connect_error’,‘failed to connect after ‘+limit_reconnect+’ tries …’); | |
} | |
}); | |
// startup — first try | |
notifier.emit(‘mssql_connect’); // this also could be done in the main application … | |
module.exports = { | |
sql, getPool | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment