У меня типа так:
const sql = require('mssql');
let _instance = null;
class AppSql{
constructor(){
const sqlConfig = {
user: config.sql.user,
password: config.sql.password,
database: config.sql.database,
server: config.sql.server,
domain: config.sql.domain,
parseJSON: config.sql.parseJSON,
options: {
trustedConnection: config.sql.trustedConnection,
}
};
this._pool = null;
(async () => {
try {
this._pool = await new sql.ConnectionPool(sqlConfig).connect();
this._pool.on('error', (err) => {
LOG.logError( MSG.fgetMsg('eSqlCommon', err.stack) );
})
LOG.logInfo( MSG.getMsg('iSqlConnected') );
} catch (err) {
LOG.logError( 'AppSql.constructor. \n' + MSG.fgetMsg('eSqlConnect', err.stack) );
}
})();
}
get pool(){
return this._pool;
}
async querySimple(q, params){
await this._pool;
try {
const request = this._pool.request();
if(params){
for (const key in params) {
const value = params[key];
request.input(key, value);
}
}
const result = await request.query(q);
return Promise.resolve(result);
} catch (err) {
console.log(q, err);
return Promise.reject(err);
}
}
}
sql.on('error', (err) => {
LOG.logError( 'SQL Emitter.\n' + MSG.fgetMsg('eSqlCommon', err.stack) );
});
// Экспорт инстанса
module.exports = {
getInstance: () => {
return _instance || (_instance = new AppSql());
}
};