Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* @author David Spreekmeester <david@grrr.nl>
*/

const mysql = require('mysql2')
const Client = require('ssh2').Client;
const mysql = require('mysql2')
const Client = require('ssh2').Client;

var tunnel = module.exports = {

Expand All @@ -23,11 +23,11 @@ var tunnel = module.exports = {
* @param obj dbConfig MySQL Configuration as defined by mysql(2) package
* @return Promise <mysql2 connection>
*/
connect: function(sshConfig, dbConfig) {
connect: function (sshConfig, dbConfig) {
dbConfig = tunnel._addDefaults(dbConfig)
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
tunnel._conn = new Client();
tunnel._conn.on('ready', function() {
tunnel._conn.on('ready', function () {
tunnel._conn.forwardOut(
'127.0.0.1',
12345,
Expand All @@ -54,15 +54,25 @@ var tunnel = module.exports = {
})
},

close: function() {
if ('end' in tunnel._sql) {
tunnel._sql.end(function(err) {})
}
close: function () {
try {
if ('end' in tunnel._sql) {
tunnel._sql.end(function (err) {
if (err) { throw new Error(err); }
})
}
} catch (error) {
console.log(`error closing mysql connection: ${error.message}`);
}

if ('end' in tunnel._conn) {
tunnel._conn.end()
}
},
try {
if ('end' in tunnel._conn) {
tunnel._conn.end()
}
} catch (error) {
console.log(`error closing ssh connection: ${error.message}`);
}
},

_addDefaults(dbConfig) {
if (!('port' in dbConfig)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ssh2": "^0.5.4"
},
"name": "mysql-ssh",
"version": "1.0.6",
"version": "1.0.7",
"description": "Execute mysql commands over an ssh tunnel.",
"main": "mysql.js",
"devDependencies": {},
Expand Down