Skip to content

Instantly share code, notes, and snippets.

@r37r0m0d3l
Forked from JeremyBernier/typeorm-upsert.ts
Created September 8, 2020 08:21
Show Gist options
  • Select an option

  • Save r37r0m0d3l/46f135aa4e7cd973656cb3fa064c27b6 to your computer and use it in GitHub Desktop.

Select an option

Save r37r0m0d3l/46f135aa4e7cd973656cb3fa064c27b6 to your computer and use it in GitHub Desktop.
TypeORM upsert
import { getRepository, InsertResult } from "typeorm";
/**
* Upsert for TypeORM on PostgreSQL
* Returns InsertResult object (contains ID)
* @param repo Repository
* @param {object | object[]} data Data to upsert. Can be object or array
* @param {string} primaryKey Name of column that is primary key
* @returns {Promise<InsertResult>}
*/
export default function upsert(Entity, data, primaryKey: string): Promise<InsertResult> {
const repo = getRepository(Entity);
const row = Array.isArray(data) ? data[0] : data;
const keys = Object.keys(row);
if (keys.length < 1) {
throw new Error("Cannot upsert without any values specified");
}
const updateStr = keys.map(key => `"${key}" = EXCLUDED."${key}"`).join(",");
return repo
.createQueryBuilder()
.insert()
.values(data)
.onConflict(`("${primaryKey}") DO UPDATE SET ${updateStr}`)
.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment