Hey. I have a database class that I would be instantiating an of object in my controllers. The idea is to have pooled connections.My question is, would I be creating multiple pools if I go about doing things like this where I create a pool connection in the class constructor and then have multiple calls to that in a (express)controller or even maybe another controller?I wrote this code when I was closing the connections and it worked fine but something tells me I would run into issues with pooled connection real quick.Hopefully you folks could give me some advice on this.dbpool.jsconst mysql = require( 'mysql' ); var config = { connectionLimit:100, host:'144.122.333.xxx', database:'somedb', user:'root', password:"blah", }; module.exports = class Database { constructor() { this.pool = mysql.createPool(config); } GetEmailData(){ return new Promise(async ( resolve, reject ) => { this.pool.getConnection(function(err, connection) { if (err) throw err; var query = `SELECT * FROM email`; connection.query(query, (err, rows, fields) =>{ if ( err ){ connection.release(); return reject( err ); } connection.release(); resolve(rows); } ); }); } ); } }; controller.js fileconst sql = require(`./dbpool.js`); const db = new sql(); db.GetEmailData().then((emailData)=>{ console.log(emailData); }).catch((err) => { });
Submitted November 13, 2018 at 01:47PM by _Reala_
No comments:
Post a Comment