bminer / node-mysql-queues

Wraps 'node-mysql' to provide mulitple query queues, allowing support for multiple statements and transactions.
MIT License
92 stars 11 forks source link

Error with node-mysql 2.0.0 - mysql has no method createClient #22

Closed danielabar closed 11 years ago

danielabar commented 11 years ago

I'm using node-mysql 2.0.0-alpha8 and can't get the sample code in the readme working regarding mysql.createClient

Running the code generates a has no method error. Is there a different syntax to use to get this working with node-mysql 2.0.0? (Note that mysql is otherwise working for me using the connection pool)

/Users/dbaron/projects/LEDITA/server/dao/Dao.js:6
var client = mysql.createClient({
                   ^
TypeError: Object #<Object> has no method 'createClient'
    at Object.<anonymous> (/Users/dbaron/projects/LEDITA/server/dao/Dao.js:6:20)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/dbaron/projects/LEDITA/server/dao/ActivityDao.js:2:11)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/dbaron/projects/LEDITA/server/service/ActivityService.js:1:81)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/dbaron/projects/LEDITA/server/tests/controllers/ActivityControllerSpec.js:4:25)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at /Users/dbaron/projects/LEDITA/node_modules/mocha/lib/mocha.js:152:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/dbaron/projects/LEDITA/node_modules/mocha/lib/mocha.js:149:14)
    at Mocha.run (/Users/dbaron/projects/LEDITA/node_modules/mocha/lib/mocha.js:305:31)
    at Object.<anonymous> (/Users/dbaron/projects/LEDITA/node_modules/mocha/bin/_mocha:327:7)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
bminer commented 11 years ago

Right. You need to use mysql.createConnection for node-mysql 2.0. I fixed the code sample in the README. Thanks!

danielabar commented 11 years ago

Thanks using mysql.createConnection got me past that error.

But now I'm having an issue where no inserts are happening. I turned on debugging for both this project on node-mysql, not getting any errors, but also not seeing any inserts.

Here is my code:

var client = mysql.createConnection({ 
    host: config.db_host,
    user: config.db_user,
    password: config.db_pswd,
    database: config.db_schema,
    connectionLimit: config.db_pool_connection_limit,
    debug: config.db_debug_sql
});
var queues = require('mysql-queues');
const DEBUG = true;
queues(client, DEBUG);

module.exports = {

  executeTrans: function(queryStrings, queryParams) {
    var trans = client.startTransaction();
    function error(err) {
      if(err && trans.rollback) {trans.rollback(); throw err;}
    }
    for(var i = 0; i < queryStrings.length; i++)
      trans.query(queryStrings[i], queryParams[i], error);
    trans.commit();
  },
 // other dao functions...

And here is the mocha test (not asserting anything at the moment, just using it to exercise the code, then manually checking in the database if any new records were inserted)

   it.only('Transaction support works', function() {
        var today = new Date();
        var query1 = 'insert into ld set ?';
        var queryParam1 = {
            "user_id": 1,
            "ld_model_id": null,
            "name": "LD Trans Test 1",
            "scope": "Lesson",
            "publication": 1,
            "students_profile": "SP Trans Test 1",
            "creation_date": today
        };
        var queryParam2 = {
            "user_id": 1,
            "ld_model_id": null,
            "name": "LD Trans Test 2",
            "scope": "Lesson",
            "publication": 1,
            "students_profile": "SP Trans Test 2",
            "creation_date": today
        };
        try {
            Dao.executeTrans([query1, query1], [queryParam1, queryParam2]);
        } catch (e) {
            console.log(e);     
        }
    });

And here is debug output:

<-- HandshakeInitializationPacket
{ protocolVersion: 10,
  serverVersion: '5.5.24',
  threadId: 42,
  scrambleBuff1: <Buffer 70 79 30 21 54 62 6f 29>,
  filler1: <Buffer 00>,
  serverCapabilities1: 63487,
  serverLanguage: 8,
  serverStatus: 2,
  serverCapabilities2: 32783,
  scrambleLength: 21,
  filler2: <Buffer 00 00 00 00 00 00 00 00 00 00>,
  scrambleBuff2: <Buffer 7a 4f 5a 58 29 60 2b 53 41 2a 4f 30>,
  filler3: <Buffer 00>,
  pluginData: 'mysql_native_password',
  protocol41: true }

--> ClientAuthenticationPacket
{ clientFlags: 193487,
  maxPacketSize: 0,
  charsetNumber: 33,
  filler: undefined,
  user: 'ledita',
  scrambleBuff: <Buffer 13 a1 78 c5 1e a7 c2 79 3b ab be d1 00 6c e5 21 ad be ba b1>,
  database: 'ledita-web-app',
  protocol41: true }

<-- OkPacket
{ fieldCount: 0,
  affectedRows: 0,
  insertId: 0,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0 }

--> ComQueryPacket
{ command: 3, sql: 'START TRANSACTION' }