Last active
July 28, 2018 21:43
-
-
Save lkatney/4e5c05df5e4a6fb5d61ea22cbb35712a to your computer and use it in GitHub Desktop.
Wrapper code for Postgres queries to do crud operations in Salesforce Tables to sync with Heroku Connect - pg 6.0.1
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
var pgClient; | |
exports.setConnection = function(client){ | |
pgClient = client; | |
} | |
/************************************************************************* | |
*************************** CRUD OPERTIONS *************************** | |
************************************************************************/ | |
exports.getRecords = function(objectApiName, config, callback){ | |
var params = prepareReadParams(config); | |
var query = 'SELECT * FROM salesforce.'+objectApiName; | |
if(params.where){ | |
query += ' WHERE ' + params.where; | |
} | |
runQuery(query, params.vals, objectApiName , callback); | |
}; | |
exports.saveRecord = function(objectApiName, body, callback){ | |
var params = prepareInsertParams(body); | |
var query = 'INSERT INTO salesforce.'+objectApiName; | |
if(params.fields){ | |
query += '('+params.fields+') VALUES(' + params.numbers + ') RETURNING *'; | |
} | |
runQuery(query, params.vals, objectApiName , callback); | |
}; | |
exports.editRecord = function(objectApiName, config, body, callback){ | |
var params = prepareUpdateParams(body, config); | |
var query = 'UPDATE salesforce.'+objectApiName; | |
if(params.set){ | |
query += ' SET ' + params.set; | |
} | |
if(params.where){ | |
query += ' WHERE ' + params.where; | |
} | |
runQuery(query, params.vals, objectApiName , callback); | |
}; | |
exports.deleteRecord = function(objectApiName, IdToDelete, callback){ | |
var config = { Id : IdToDelete }; | |
var params = prepareReadParams(config); | |
var query = 'DELETE FROM salesforce.'+objectApiName; | |
if(params.where){ | |
query += ' WHERE ' + params.where; | |
} | |
runQuery(query, params.vals, objectApiName , callback); | |
}; | |
function prepareUpdateParams(config, findWith){ | |
var vals = []; | |
var set = ''; | |
var where = ''; | |
var counter = 1; | |
if(config !== {}){ | |
for(var key in config){ | |
if(key != '__proto__'){ | |
set += set != '' ? ', ' + key +'=($'+counter + ')' : key +'=($'+counter + ')'; | |
vals.push(config[key]); | |
counter++; | |
} | |
} | |
} | |
if(findWith !== {}){ | |
for(var key in findWith){ | |
where += where != '' ? ' AND '+ key + ' = ($'+ counter + ')': key + ' = ($'+ counter + ')'; | |
vals.push(findWith[key]); | |
} | |
} | |
return {where : where, vals : vals, set : set}; | |
} | |
function prepareReadParams(config){ | |
var vals = []; | |
var where = ''; | |
if(config !== {}){ | |
var counter = 1; | |
for(var key in config){ | |
where += where != '' ? ' AND '+ key + ' = $'+ counter : key + ' = $'+ counter; | |
vals.push(config[key]); | |
counter++; | |
} | |
} | |
return {where : where, vals : vals}; | |
} | |
function prepareInsertParams(config){ | |
var vals = []; | |
var fields = ''; | |
var numbers = ''; | |
if(config !== {}){ | |
var counter = 1; | |
for(var key in config){ | |
fields += fields != '' ? ',' + key : key; | |
numbers += numbers != ''? ',' + '$' + counter : '$'+counter; | |
vals.push(config[key]); | |
counter++; | |
} | |
} | |
return {fields : fields, numbers: numbers, vals : vals}; | |
} | |
function runQuery(query, vals, type, callback){ | |
console.log('-->query', query); | |
console.log('-->vals', vals); | |
pgClient.query(query, vals, function(err, result){ | |
var res, error; | |
if(!err){ | |
if(result.rows.length > 0){ | |
res = result.rows; | |
}else{ | |
res = []; | |
} | |
}else{ | |
console.log('-->error', err); | |
error = err; | |
} | |
callback(prepareResponse(error, res)); | |
}); | |
}; | |
function prepareResponse(err, result){ | |
var response = {success : false, data : ''}; | |
if (err){ | |
response.success = false; | |
response.data = 'Error: ' + err; | |
} | |
else{ | |
response.success = true; | |
response.data = result; | |
} | |
return response; | |
} |
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
var express = require("express"), //express version 3.4.1 | |
app = express(), | |
pg = require("pg"), //pg version 6.0.1 | |
path = require("path"), | |
crud = require('./crud.operations'); | |
app.set("port", (process.env.PORT || 5000)); | |
/* | |
* PG Client connection | |
*/ | |
pg.defaults.ssl = true; | |
var dbString = process.env.DATABASE_URL; | |
var sharedPgClient; | |
pg.connect(dbString, function(err,client){ | |
if(err){ | |
console.error("PG Connection Error") | |
} | |
console.log("Connected to Postgres"); | |
crud.setConnection(client); | |
}); | |
/* | |
* ExpressJS View Templates | |
*/ | |
app.set("views", path.join(__dirname, "./app/views")); | |
app.set("view engine", "ejs"); | |
app.get("/accounts",function(req, res){ | |
crud.getRecords('account', {Industry: 'IT'}, function(result){ | |
if(result.success){ | |
res.render("index.ejs", {result: result.data}); | |
}else{ | |
console.log(result); | |
} | |
}); | |
}); | |
app.post("/account",function(req, res){ | |
var body = req.body; // this is body : {name: 'New Acme', Industry : 'IT'}; | |
crud.saveRecord('account', body , function(result){ | |
if(result.success){ | |
res.render("index.ejs", {result: result.data}); | |
}else{ | |
console.log(result); | |
} | |
}); | |
}); | |
app.post("/account/:id",function(req, res){ | |
var body = req.body; // this is body : {id: 'sfid', name: 'Update Acme', Industry : 'IT'}; | |
var findWith = {id : body.id}; | |
crud.editRecord('account', body , findWith, function(result){ | |
if(result.success){ | |
res.render("index.ejs", {result: result.data}); | |
}else{ | |
console.log(result); | |
} | |
}); | |
}); | |
app.deleteRecord("/account/:id",function(req, res){ | |
crud.deleteRecord('account', req.params.id , function(result){ | |
if(result.success){ | |
res.render("index.ejs", {result: result.data}); | |
}else{ | |
console.log(result); | |
} | |
}); | |
}); | |
/* | |
* Run Server | |
*/ | |
var server = app.listen(app.get('port'), function(){ | |
console.log('Node Connect App Running at http://%s:%s', server.address().address, server.address().port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment