Created
June 14, 2018 06:13
-
-
Save christiaanwesterbeek/57eabe43c81afc62fa323e708c507650 to your computer and use it in GitHub Desktop.
use a single mssql connection pool across several files using a promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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