* Connect with MariaDB database based on config, Handle any errors in connection * Set the pool handlers on connection.error * Also set proper timezone once connection is connected. * * @param {object} config * @returns {Promise<Connection>} * @private
(config)
| 51 | * @private |
| 52 | */ |
| 53 | async connect(config) { |
| 54 | // Named timezone is not supported in mariadb, convert to offset |
| 55 | let tzOffset = this.sequelize.options.timezone; |
| 56 | tzOffset = /\//.test(tzOffset) ? momentTz.tz(tzOffset).format('Z') |
| 57 | : tzOffset; |
| 58 | |
| 59 | const connectionConfig = { |
| 60 | host: config.host, |
| 61 | port: config.port, |
| 62 | user: config.username, |
| 63 | password: config.password, |
| 64 | database: config.database, |
| 65 | timezone: tzOffset, |
| 66 | typeCast: ConnectionManager._typecast.bind(this), |
| 67 | bigNumberStrings: false, |
| 68 | supportBigNumbers: true, |
| 69 | foundRows: false, |
| 70 | ...config.dialectOptions |
| 71 | }; |
| 72 | |
| 73 | if (!this.sequelize.config.keepDefaultTimezone) { |
| 74 | // set timezone for this connection |
| 75 | if (connectionConfig.initSql) { |
| 76 | if (!Array.isArray( |
| 77 | connectionConfig.initSql)) { |
| 78 | connectionConfig.initSql = [connectionConfig.initSql]; |
| 79 | } |
| 80 | connectionConfig.initSql.push(`SET time_zone = '${tzOffset}'`); |
| 81 | } else { |
| 82 | connectionConfig.initSql = `SET time_zone = '${tzOffset}'`; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | try { |
| 87 | const connection = await this.lib.createConnection(connectionConfig); |
| 88 | this.sequelize.options.databaseVersion = semver.coerce(connection.serverVersion()).version; |
| 89 | |
| 90 | debug('connection acquired'); |
| 91 | connection.on('error', error => { |
| 92 | switch (error.code) { |
| 93 | case 'ESOCKET': |
| 94 | case 'ECONNRESET': |
| 95 | case 'EPIPE': |
| 96 | case 'PROTOCOL_CONNECTION_LOST': |
| 97 | this.pool.destroy(connection); |
| 98 | } |
| 99 | }); |
| 100 | return connection; |
| 101 | } catch (err) { |
| 102 | switch (err.code) { |
| 103 | case 'ECONNREFUSED': |
| 104 | throw new SequelizeErrors.ConnectionRefusedError(err); |
| 105 | case 'ER_ACCESS_DENIED_ERROR': |
| 106 | case 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR': |
| 107 | throw new SequelizeErrors.AccessDeniedError(err); |
| 108 | case 'ENOTFOUND': |
| 109 | throw new SequelizeErrors.HostNotFoundError(err); |
| 110 | case 'EHOSTUNREACH': |