I've searched extensively and I've not been able to solve this simple issue. I'm programming in nodejs, and using pg-promise to store my JSON file dynamically into a postgres 9.4.11 database (I'm not allowed to upgrade the DB). My JSON contains about a hundred columns each, and I've parsed it properly into three objects. 1. fields 2. columns 3. tables Pg-promise works PERFECTLY when I do this:
//-- connection details would be up here const db = pgp (conn);--
//Insert statement is below
for (var i=0; i<Object.keys(fields).length; i++){
const data = [fields[i]];
const cs = new pgp.helpers.ColumnSet(columns[i]);
const table = new pgp.helpers.TableName(tables[i],"public");
const insert = pgp.helpers.insert(data,cs,table);
db.none(insert)
.then(data => {
console.log("Success! Record inserted for " + table);
})
.catch(error => {
//error
console.log("ALERT!!! Something went wrong with Record in table " + table);
});
}
But of course, I'm on postgresql 9.4.11 and I can't use the simple:
const insert = pgp.helpers.insert(data,cs,table) + " ON CONFLICT (id1, uid2, xid3) DO NOTHING";
to perform an insert of ONLY new items because UPSERTS are only available starting at 9.5.
My query performs about 2,000 row inserts per run and I'm comfortable with that.
I'm pretty new at this, and I hope maybe in my hours of googling I overlooked the straight forward solution.
The closest resolution I've seen is a stored procedure, but I'm not sure how I'd implement that.
Please, stackoverflow, you might be my only hope!
I simply want to make sure each row that matches on id1, uid2, and xid3 does not get inserted into my postgres db.