rodrigogs / mysql-events

A node package that watches a MySQL database and runs callbacks on matched events.
BSD 3-Clause "New" or "Revised" License
136 stars 52 forks source link

Connection not being recognised #19

Open PatrickWalker opened 5 years ago

PatrickWalker commented 5 years ago

Seeing an intermittent error.

Passing a Mysql Lib (same version) Connection into EventHandler but when I start all the config has been dropped and it's trying to connect to localhost.

    this.mysqlEvents = mysqlEvents;
    this.mysqlconnection = mysql.createConnection({
      host: connection.host,
      user: connection.user,
      password: connection.password,
    });

    this.eventWatcher = new this.mysqlEvents(this.mysqlconnection, {
      startAtEnd: true,
      excludedSchemas: {
        mysql: true,
      },
    })
    this.eventWatcher.start();

Connection Error

2019-08-14T14:21:21.188574900Z 
(node:25) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
    --------------------
    at Protocol._enqueue (/src/node_modules/@rodrigogs/mysql-events/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/src/node_modules/@rodrigogs/mysql-events/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/src/node_modules/@rodrigogs/mysql-events/node_modules/mysql/lib/Connection.js:119:18)
    at Promise (/src/node_modules/@rodrigogs/mysql-events/lib/connectionHandler.js:6:75)
    at new Promise (<anonymous>)
    at connect (/src/node_modules/@rodrigogs/mysql-events/lib/connectionHandler.js:6:31)
    at connectionHandler (/src/node_modules/@rodrigogs/mysql-events/lib/connectionHandler.js:42:11)
    at MySQLEvents.start (/src/node_modules/@rodrigogs/mysql-events/lib/MySQLEvents.js:88:29)

This is because it's not recognizing the Connection as an instance of a connection but instead just as an object so the config is being defaulted again

Now weirdly when it prints out the connection as an object it looks like this

creating connection from object: Connection {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  config:
   ConnectionConfig {
     host: 'redacted',
     port: 3306,
     localAddress: undefined,
     socketPath: undefined,
     user: 'root',
     password: 'ourrootpassword',
     database: undefined,
     connectTimeout: 10000,
     insecureAuth: false,
     supportBigNumbers: false,
     bigNumberStrings: false,
     dateStrings: false,
     debug: undefined,
     trace: true,
     stringifyObjects: false,
     timezone: 'local',
     flags: '',
     queryFormat: undefined,
     pool: undefined,
     ssl: false,
     multipleStatements: false,
     typeCast: true,
     maxPacketSize: 0,
     charsetNumber: 33,
     clientFlags: 455631 },
  _socket: undefined,
  _protocol:
   Protocol {
     _events: [Object: null prototype] {},
     _eventsCount: 0,
     _maxListeners: undefined,
     readable: true,
     writable: true,
     _config:
      ConnectionConfig {
        host: 'redacted',
        port: 3306,
        localAddress: undefined,
        socketPath: undefined,
        user: 'root',
        password: 'ourrootpassword',
        database: undefined,
        connectTimeout: 10000,
        insecureAuth: false,
        supportBigNumbers: false,
        bigNumberStrings: false,
        dateStrings: false,
        debug: undefined,
        trace: true,
        stringifyObjects: false,
        timezone: 'local',
        flags: '',
        queryFormat: undefined,
        pool: undefined,
        ssl: false,
        multipleStatements: false,
        typeCast: true,
        maxPacketSize: 0,
        charsetNumber: 33,
        clientFlags: 455631 },
     _connection: [Circular],
     _callback: null,
     _fatalError: null,
     _quitSequence: null,
     _handshake: false,
     _handshaked: false,
     _ended: false,
     _destroyed: false,
     _queue: [],
     _handshakeInitializationPacket: null,
     _parser:
      Parser {
        _supportBigNumbers: false,
        _buffer: <Buffer >,
        _nextBuffers: [BufferList],
        _longPacketBuffers: [BufferList],
        _offset: 0,
        _packetEnd: null,
        _packetHeader: null,
        _packetOffset: null,
        _onError: [Function: bound handleParserError],
        _onPacket: [Function: bound ],
        _nextPacketNumber: 0,
        _encoding: 'utf-8',
        _paused: false } },
  _connectCalled: false,
  state: 'disconnected',
  threadId: null }

So not sure why it doesn't think it's an instance of connection. I had thought initially mysql lib versions meaning different interface but seem to both be the same "mysql": "^2.17.1",

In my code if I try to the same instanceof Connection check it passes but in the mysqlevents it does not.

Apologies if already seen.

Thanks for the library as well it's been great. I have a fix for this but not sure it's ideal. Will send a PR in later. Been unable to willfully recreate this as well but it has happened a few times. It's very odd.

rodrigogs commented 5 years ago

Thanks for the PR, I will have a look later