Created
January 19, 2019 17:51
-
-
Save pirosuke/959b58eccb63755458d0a262d9ad2624 to your computer and use it in GitHub Desktop.
Sample To Fetch Data From Remote Database Table Through SSH Tunnel With Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const tunnel = require('tunnel-ssh'); | |
import knexLib = require('knex'); | |
async function main() { | |
const sshUserName = 'SSH_USER_NAME'; | |
const sshPassword = 'SSH_PASSWORD'; | |
let sshConfig = { | |
host: 'SSH_HOST_ADDRESS', | |
port: SSH_PORT, | |
username: sshUserName, | |
password: sshPassword, | |
keepaliveInterval: 60000, | |
keepAlive: true, | |
dstHost: 'TUNNEL_DEST_HOST_ADDRESS', | |
dstPort: TUNNEL_DEST_PORT, | |
localHost: 'localhost', | |
localPort: 45432 | |
}; | |
const tnl = tunnel(sshConfig, async (err: any, server: any) => { | |
if (err) { | |
throw err; | |
} | |
let knex = knexLib({ | |
client: 'postgresql', | |
connection: { | |
database: 'DATABASE_NAME', | |
port: 45432, | |
user: 'DATABASE_USER', | |
password: 'DATABASE_PASSWORD' | |
} | |
}); | |
const rows = await knex.select().from(TABLE_NAME); | |
console.log(rows); | |
await knex.destroy(); | |
tnl.close(); | |
}); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment