connection-manager.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. const AbstractConnectionManager = require("../abstract/connection-manager");
  3. const sequelizeErrors = require("../../errors");
  4. const { logger } = require("../../utils/logger");
  5. const DataTypes = require("../../data-types").db2;
  6. const debug = logger.debugContext("connection:db2");
  7. const parserStore = require("../parserStore")("db2");
  8. class ConnectionManager extends AbstractConnectionManager {
  9. constructor(dialect, sequelize) {
  10. sequelize.config.port = sequelize.config.port || 3306;
  11. super(dialect, sequelize);
  12. this.lib = this._loadDialectModule("ibm_db");
  13. this.refreshTypeParser(DataTypes);
  14. }
  15. static _typecast(field, next) {
  16. if (parserStore.get(field.type)) {
  17. return parserStore.get(field.type)(field, this.sequelize.options, next);
  18. }
  19. return next();
  20. }
  21. _refreshTypeParser(dataType) {
  22. parserStore.refresh(dataType);
  23. }
  24. _clearTypeParser() {
  25. parserStore.clear();
  26. }
  27. async connect(config) {
  28. const connectionConfig = {
  29. database: config.database,
  30. hostname: config.host,
  31. port: config.port,
  32. uid: config.username,
  33. pwd: config.password
  34. };
  35. if (config.ssl) {
  36. connectionConfig["security"] = config.ssl;
  37. }
  38. if (config.sslcertificate) {
  39. connectionConfig["SSLServerCertificate"] = config.sslcertificate;
  40. }
  41. if (config.dialectOptions) {
  42. for (const key of Object.keys(config.dialectOptions)) {
  43. connectionConfig[key] = config.dialectOptions[key];
  44. }
  45. }
  46. try {
  47. const connection = await new Promise((resolve, reject) => {
  48. const connection2 = new this.lib.Database();
  49. connection2.lib = this.lib;
  50. connection2.open(connectionConfig, (error) => {
  51. if (error) {
  52. if (error.message && error.message.includes("SQL30081N")) {
  53. return reject(new sequelizeErrors.ConnectionRefusedError(error));
  54. }
  55. return reject(new sequelizeErrors.ConnectionError(error));
  56. }
  57. return resolve(connection2);
  58. });
  59. });
  60. return connection;
  61. } catch (err) {
  62. throw new sequelizeErrors.ConnectionError(err);
  63. }
  64. }
  65. disconnect(connection) {
  66. if (connection.connected) {
  67. connection.close((error) => {
  68. if (error) {
  69. debug(error);
  70. } else {
  71. debug("connection closed");
  72. }
  73. });
  74. }
  75. return Promise.resolve();
  76. }
  77. validate(connection) {
  78. return connection && connection.connected;
  79. }
  80. _disconnect(connection) {
  81. return this.dialect.connectionManager.disconnect(connection);
  82. }
  83. }
  84. module.exports = ConnectionManager;
  85. module.exports.ConnectionManager = ConnectionManager;
  86. module.exports.default = ConnectionManager;
  87. //# sourceMappingURL=connection-manager.js.map