Advertisement
Guest User

multiple config ConnectionPool

a guest
Nov 3rd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const config = {
  2.     nConnection:'conexion1',
  3.     user: 'usuario_node',
  4.     password: 'node123',
  5.     server: '127.0.0.1', // You can use 'localhost\\instance' to connect to named instance
  6.     database: 'pruebas_node',
  7.     port: '1433',
  8.     connectionTimeout:1000 ,
  9.     options: {
  10.         trustedConnection: true
  11.     },
  12.     parseJSON: true
  13. }
  14. const configs = {principal:{nConnection:'principal',
  15.                         user: 'usuario_node',
  16.                         password: 'node123',
  17.                         server: '127.0.0.1', // You can use 'localhost\\instance' to connect to named instance
  18.                         database: 'pruebas_node',
  19.                         port: '1433',
  20.                         connectionTimeout:1000 ,
  21.                         options: {
  22.                             trustedConnection: true
  23.                         },
  24.                         parseJSON: true
  25.                     },
  26.                     secundaria:{
  27.                     nConnection:'secundaria',
  28.                     user: 'usuario_node',
  29.                     password: 'node123',
  30.                     server: '127.0.0.1', // You can use 'localhost\\instance' to connect to named instance
  31.                     database: 'pruebas  ',
  32.                     port: '1433',
  33.                     connectionTimeout:1000 ,
  34.                     options: {
  35.                         trustedConnection: true
  36.                     },
  37.                     parseJSON: true
  38.                 }};
  39.  
  40. var sql = require('mssql')
  41. function getConnectionPoolGlobal(config){
  42.     function Connect(resolve,reject){
  43.         if (global.poolGlobal[config.nConnection]){
  44.             console.log('Reutilizando pool '+config.nConnection+','+poolGlobal[config.nConnection].listenerCount())
  45.            // console.log(poolGlobal[config.nConnection])
  46.             resolve(poolGlobal[config.nConnection])
  47.         }
  48.         else{
  49.         new sql.ConnectionPool(config).connect().then(poolObt=>{
  50.             global.poolGlobal[config.nConnection]=poolObt
  51.             resolve(global.poolGlobal[config.nConnection])
  52.             console.log('Nuevo pool Creado')
  53.         }).catch(err =>{
  54.             reject(err)
  55.         })
  56.         }      
  57.     }
  58.     return new Promise(Connect)
  59. }
  60. module.exports={
  61.     config,
  62.     getConnectionPoolGlobal,
  63.     configs
  64. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement