connection-manager.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  6. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  7. var __spreadValues = (a, b) => {
  8. for (var prop in b || (b = {}))
  9. if (__hasOwnProp.call(b, prop))
  10. __defNormalProp(a, prop, b[prop]);
  11. if (__getOwnPropSymbols)
  12. for (var prop of __getOwnPropSymbols(b)) {
  13. if (__propIsEnum.call(b, prop))
  14. __defNormalProp(a, prop, b[prop]);
  15. }
  16. return a;
  17. };
  18. const semver = require("semver");
  19. const AbstractConnectionManager = require("../abstract/connection-manager");
  20. const SequelizeErrors = require("../../errors");
  21. const { logger } = require("../../utils/logger");
  22. const DataTypes = require("../../data-types").mariadb;
  23. const momentTz = require("moment-timezone");
  24. const debug = logger.debugContext("connection:mariadb");
  25. const parserStore = require("../parserStore")("mariadb");
  26. class ConnectionManager extends AbstractConnectionManager {
  27. constructor(dialect, sequelize) {
  28. sequelize.config.port = sequelize.config.port || 3306;
  29. super(dialect, sequelize);
  30. this.lib = this._loadDialectModule("mariadb");
  31. this.refreshTypeParser(DataTypes);
  32. }
  33. static _typecast(field, next) {
  34. if (parserStore.get(field.type)) {
  35. return parserStore.get(field.type)(field, this.sequelize.options, next);
  36. }
  37. return next();
  38. }
  39. _refreshTypeParser(dataType) {
  40. parserStore.refresh(dataType);
  41. }
  42. _clearTypeParser() {
  43. parserStore.clear();
  44. }
  45. async connect(config) {
  46. let tzOffset = this.sequelize.options.timezone;
  47. tzOffset = /\//.test(tzOffset) ? momentTz.tz(tzOffset).format("Z") : tzOffset;
  48. const connectionConfig = __spreadValues({
  49. host: config.host,
  50. port: config.port,
  51. user: config.username,
  52. password: config.password,
  53. database: config.database,
  54. timezone: tzOffset,
  55. typeCast: ConnectionManager._typecast.bind(this),
  56. bigNumberStrings: false,
  57. supportBigNumbers: true,
  58. foundRows: false
  59. }, config.dialectOptions);
  60. if (!this.sequelize.config.keepDefaultTimezone) {
  61. if (connectionConfig.initSql) {
  62. if (!Array.isArray(connectionConfig.initSql)) {
  63. connectionConfig.initSql = [connectionConfig.initSql];
  64. }
  65. connectionConfig.initSql.push(`SET time_zone = '${tzOffset}'`);
  66. } else {
  67. connectionConfig.initSql = `SET time_zone = '${tzOffset}'`;
  68. }
  69. }
  70. try {
  71. const connection = await this.lib.createConnection(connectionConfig);
  72. this.sequelize.options.databaseVersion = semver.coerce(connection.serverVersion()).version;
  73. debug("connection acquired");
  74. connection.on("error", (error) => {
  75. switch (error.code) {
  76. case "ESOCKET":
  77. case "ECONNRESET":
  78. case "EPIPE":
  79. case "PROTOCOL_CONNECTION_LOST":
  80. this.pool.destroy(connection);
  81. }
  82. });
  83. return connection;
  84. } catch (err) {
  85. switch (err.code) {
  86. case "ECONNREFUSED":
  87. throw new SequelizeErrors.ConnectionRefusedError(err);
  88. case "ER_ACCESS_DENIED_ERROR":
  89. case "ER_ACCESS_DENIED_NO_PASSWORD_ERROR":
  90. throw new SequelizeErrors.AccessDeniedError(err);
  91. case "ENOTFOUND":
  92. throw new SequelizeErrors.HostNotFoundError(err);
  93. case "EHOSTUNREACH":
  94. case "ENETUNREACH":
  95. case "EADDRNOTAVAIL":
  96. throw new SequelizeErrors.HostNotReachableError(err);
  97. case "EINVAL":
  98. throw new SequelizeErrors.InvalidConnectionError(err);
  99. default:
  100. throw new SequelizeErrors.ConnectionError(err);
  101. }
  102. }
  103. }
  104. async disconnect(connection) {
  105. if (!connection.isValid()) {
  106. debug("connection tried to disconnect but was already at CLOSED state");
  107. return;
  108. }
  109. return await connection.end();
  110. }
  111. validate(connection) {
  112. return connection && connection.isValid();
  113. }
  114. }
  115. module.exports = ConnectionManager;
  116. module.exports.ConnectionManager = ConnectionManager;
  117. module.exports.default = ConnectionManager;
  118. //# sourceMappingURL=connection-manager.js.map