2022-06-16 22:44:43 +00:00
|
|
|
import MSSQL from 'msnodesqlv8'
|
|
|
|
import { HttpError } from 'flaska'
|
2023-11-14 07:26:54 +00:00
|
|
|
import { HttpErrorInternal } from './error.mjs'
|
2022-06-16 22:44:43 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
|
|
|
core.log.info('Attempting to connect to MSSQL server')
|
|
|
|
pool.open()
|
|
|
|
|
2022-06-28 08:15:51 +00:00
|
|
|
// const sp = pool.procedureMgr()
|
|
|
|
|
2022-06-16 22:44:43 +00:00
|
|
|
return {
|
|
|
|
safeCallProc: function(name, params, options) {
|
2022-07-20 00:33:06 +00:00
|
|
|
if (name.indexOf('.') < 0) {
|
2022-07-27 08:41:18 +00:00
|
|
|
name = 'common.' + name
|
2022-07-20 00:33:06 +00:00
|
|
|
}
|
|
|
|
return pool.promises.callProc(name, params, options)
|
2022-06-16 22:44:43 +00:00
|
|
|
.catch(function(err) {
|
2022-07-20 00:33:06 +00:00
|
|
|
let message = err.message.replace(/\[[^\]]+\]/g, '')
|
|
|
|
if (err.code > 50000) {
|
|
|
|
if (err.code === 51001) {
|
|
|
|
throw new HttpError(403, message)
|
|
|
|
}
|
|
|
|
throw new HttpError(422, message)
|
|
|
|
}
|
2022-06-16 22:44:43 +00:00
|
|
|
if (err.lineNumber && err.procName) {
|
|
|
|
message = `Error at ${err.procName}:${err.lineNumber} => ${message}`
|
|
|
|
}
|
2023-11-30 04:13:08 +00:00
|
|
|
throw new HttpErrorInternal(message, err, !err.lineNumber ? {
|
|
|
|
name,
|
|
|
|
params
|
|
|
|
} : null)
|
2022-06-16 22:44:43 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
promises: pool.promises,
|
|
|
|
}
|
|
|
|
}
|