UNPKG

mysql-rewrapped

Version:

A wrapper for MySQL on node to make querying a database as easy as possible

61 lines (60 loc) 2.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var mysql = require("mysql"); var ConnectionFailedError_1 = require("./Errors/ConnectionFailedError"); var Database = /** @class */ (function () { function Database(conf) { this.dbname = conf.database; this.connectionPool = mysql.createPool(conf); this.tables = {}; this.conf = conf; Database.db = this; } Database.prototype.meta = function (cback, counter) { var _this = this; if (counter === void 0) { counter = 0; } if (counter >= 600) { cback(new ConnectionFailedError_1.default("The connection was not able to be established in a timely manner")); } this.connectionPool.getConnection(function (err) { if (err) { setInterval(function () { _this.meta(cback, counter + 1); }, 50); } _this.connectionPool.query("SHOW TABLES;", function (error, results) { if (error) { setInterval(function () { _this.meta(cback, counter + 1); }, 50); } if (results !== undefined) { if (results.length === 0) { if (cback) { return cback(); } } results.forEach(function (table) { _this.tables[table["Tables_in_" + _this.dbname]] = {}; }); var len_1 = Object.keys(_this.tables).length; var counter_1 = 0; Object.keys(_this.tables).forEach(function (table) { _this.connectionPool.query("describe " + table, function (error, results) { results.forEach(function (col) { _this.tables[table][col["Field"]] = col["Type"]; }); counter_1++; if (counter_1 === len_1) { if (cback) cback(); } }); }); } }); }); }; return Database; }()); exports.default = Database;