0

I have a file (db_slave.js) that looks like this:

var mysql = require('mysql')
var db = require('./db')

var conn = mysql.createConnection({
    host: db.host,
    user: db.user,
    password: db.password
});

conn.connect(function (err) {
    if (err) throw err;
    console.log("Connected to db");
});

module.exports = conn;

so I want one of my files to have a permanent connection to the db because many other files will request this file to execute queries. my question is though, when other files call this file, will this file make new DB connections or only keep this one conn variable?

The other files would call this file like this:

var sql = require('./db_slave')

sql.query("select 2", function (err, result) {
    if (err) console.log(err)
    console.log(result)
});

so are there multiple instances of the DB_slave or multiple connections?

Is there a better way to do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user10111623
  • 11
  • 1
  • 5

3 Answers3

0

This should be fine. After the first require of db_slave, future requires will use a cached version, and not re-run the module code. See this answer for more info.

Alan Friedman
  • 1,582
  • 9
  • 7
0

NodeJS is sort of a factorial environment and so, the module's code is executed only at his first require call. All the other calls are using the result of that first execution, which is the objects that were exported. So in your code the same connection will be used everywhere you require this module.

Shl
  • 3,130
  • 1
  • 17
  • 16
-1

Modules in node.js are cached. So, the first time they are require() ed in, they are loaded and the initialization code in the module is run. Whatever is assigned to module.exports is then cached and subsequent times the module is require() ed in again, the exports object is just fetched from the module cache and your module code is not run again. So, that will work for creating and sharing one database connection.

There are a couple problems with this design that make it not really work in the real world.

  1. If you have an error creating a connection to your database, you have no way of communicating that error back to any of the users of the database (to presumably do something intelligent).

  2. The callers have no way of knowing when the connection is ready to be used (the connect event is only internal).

In modern node.js what would likely make more sense is to export a promise that resolves when the connect succeeds and rejects if there's a connect error and then every time someone requires in that module, they will get access to the promise which they can use .then() to get the db connection and .catch() to get an error.

For example:

var mysql = require('mysql-promise')
var db = require('./db')

module.exports = mysql.createConnection({
    host: db.host,
    user: db.user,
    password: db.password
}).then(conn => {
    console.log("connected to the DB");
    return conn;
}).catch(err => {
    console.log("error connecting to the DB", err);
    throw err;
});

Usage:

require('./db_slave').then(conn => {
    return conn.query("select 2").then(result => {
        console.log(result);
    });
}).catch(err => {
    // handle error here
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979