sql server - Commonize mssql connection -
i'm using mssql on node.js.
i want commonize mssql connection.
i summarize connections generated each time multiple modules 1 file
could tell me way?
example
[table1.js]
var mssql = require('mssql'); var config = { user: 'user', password: 'pass', server: 'server', database: 'db', stream: true, options: { encrypt: true } } mssql.connect(config, function(err) { var request = new mssql.request(); request.query('select * table1',function(err,data) { callback(data); mssql.close(); }); });
[table2.js]
var mssql = require('mssql'); var config = { user: 'user', password: 'pass', server: 'server', database: 'db', stream: true, options: { encrypt: true } } mssql.connect(config, function(err) { var request = new mssql.request(); request.query('select * table2',function(err,data) { callback(data); mssql.close(); }); });
anytime need things config/ database connections, wrap modules in
module.exports = function (db) { // code table1 or table 2 }
now need central place, can app.js / server.js
, connection params config file , require('./path/to/table')(db)
app.js
const db = { 'mssql': { user: 'user', password: 'pass', server: 'server', database: 'db', stream: true, options: { encrypt: true } }
table1.js
module.exports = function(db) { const mssql = require('mssql'); mssql.connect(db.mssql, function (err) { // ever }); }
wiki
Comments
Post a Comment