I'm currently writing a node.js project where I defined and instantiated a rethinkdb variable within the main ES6 class.
My question is what is the best method of sharing the database variable amongst all subsequent project classes without re-instantiating the same variable or making another db connection? A "Global" Variable of such.
Something like this the only way? Anyway to define it as a global?
const db = require('rethinkdb');
db.connect({
host: config.db.host,
port: config.db.port,
authKey: config.db.authkey,
ssl: {
ca: content
}
}, function(err, conn) {
if(err) {
fail(err)
} else {
success(conn)
}
});
new ClassName(db);
EDIT:
I could also consider making a DB class where the db variable is a singleton? Is that the best way?