Skip to content

Instantly share code, notes, and snippets.

@christiaanwesterbeek
Created June 14, 2018 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiaanwesterbeek/57eabe43c81afc62fa323e708c507650 to your computer and use it in GitHub Desktop.
Save christiaanwesterbeek/57eabe43c81afc62fa323e708c507650 to your computer and use it in GitHub Desktop.
use a single mssql connection pool across several files using a promise
const sql = require('mssql')
const config = {}
const pool = new sql.ConnectionPool(config)
.connect()
.then(connPool => {
console.log('Connected to MSSQL')
return connPool
})
.catch(err => console.log('Database Connection Failed! Bad Config: ', err))
module.exports = {
sql, pool
}
const { pool, sql } = require('./db')
return pool.then(conn => {
const ps = new sql.PreparedStatement(conn)
ps.input('xxxx', sql.VarChar)
return ps.prepare(`SELECT * from table where xxxx = @xxxx`)
.then(data => ps.execute({ xxxx: 'xxxx' }))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment