Search code examples
javascriptmysqlnode.jspool

How can I create a Node js MySQL pool with getconnection method


I have just stated learning node js, I have this piece of code so far.

//Create a mysql connection pool
var pool = mysql.createPool({
    connectionLimit : 100, //important
    host     : '127.0.0.1',
    user     : 'root',
    password : '',
    database : 'nodetuts',
    debug    :  false
});

// retrieve database connection
var connection = function(callback) {
    pool.getConnection(function(err, conn){
        if (err) {
            console.log(err);
            return;
        } 
        console.log('connected');
        callback(err, conn);
        
    });
};

//create tabase tables
connection.getConnection(function(err, conn){
    if (!err){
        console.log("Connected!");
        var rl = readline.createInterface({
            input: fs.createReadStream('struct.sql'),
            terminal: false
        });
        rl.on('line', function(chunk){
            conn.query(chunk.toString('ascii'), function(err, sets, fields){
                if(err){
                    console.log(err);
                }else{
                    console.log("Table created");
                }
            });
        });
    }
});
module.exports = connection;

I want create tables function to run within this module but use the same for connection in other modules.

Create table function generates an error,

Any one who can help I will appreciate.


Solution

  • I was playing with the piece of code and I came up with this.

    var mysql      = require('mysql');
    var fs         = require('fs');
    var readline   = require('readline');
    
    //Create a mysql connection pool
    var pool = mysql.createPool({
        connectionLimit : 100, //important
        host     : '127.0.0.1',
        user     : 'root',
        password : '',
        database : 'nodetuts',
        debug    :  false
    });
    
    var saveConnection = pool.getConnection(function(err,conn){
        if (err) {
            console.log(err);
            return;
        }
    
        //create tables
        var createTables = function(conn){
            connection.release();
            var rl = readline.createInterface({
                input: fs.createReadStream('struct.sql'),
                terminal: false
            });
            rl.on('line', function(chunk){
                conn.query(chunk.toString('ascii'), function(err, sets, fields){
                    if(err){
                        console.log(err);
                    }else{
                        console.log("Table created");
                    }
                });
            });
        }
    
        var findByPrimaryKey = connection.query("select * from user",function(err,rows){
            connection.release();
            if(!err) {
                res.json(rows);
            }          
        });
    
        conn.on('error', function(err) {      
            console.log(err);
            return;    
        });
        console.log('connected');
    });
    
    module.exports = saveConnection;
    

    Now my question is: Can I use methode bellow to access createTables function

    saveConnection.createTables();