1

Currently trying to implement a different approach to connecting to my database using promises and pooling. This is what I have as of the moment:

// databaseConnection.js
var configDB = require('./database.js');
var mysql = require('promise-mysql');

var pool = mysql.createPool(configDB.connectionData);

function getSqlConnection() {
    return pool.getConnection(configDB.connectionData).disposer(function(connection) {
        connection.release();
    });
}

module.exports = getSqlConnection;

Then I use the query like this:

#sqlQuery.js
var Promise = require("bluebird");
var getSqlConnection = require('./databaseConnection')
Promise.using(getSqlConnection(), function(connection) {
    return connection.query("SELECT * FROM EXAMPLE_TABLE").then(function(row) {
        return process(rows);
    }
}

I'm using this library which is just node-mysql wrapped with BlueBird promises. With that, I wanted to take advantage of BlueBird's disposing and using capability so I would only be connected to the DB when I needed to be.

Currently though I'm getting an error from Connection.js of mysql stating: cb is not a function. Based on this question I have somewhat of an idea of what I'm doing wrong but I'm not sure how I would go about using that with BlueBird's dispose/using paradigm. Thanks in advance for anyone that can help!

Community
  • 1
  • 1
wootencl
  • 585
  • 4
  • 15
  • In your `sqlQuery.js` code you are calling `getSqlConnection()` - without passing the `cb` (callback?) argument. What is this `cb` used for? – ishmaelMakitla Aug 12 '16 at 07:19
  • Sorry about that. I copied that code from my modified source. What's up there now should be the original. I don't use a callback in my code. Nor does it mention requiring one in the `node-mysql` documentation – wootencl Aug 12 '16 at 13:06
  • OK, now since you do not have `cb` there anymore - are you still getting the same error message? – ishmaelMakitla Aug 12 '16 at 13:23
  • That is correct. Even more curious I just found this BlueBird example that seems to resemble mine: https://github.com/petkaantonov/bluebird/blob/master/docs/docs/working-with-callbacks.md#mysql – wootencl Aug 12 '16 at 13:26
  • Just figured it out. Going to post an answer. One moment – wootencl Aug 12 '16 at 13:29

1 Answers1

0

Huge lack of oversight on my part. The following line:

return pool.getConnection(configDB.connectionData).disposer...

should be:

return pool.getConnection().disposer...

Sorry about that. Still getting an error for connection.release not being a function which is strange but at least I can move forward with debugging that.

wootencl
  • 585
  • 4
  • 15