Tuesday, 1 September 2020

Help structuring a new database connection

To start i want to switch my databases from Mongodb to KDB. Reasoning is because at work i have to use KDB so i want to get a good handle on it in my personal project(website) https://github.com/michaelwittig/node-q Above is the package i am using.Basically im looking for the cleanest/best practice when it comes to creating a DB connecting and requiring it in DB.The basic connection with nodeq looks like thisnodeq.connect({ host: "192.168.1.91", port: 5000 },(err,conn)=>{ if(err) throw err console.log("KDB Connected") }) I need to somehow get this instance of conn to be used all over. For example when i used mongoose it would seem that when i start the app it connected to mongodb then when i include mongoose in other files it would reuse the connection. Im assuming this is a singleton? Im looking for that same kind of result, connect once in app.js then just include a file to access the connection and call a query. Right now i have a DB.js file that looks like this.var nodeq = require("node-q"); class KDBAdaptor { constructor(conn){ this._con = conn; } query(sql) { return new Promise((resolve, reject) => { this._con.k(sql, (err, res) => { if (err) reject(err); resolve(res); }); }); } close() { this._con.close(); } } // module.exports = new KDBAdaptor(); module.exports = new Promise((resolve,reject)=>{ nodeq.connect({ host: "192.168.1.91", port: 5000 },(err,conn)=>{ if(err) reject(err) console.log("KDB Connected") resolve(new KDBAdaptor(conn)) }) }) It seems jest doesnt like this way and isnt allowing it to exit? Any advice is appreciated.

Submitted September 01, 2020 at 08:03PM by Codemonkey314

No comments:

Post a Comment