biggora / caminte

Cross-db ORM for NodeJS
http://www.camintejs.com/
MIT License
1.08k stars 119 forks source link

Mysql adapter models get reset after reconnection #191

Open raffaele-clevermind opened 6 years ago

raffaele-clevermind commented 6 years ago

when the client has the option "autoReconnect" set to true, the method "handleDisconnect" creates a new connection as shown in the code below:

function handleDisconnect(client) {
        schema.client.on('error', function (error) {
            if (error.code !== 'PROTOCOL_CONNECTION_LOST') throw error;
            console.log('> Re-connecting lost MySQL connection: ' + error.stack);
            if (s.pool) {
                schema.client = mysql.createPool(conSettings);
                schema.client.getConnection(function (err, connection) {
                    if (err) {
                        throw new Error(err);
                    }
                });
                schema.adapter = new MySQL(schema.client, conSettings);
                schema.adapter.schema = schema;
                schema.client.once('connection', function (connection) {
                    startAdapter(schema, dbName, callback);
                });
                handleDisconnect(schema.client);
            } else {
                schema.client = mysql.createConnection(conSettings);
                schema.adapter = new MySQL(schema.client, conSettings);
                schema.adapter.schema = schema;
                startAdapter(schema, dbName, callback);
                handleDisconnect(schema.client);
            }
        });
    }

When the adapter creates a new istance of the MySQL prototype it will reset the models structure:

function MySQL(client, conSettings) {
    'use strict';
    this.name = 'mysql';
    this._models = {};
    this.log = console.log;
    this.client = client;
    this.settings = conSettings;
}

This means the client won't be usable anymore unless you re-initialize all the models again manually.