不要怂,就是干,撸起袖子干!

connection-manager.js 1.86 KB
"use strict";
var ConnectionManager
  , Promise = require('../../promise');

ConnectionManager = function(dialect, sequelize) {
  try {
    this.lib = require(sequelize.config.dialectModulePath || 'mysql');
  } catch (err) {
    throw new Error('Please install mysql package manually');
  }
};

ConnectionManager.prototype.connect = function(config) {
  var self = this;
  return new Promise(function (resolve, reject) {
    var connectionConfig = {
      host: config.host,
      port: config.port,
      user: config.username,
      password: config.password,
      database: config.database,
      timezone: 'Z'
    };

    if (config.dialectOptions) {
      Object.keys(config.dialectOptions).forEach(function(key) {
        connectionConfig[key] = config.dialectOptions[key];
      });
    }

    var connection = self.lib.createConnection(connectionConfig);

    connection.connect(function(err) {
      if (err) {
        switch (err.code) {
        case 'ECONNREFUSED':
        case 'ER_ACCESS_D2ENIED_ERROR':
          reject('Failed to authenticate for MySQL. Please double check your settings.');
          break;
        case 'ENOTFOUND':
        case 'EHOSTUNREACH':
        case 'EINVAL':
          reject('Failed to find MySQL server. Please double check your settings.');
          break;
        default:
          reject(err);
          break;
        }

        return;
      }

      resolve(connection);
    });

  }).tap(function (connection) {
    connection.query("SET time_zone = '+0:00'");
  });
};
ConnectionManager.prototype.disconnect = function(connection) {
  return new Promise(function (resolve, reject) {
    connection.end(function(err) {
      if (err) return reject(err);
      resolve();
    });
  });
};
ConnectionManager.prototype.validate = function(connection) {
  return connection && connection.state !== 'disconnected';
};

module.exports = ConnectionManager;