如何为我的整个Node.js应用程序使用相同的MySQL连接?

时间:2023-01-13 12:12:12

I have an app.js. I run my entire app from there.

我有一个app.js.我从那里运行我的整个应用程序。

Inside app.js, I require many files that have code in it.

在app.js中,我需要许多包含代码的文件。

For each of these files, I do this:

对于这些文件中的每一个,我这样做:

var mysql = require('mysql');
var mclient = mysql.createConnection({
    host: settings.MYSQL.HOST,
    user: settings.MYSQL.USER,
    password: settings.MYSQL.PASSWORD,
    database: settings.MYSQL.DB,
});

Essentially, I am initiating a new connection for every file.

基本上,我正在为每个文件启动一个新连接。

I want my app.js to make ONE connection, and then pass it to every file during the require line. How can I pass the connection to them so those files can use it?

我希望我的app.js建立一个连接,然后在require行期间将其传递给每个文件。如何将连接传递给它们,以便这些文件可以使用它?

(Or how can I pass a pool of connections to them?)

(或者我如何将连接池传递给他们?)

2 个解决方案

#1


7  

You can create a separate module, call it mysqlLib.js that will be responsible for creating a pool and returning connections:

您可以创建一个单独的模块,将其命名为mysqlLib.js,它将负责创建池并返回连接:

var mysql = require("mysql");
var pool = mysql.createPool(/* credentials go here */);

exports.getConnection = function(callback) {
  pool.getConnection(function(err, conn) {
    if(err) {
      return callback(err);
    }
    callback(err, conn);
  });
};

and in any module/file that needs a mysql connection, you can do this:

在任何需要mysql连接的模块/文件中,您可以这样做:

var mysqlLib = require("mysqlLib");

mysqlLib.getConnection(function(err, mclient) {
  //do queries that you need
});

The way require() works, the code in mysqlLib.js will only be run once so only one pool will be created even if require("mysqlLib.js"} is called in multiple files. See this section of the node.js docs for an explanation of module cacheing.

require()的工作方式,mysqlLib.js中的代码只运行一次,因此即使在多个文件中调用require(“mysqlLib.js”},也只会创建一个池。请参阅node.js文档的这一部分有关模块缓存的说明。

#2


1  

Yes you can use the mclient to attach your query

是的,您可以使用mclient附加查询

var mysqlLib = require("mysqlLib");

mysqlLib.getConnection(function(err, mclient) {
  //do queries that you need
  var sql = '//Your SQL here';
  var inserts = [your inserts here];
  sql = mysql.format(sql, inserts);
  mclient.query(sql, function (err, rows) {

  if (err) {
                //handle error here
            }

  })
});

#1


7  

You can create a separate module, call it mysqlLib.js that will be responsible for creating a pool and returning connections:

您可以创建一个单独的模块,将其命名为mysqlLib.js,它将负责创建池并返回连接:

var mysql = require("mysql");
var pool = mysql.createPool(/* credentials go here */);

exports.getConnection = function(callback) {
  pool.getConnection(function(err, conn) {
    if(err) {
      return callback(err);
    }
    callback(err, conn);
  });
};

and in any module/file that needs a mysql connection, you can do this:

在任何需要mysql连接的模块/文件中,您可以这样做:

var mysqlLib = require("mysqlLib");

mysqlLib.getConnection(function(err, mclient) {
  //do queries that you need
});

The way require() works, the code in mysqlLib.js will only be run once so only one pool will be created even if require("mysqlLib.js"} is called in multiple files. See this section of the node.js docs for an explanation of module cacheing.

require()的工作方式,mysqlLib.js中的代码只运行一次,因此即使在多个文件中调用require(“mysqlLib.js”},也只会创建一个池。请参阅node.js文档的这一部分有关模块缓存的说明。

#2


1  

Yes you can use the mclient to attach your query

是的,您可以使用mclient附加查询

var mysqlLib = require("mysqlLib");

mysqlLib.getConnection(function(err, mclient) {
  //do queries that you need
  var sql = '//Your SQL here';
  var inserts = [your inserts here];
  sql = mysql.format(sql, inserts);
  mclient.query(sql, function (err, rows) {

  if (err) {
                //handle error here
            }

  })
});