mysql-ssh
Advanced tools
Comparing version 1.0.3 to 1.0.4
57
mysql.js
@@ -10,4 +10,6 @@ #!/usr/bin/env node | ||
var mysqlssh = module.exports = { | ||
var tunnel = module.exports = { | ||
_conn: null, | ||
/** | ||
@@ -19,31 +21,36 @@ * @param obj sshConfig SSH Configuration as defined by ssh2 package | ||
connect: function(sshConfig, dbConfig) { | ||
dbConfig = mysqlssh._addDefaults(dbConfig) | ||
dbConfig = tunnel._addDefaults(dbConfig) | ||
return new Promise(function(resolve, reject) { | ||
var ssh = new Client(); | ||
ssh.on('ready', function() { | ||
ssh.forwardOut( | ||
'127.0.0.1', | ||
12345, | ||
dbConfig.host, | ||
dbConfig.port, | ||
function (err, stream) { | ||
if (err) { | ||
if (err.reason == 'CONNECT_FAILED') { | ||
ssh.end() | ||
reject('Connection failed.') | ||
} | ||
tunnel._conn = new Client(); | ||
tunnel._conn.on('ready', tunnel._onReady).connect(sshConfig) | ||
}) | ||
}, | ||
reject(err) | ||
} | ||
close: function() { | ||
tunnel._conn.end() | ||
}, | ||
// override db host, since we're operating from within the SSH tunnel | ||
dbConfig.host = 'localhost' | ||
dbConfig.stream = stream | ||
_onReady: function() { | ||
tunnel._conn.forwardOut( | ||
'127.0.0.1', | ||
12345, | ||
dbConfig.host, | ||
dbConfig.port, | ||
function (err, stream) { | ||
if (err) { | ||
tunnel._conn.end() | ||
var msg = err.reason == 'CONNECT_FAILED' | ||
? 'Connection failed.' | ||
: err | ||
return reject(err) | ||
} | ||
var sql = mysql.createConnection(dbConfig) | ||
resolve(sql) | ||
}); | ||
// override db host, since we're operating from within the SSH tunnel | ||
dbConfig.host = 'localhost' | ||
dbConfig.stream = stream | ||
}).connect(sshConfig); | ||
}) | ||
var sql = mysql.createConnection(dbConfig) | ||
resolve(sql) | ||
} | ||
) | ||
}, | ||
@@ -50,0 +57,0 @@ |
@@ -9,3 +9,3 @@ { | ||
"name": "mysql-ssh", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Execute mysql commands over an ssh tunnel.", | ||
@@ -12,0 +12,0 @@ "main": "mysql.js", |
@@ -7,3 +7,5 @@ # MySQL SSH | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/grrr-amsterdam/mysql-ssh.svg)](https://greenkeeper.io/) | ||
## API | ||
@@ -18,4 +20,4 @@ | ||
## Usage | ||
Don't forget to `.close()` the tunnel connection when you're done querying the database. | ||
@@ -43,2 +45,3 @@ ```javascript | ||
console.log(results); | ||
mysqlssh.close() | ||
}) | ||
@@ -45,0 +48,0 @@ }) |
10952
56
50