UNPKG

node-jdbc-api

Version:

150 lines (128 loc) 4.55 kB
'use strict'; var jdbc = new (require('jdbc')), fs = require('fs'), winston = require("winston"), connector = require("kido-connector"); var path = require("path"); var exec = require("child_process").exec; connector.init("node-jdbc-api", winston); module.exports = function(config) { var self = this; var randomFileName = Math.round(Math.random() * 1e10) + '.jar'; config.jarFileName = config.jarFileName || randomFileName; config.jarFileName = config.jarFileName.split(path.sep)[config.jarFileName.split(path.sep).length - 1]; if(!config || typeof config !== 'object') { throw new Error('\'config\' argument must be an object.'); } if(!config.jdbcOutput){ winston.debug("no file selected"); if(!config.jarPath || typeof config.jarPath !== 'string') { throw new Error('\'jarPath\' property is missing or invalid.'); } if(!fs.readFileSync(config.jarPath)){ throw new Error('the driver file couldnt be found or its deleted, check the jarPath that it has to be an absolute route to the file.'); } }else{ winston.debug("Writing JAR file."); var regex = /^data:.+\/(.+);base64,(.*)$/; var matches = config.jdbcOutput.match(regex); var data = matches[2]; var buffer = new Buffer(data, 'base64'); // write buffer to file fs.writeFileSync(config.jarFileName, buffer); } if(!config.driverName || typeof config.driverName !== 'string') { throw new Error('\'driverName\' property is missing or invalid.'); } config.url = config.url || ""; var jarFile; if(config.jdbcOutput){ jarFile = process.cwd() + path.sep + config.jarFileName; }else{ jarFile = config.jarPath; } var jdbcConfig = { libpath: jarFile, drivername: config.driverName, //com.mysql.jdbc.Driver url: config.url //jdbc:mysql://localhost:3306 }; self.config = config; var createSession = function(credentials, cb) { winston.debug(JSON.stringify(jdbcConfig)); jdbcConfig.url += '?user=' + credentials.username + '&password=' + credentials.password; jdbc.initialize(jdbcConfig, function(err) { if (err) { return cb(err); } jdbc.open(function(err, conn) { if (err) { return cb(err); } cb(null, conn); }); }); }; var disposeSession = function(session, cb) { exec('rm ' + randomFileName, function(error){ if(error) { return winston.debug("Couldn't delete JAR file:" + error); } winston.debug('JAR file deleted.'); jdbc.close(cb); }) }; var _connector = new connector.Connector({ config: self.config, credentialProps: ["username", "password"], createSessionCb: createSession, disposeSessionCb: disposeSession }); // The authenticate method of your connector will be: this.authenticate = function(options, cb) { //get session _connector.getSession(options, function(err, session, auth) { if (err) return cb(err); cb(null, { auth: auth }); }); }; this.query = function(options, cb) { // Get session _connector.getSession(options, function(err, conn, auth) { if (err) { return cb(err); } jdbc.executeQuery(options.query, function(err, result) { if (err) { return cb(err); } cb(null, result); }); }); }; this.update = function(options, cb) { // Get session _connector.getSession(options, function(err, conn, auth) { if (err) { return cb(err); } jdbc.executeUpdate(options.query, function(err, result) { if (err) { return cb(err); } cb(null, result); }); }); }; // Close and release all sessions. this.close = function(cb) { exec('rm ' + randomFileName, function(error){ if(error) { return winston.debug("Couldn't delete JAR file:" + error); } winston.debug('JAR file deleted.'); jdbc.close(cb); }); }; };