@rcronin/sequelize-ibmi-mapepire
Version:
IBM i (via Mapepire) Sequelize V7 Dialect
91 lines • 3.79 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.IBMiConnectionManager = void 0;
const Mapepire = __importStar(require("@ibm/mapepire-js"));
const core_1 = require("@sequelize/core");
const logger_js_1 = require("@sequelize/core/_non-semver-use-at-your-own-risk_/utils/logger.js");
const debug = logger_js_1.logger.debugContext('connection:ibmi');
class IBMiConnectionManager extends core_1.AbstractConnectionManager {
#lib;
constructor(dialect) {
super(dialect);
this.#lib = Mapepire;
}
async connect(config) {
const connection = (config.jdbcOptions ? new Mapepire.SQLJob(config.jdbcOptions) : new Mapepire.SQLJob());
try {
await connection.connect(config);
connection.connected = true;
}
catch (error) {
connection.connected = false;
debug('Error connecting to the database:', error);
void this.sequelize.pool.destroy(connection);
if (!(error instanceof Error)) {
throw error;
}
if (error.message.toString().includes('Error connecting to the database')) {
throw new core_1.ConnectionRefusedError(error);
}
if (error.message.toString().includes('getaddrinfo ENOTFOUND')) {
throw new core_1.HostNotFoundError(error);
}
if (error.message.toString().includes('connect ECONNREFUSED')) {
throw new core_1.ConnectionRefusedError(error);
}
if (error.message.toString().includes('Password is incorrect')) {
throw new core_1.AccessDeniedError(error);
}
if (error.message.toString().includes('connect ETIMEDOUT')) {
throw new core_1.ConnectionAcquireTimeoutError('Timeout connecting to database', error);
}
throw error;
}
return connection;
}
async disconnect(connection) {
if (!this.validate(connection)) {
debug('Tried to disconnect, but connection was already closed.');
return;
}
await connection.close();
}
validate(connection) {
return connection.connected && connection.getStatus() !== Mapepire.States.JobStatus.ENDED;
}
}
exports.IBMiConnectionManager = IBMiConnectionManager;
//# sourceMappingURL=connection-manager.js.map