Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uat #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Uat #40

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
123 changes: 98 additions & 25 deletions examples/connection_pooling.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,108 @@
var Redshift = require('../index.js');

var client = {
user: 'user',
database: 'database',
password: 'password',
port: 'port',
host: 'host'
user: 'karthikeyan_b',
database: 'gti_dw',
password: 'KarthikeyanB2021',
port: '5439',
host: 'gti-dw-dev.cskoof8zzowd.us-east-1.redshift.amazonaws.com'
};

var redshift = new Redshift(client); //no need to call connect(), without rawConnection, it automatically connects
var redshift = new Redshift(client,{rawConnection:true}); //no need to call connect(), without rawConnection, it automatically connects


module.exports.GetList = function(req,res) {
// using callbacks
redshift.query('SELECT * FROM "Tags"', {raw: true}, function(err, data){
if(err) throw err;
else{
console.log(data);

// if you want to close client pool, uncomment redshift.close() line
// but you won't be able to make subsequent calls because connection is terminated
// redshift.close();
try {
redshift.connect();
console.log("redshift Query");
redshift.query('SELECT * FROM "form_user_details"', { raw: true }, function (err, data) {
if (err) {
console.log(err)
res.status(400).send(err);
} else {
console.log(data);
res.status(200).send(data);

// if you wasnt to close client pool, uncomment redshift.close() line
// but you won't be able to make subsequent calls because connection is terminated
redshift.close();
}
});
} catch (error) {
res.status(400).send(error);
}
});
}

// using promises
redshift.query('SELECT * FROM "Tags"', {raw: true})
.then(function(data){
console.log(data);

// if you want to close client pool, uncomment redshift.close() line
// but you won't be able to make subsequent calls because connection is terminated
// redshift.close();
}, function(err){
throw err;
});
module.exports.GetListwithPromises = function(req,res) {
// using callbacks
try {

// var redshift = new Redshift(client);

redshift.connect(function(err){ //create connection manually
if (err) {
res.status(401).send(''+ err);
} else {
console.log('redshift connected succesfully');
redshift.query('SELECT * FROM form_user_details', {raw: true}).then(function(data){ //query redshift
console.log(data);
res.status(200).send(data);

redshift.close();
}, function(err){
res.status(400).send(err);
});
}
});
} catch (error) {
res.status(401).send('Error - ' + error);
}
}

module.exports.GetListwithRawQuery = function(req,res) {
// using callbacks
try {

// var redshift = new Redshift(client);

redshift.rawQuery(`SELECT * FROM "form_user_details"`, {raw: true}).then(function(data){
console.log(data);
})
.catch(function(err){
console.log(err);
});
} catch (error) {
res.status(401).send('Error - ' + error);
}
}

// const query = `select * from webapp.partner where is_active = true`;
module.exports.executeQuery = async function(req,res) {
try {
var query = await redshift.createQuery(req.query);
console.log(query);
redshift.connectRedShift(async function (dbConnection) {
const date1 = new Date().getTime();
const connection = await dbConnection;
const result = await connection.query(query);

const date2 = new Date().getTime();
const durationMs = date2 - date1;
const durationSeconds = Math.round(durationMs / 1000);
let dataLength = 0;

if (result && result.length) dataLength = result.length;

console.log(
`[Redshift] [${durationMs}ms] [${durationSeconds}s] [${dataLength.toLocaleString()} records] ${query}`
);

return res.status(200).send({ "query": query, "result": result });
})
} catch (e) {
console.error(`Error executing query: ${query} Error: ${e.message}`);
res.status(401).send(e);
}
}
31 changes: 28 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
const express = require('express');
const bodyParser = require("body-parser");
const path = require('path');

module.exports = require('./lib/connection.js');
module.exports.query = require('./lib/query.js');
module.exports.model = require('./lib/model.js');
module.exports.import = require('./lib/model.js').import;
module.exports = require('./lib/test_one.js');
// module.exports.query = require('./lib/query.js');
// module.exports.model = require('./lib/model.js');
// module.exports.import = require('./lib/model.js').import;

var webapp = require('./examples/connection_pooling');


var app = express();

app.set('port', process.env.PORT || 4000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.static(path.join(__dirname, 'public')));

app.get('/getList', webapp.executeQuery);

app.listen(4000, function () {
console.log('Server is running.. on Port https://localhost:4000/');
});
69 changes: 64 additions & 5 deletions lib/connection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
var pg = require('pg');
var Promise = require('bluebird');
var pgp = require("pg-promise");

/**
* Redshift constructor function
* @param {Object} config connection object with {host, port, database, user, password}
* @param {Object} options {rawConnection: <bool>}
*/
var Redshift = function (config, options) {
console.log(config)
if (config && typeof config === 'string' || typeof config === 'object') {
var that = this;
that.config = config;
Expand Down Expand Up @@ -67,5 +64,67 @@ Redshift.prototype.connect = function (callback) {
callback(new Error("Couldn't connect to redshift. Invalid connection type"));
}
};
var connections = [];
Redshift.prototype.connectRedShift = function (callback) {
const dbName = "gti_dw";

if (!connections[dbName]) {
const dbUser = "karthikeyan_b";
const dbPassword = "KarthikeyanB2021";
const dbHost = "gti-dw-dev.cskoof8zzowd.us-east-1.redshift.amazonaws.com";
const dbPort = "5439";

const dbc = pgp({ capSQL: true });
console.log(`Opening connection to: ${dbName}, host is: ${dbHost}`);

const connectionString = `postgres://${dbUser}:${dbPassword}@${dbHost}:${dbPort}/${dbName}`;
connections[dbName] = dbc(connectionString);
}

callback(connections[dbName]);
};

Redshift.prototype.createQuery = function (req) {
if (req.query) {
return req.query;
}
var schemaName = req.Schema_Name;
var tableName = req.Table_Name;
var filter_rows = req.filter? JSON.parse(req.filter): [];
var filter_columns = req.columns_list ? JSON.parse(req.columns_list): [];
// console.log(schemaName, tableName, typeof (filter_columns), typeof (filter_rows));
var query = "select "
if (filter_columns.length > 0) {
for (var i = 0; i < filter_columns.length; i++){
if (filter_columns.length - 1 != i) {
query += filter_columns[i] + ",";
} else {
query += filter_columns[i] + " ";
}
}
query += "from ";
} else {
query += "* from ";
}
if (schemaName) {
query += schemaName + ".";
}
if (tableName) {
query += tableName + "";
}
if (filter_rows.length > 0) {
query += " where "
for (var i = 0; i < filter_rows.length; i++){
if (filter_rows.length - 1 != i) {
query += filter_rows[i]["key"] + "=" + filter_rows[i]["value"] + " and ";
} else {
query += filter_rows[i]["key"] + "=" + filter_rows[i]["value"] + "";
}
}
} else {
query += "";
}
return query;
}

module.exports = Redshift;
5 changes: 5 additions & 0 deletions lib/test_one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var test = function() {
return true;
}

module.exports = test;
12 changes: 12 additions & 0 deletions node_modules/.bin/dateformat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/dateformat.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/.bin/dateformat.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions node_modules/.bin/is-ci

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/is-ci.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/.bin/is-ci.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion node_modules/.bin/migrate

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading