import MSSQL from 'msnodesqlv8' import { HttpError } from 'flaska' export function initPool(core, config) { let pool = new MSSQL.Pool(config) core.log.info(config, 'MSSQL database setttings') pool.on('open', function() { core.log.info('MSSQL connection open') }) let waiting = false /*pool.on('error', function(error) { if (error.length) { let msg = 'Error in MSSQL pool\n => ' + error[0].message.trim() for (let i = 1; i < error.length; i++) { msg += '\n => ' + error[i].message.trim() } core.log.error(msg) } else { core.log.error('Error in MSSQL pool') core.log.error(error) } if (waiting) { return } core.log.warn('Attempting to connect again in 5 seconds') waiting = true setTimeout(function() { waiting = false console.log('opening') pool.open() console.log('done') }, 5000) })*/ core.log.info('Attempting to connect to MSSQL server') pool.open() // const sp = pool.procedureMgr() return { safeCallProc: function(name, params, options) { if (name.indexOf('.') < 0) { name = 'common.' + name } return pool.promises.callProc(name, params, options) .catch(function(err) { let message = err.message.replace(/\[[^\]]+\]/g, '') if (err.code > 50000) { if (err.code === 51001) { throw new HttpError(403, message) } throw new HttpError(422, message) } if (err.lineNumber && err.procName) { message = `Error at ${err.procName}:${err.lineNumber} => ${message}` } throw new HttpError(500, message) }) }, promises: pool.promises, } }