coopernurse / node-pool

Generic resource pooling for node.js
2.38k stars 259 forks source link

TypeError: Client is not a function #136

Closed sbefort closed 8 years ago

sbefort commented 8 years ago

When I attempt the Step 1 example in the docs I get this exception:

Caught exception: TypeError: Client is not a function
TypeError: Client is not a function
    at Object.Pool.create (/Users/nomad/Web/mysite/db.js:15:11)
    at Pool._createResource (/Users/nomad/Web/mysite/node_modules/generic-pool/lib/generic-pool.js:324:17)
    at Pool._ensureMinimum (/Users/nomad/Web/mysite/node_modules/generic-pool/lib/generic-pool.js:362:12)
    at new Pool (/Users/nomad/Web/mysite/node_modules/generic-pool/lib/generic-pool.js:156:8)
    at Object.<anonymous> (/Users/nomad/Web/mysite/db.js:10:12)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
sandfox commented 8 years ago

Thanks for reporting this, it's not a good thing when the examples don't work :-)
Seems like the mysql driver has changed it's API signature since when this snippet was created.

I quickly hacked it about and

// Create a MySQL connection pool with
// a max of 10 connections, a min of 2, and a 30 second max idle time
var Pool = require('generic-pool').Pool;
var mysql = require('mysql');

var pool = new Pool({
    name     : 'mysql',
    create   : function(callback) {
        var c = mysql.createConnection({
                user: 'scott',
                password: 'tiger',
                database:'mydb'
        })

        // parameter order: err, resource
        callback(null, c);
    },
    destroy  : function(client) { client.end(); },
    max      : 10,
    // optional. if you set this, make sure to drain() (see step 3)
    min      : 2,
    // specifies how long a resource can stay idle in pool before being removed
    idleTimeoutMillis : 30000,
     // if true, logs via console.log - can also be a function
    log : true
});

should work with the current (2.10.2) mysql version

Happy to accept a PR with this or I'll do it later when I get the time.

also going to link this to #117 as there is a need to improve the docs in general and this a great example of why!