import _ from 'lodash' ; export default async < T > ( EntityType , obj , primary_key , opts ? ) : Promise < T > => { const keys = _ . difference ( _ . keys ( obj ) , opts ? opts . do_not_upsert : [ ] ) ; const setter_string = keys . map ( k => `${ opts ? opts . key_naming_transform ( k ) : k } = :${ k }` ) ; const qb = ( EntityType as any ) . createQueryBuilder ( ) . insert ( ) . values ( obj ) . onConflict ( `("${ primary_key }") DO UPDATE SET ${ setter_string }` ) ; keys . forEach ( k => { qb . setParameter ( k , ( obj as any ) [ k ] ) ; } ) ; return ( await qb . returning ( '*' ) . execute ( ) ) . generatedMaps [ 0 ] as T ; } ;