Skip to content

Instantly share code, notes, and snippets.

@adnanoner
adnanoner / upsert.js
Last active January 18, 2018 09:21
Knex postgres upsert
/**
* Perform an "Upsert" using the "INSERT ... ON CONFLICT ... " syntax in PostgreSQL 9.5
* @link http://www.postgresql.org/docs/9.5/static/sql-insert.html
* @author https://github.com/adnanoner
* inspired by: https://gist.github.com/plurch/118721c2216f77640232
* @param {string} tableName - The name of the database table
* @param {string} conflictTarget - The column in the table which has a unique index constraint
* @param {Object} itemData - a hash of properties to be inserted/updated into the row
* @returns {Promise} - A Promise which resolves to the inserted/updated row
*/