tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
136 lines • 5.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryBuilder = exports.BaseDriver = void 0;
const events_1 = require("events");
const tools_1 = require("../../tools");
const constants_1 = require("../../constants");
class BaseDriver extends events_1.EventEmitter {
SLOW_QUERY_EXECUTE_TIME = 1000 * 15;
SLOW_QUERY_LIMIT_LENGTH = 1000 * 2;
pool;
options;
MESSAGE_TRX_CLOSED = "The transaction has either been closed";
import(mod) {
return tools_1.Tool.import(mod);
}
_detectEventQuery({ start, sql }) {
const duration = Date.now() - start;
if (duration > this.SLOW_QUERY_EXECUTE_TIME) {
if (sql.length > this.SLOW_QUERY_LIMIT_LENGTH) {
sql = `${sql.slice(0, this.SLOW_QUERY_LIMIT_LENGTH)}.......`;
}
console.log(this._messageSlowQuery(duration, sql));
this.emit("slowQuery", {
sql,
execution: duration,
});
}
this.emit("query", {
sql,
execution: duration,
});
this.emit(this._detectQueryType(sql).toLocaleLowerCase(), {
sql,
execution: duration,
});
}
_messageSlowQuery(duration, sql) {
const message = `\n\x1b[1m\x1b[31mWARING:\x1b[0m \x1b[1m\x1b[29mSlow query detected: Execution time: ${duration} ms\x1b[0m \n\x1b[33m${sql};\x1b[0m`;
return message;
}
_detectQueryType(query) {
const selectRegex = /^SELECT\b/i;
const updateRegex = /^UPDATE\b/i;
const insertRegex = /^INSERT\b/i;
const deleteRegex = /^DELETE\b/i;
if (selectRegex.test(query))
return "SELECT";
if (updateRegex.test(query))
return "UPDATE";
if (insertRegex.test(query))
return "INSERT";
if (deleteRegex.test(query))
return "DELETE";
return "";
}
_onPoolConnect(pool) {
const delay = 0;
setTimeout(() => {
pool.getConnection((err, connection) => {
if (err) {
const message = this._messageError.bind(this);
process.nextTick(() => {
if (String(err.message).includes("Pool is close")) {
return;
}
console.log(message(err.message == null || err.message === ""
? err.code
: err.message));
if (this.options.CONNECTION_ERROR)
return process.exit();
});
return;
}
this.emit("connected", connection);
if (this.options.CONNECTION_SUCCESS) {
connection.query(`SHOW VARIABLES LIKE 'version%'`, (err, results) => {
connection.release();
if (err)
return;
const message = [
results.find((v) => v?.Variable_name === "version"),
results.find((v) => v?.Variable_name === "version_comment"),
]
.map((v) => v?.Value)
.join(" - ");
console.log(this._messageConnected.bind(this)(`${message}`));
});
}
});
}, delay);
return;
}
_messageConnected(message) {
return `
\x1b[1m\x1b[32m
Connection established to the database.
Version : ${message ?? ""} \x1b[0m
------------------------------- \x1b[34m
HOST : ${this.options.host}
PORT : ${this.options.port}
DATABASE : ${this.options.database}
USERNAME : ${this.options.user}
PASSWORD : ${this.options.password} \x1b[0m
-------------------------------
`;
}
_messageError(message) {
return `
\x1b[1m\x1b[31m
Connection lost to database ! \x1b[0m
------------------------------- \x1b[33m
HOST : ${this.options.host}
PORT : ${this.options.port}
DATABASE : ${this.options.database}
USERNAME : ${this.options.user}
PASSWORD : ${this.options.password} \x1b[0m
-------------------------------
\x1b[1m\x1b[31mError Message
: ${message ?? ""} \x1b[0m
`;
}
}
exports.BaseDriver = BaseDriver;
class QueryBuilder {
$constants = (name) => {
if (!constants_1.CONSTANTS.hasOwnProperty(name))
throw new Error(`Not found that constant : '${name}'`);
return constants_1.CONSTANTS[name];
};
$state;
constructor(state) {
this.$state = state;
}
}
exports.QueryBuilder = QueryBuilder;
//# sourceMappingURL=index.js.map