jeefo-mysql
Advanced tools
Comparing version 0.0.32 to 0.0.33
51
index.js
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. | ||
* File Name : index.js | ||
* Created at : 2021-10-09 | ||
* Updated at : 2021-10-10 | ||
* Updated at : 2021-12-02 | ||
* Author : jeefo | ||
@@ -19,4 +19,4 @@ * Purpose : | ||
const fs = require("@jeefo/fs"); | ||
const is = require("@jeefo/utils/is"); | ||
const mysql = require("mysql"); | ||
const is = require("@jeefo/utils/is"); | ||
const processes = Object.create(null); | ||
@@ -37,7 +37,2 @@ | ||
this.is_connected = false; | ||
if (config.auto_connect_enabled) { | ||
this.connect(); | ||
} | ||
} | ||
@@ -50,7 +45,8 @@ | ||
this.connection.connect(err => { | ||
this.pending = null; | ||
if (err) return reject(err); | ||
const {threadId} = this.connection; | ||
this.is_connected = true; | ||
resolve(); | ||
console.log(`[Jeefo MySQL] connceted thread id: [${threadId}].`); | ||
//const {threadId} = this.connection; | ||
//console.log(`[Jeefo MySQL] connceted thread id: [${threadId}].`); | ||
}); | ||
@@ -63,12 +59,8 @@ }); | ||
end() { | ||
return new Promise((resolve, reject) => { | ||
this.connection.end(err => { | ||
if (err) return reject(err); | ||
const {threadId} = this.connection; | ||
console.log(`[Jeefo MySQL] conncetion thread [${threadId}] is closed.`); | ||
this.pending = null; | ||
resolve(); | ||
}); | ||
}); | ||
return new Promise((resolve, reject) => this.connection.end(err => { | ||
this.is_connected = false; | ||
err ? reject(err) : resolve(); | ||
//const {threadId} = this.connection; | ||
//console.log(`[Jeefo MySQL] conncetion thread [${threadId}] is closed.`); | ||
})); | ||
} | ||
@@ -78,2 +70,3 @@ | ||
this.connection.destroy(); | ||
this.is_connected = false; | ||
} | ||
@@ -174,4 +167,7 @@ | ||
exec(query) { | ||
clearTimeout(this.timeout_id); | ||
return new Promise(async (resolve, reject) => { | ||
console.log(`[Jeefo MySQL] exec query: ${query}`); | ||
if (!this.is_connected) await this.connect(); | ||
//console.log(`[Jeefo MySQL] exec query: ${query}`); | ||
this.connection.query(query, (err, results, fields) => { | ||
@@ -181,2 +177,3 @@ if (err) return reject(err); | ||
resolve({results, fields}); | ||
this.timeout_id = setTimeout(() => this.end(), 10); | ||
}); | ||
@@ -216,3 +213,3 @@ }); | ||
function get_database(table_name, config = default_config) { | ||
function get_connection(table_name, config = default_config) { | ||
return ( | ||
@@ -224,4 +221,4 @@ processes[table_name] || | ||
get_database.config = async cfg => { | ||
default_config = Object.assign(Object.create(null), cfg); | ||
get_connection.config = cfg => { | ||
default_config = Object.assign({}, cfg); | ||
if (default_config.auto_connect_enabled === void 0) { | ||
@@ -232,6 +229,7 @@ default_config.auto_connect_enabled = true; | ||
get_database.config_load = async filepath => { | ||
get_database.config(await fs.load_json(filepath)); | ||
get_connection.load_config = async filepath => { | ||
get_connection.config(await fs.load_json(filepath)); | ||
}; | ||
/* | ||
const exit_events = [ | ||
@@ -272,3 +270,4 @@ "exit", | ||
} | ||
*/ | ||
module.exports = get_database; | ||
module.exports = get_connection; |
{ | ||
"name": "jeefo-mysql", | ||
"version": "0.0.32", | ||
"version": "0.0.33", | ||
"description": "Very simple MySQL connection instances for each table. I had annoyed enough for MySQL Deadlocks.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
16320
448