From fbbe6ea9edcc457c089854873c6b38f4160ea226 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Thu, 3 Aug 2017 11:24:58 +1000 Subject: [PATCH 1/4] add upsert --- lib/createGeneric.js | 15 ++++++++------- lib/upsert.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 lib/upsert.js diff --git a/lib/createGeneric.js b/lib/createGeneric.js index 6de1309..8a2401e 100644 --- a/lib/createGeneric.js +++ b/lib/createGeneric.js @@ -12,12 +12,13 @@ function createGeneric(table, settable_fields, object) { return "$" + counter; }); - let query = `INSERT INTO ${table} (` - + set_fields.join(",") - + ") VALUES (" - + placeholders - + ") RETURNING *;"; - + let query = ` + INSERT INTO ${table} ( + ${set_fields.join(",")} + ) VALUES ( + ${placeholders} + ) RETURNING *; + ` return db.getFirstRow(query, values); } -module.exports = createGeneric; \ No newline at end of file +module.exports = createGeneric; diff --git a/lib/upsert.js b/lib/upsert.js new file mode 100644 index 0000000..9fa4c24 --- /dev/null +++ b/lib/upsert.js @@ -0,0 +1,36 @@ +const db = require('./index.js'); +module.exports = function (object, table, settable_fields, upsert_index) { + if(!(upsert_index in object)){ + throw new Error(upsert_index+" is required for upsert.") + } + let set_fields = settable_fields.filter(function(field){ + return Object.keys(object).includes(field); + }); + + let values = set_fields.map(function(field){ + return object[field]; + }); + let counter = 0; + let placeholders = set_fields.map(function() { + counter = counter + 1; + return "$" + counter; + }); + + let query = ` + INSERT INTO ${table} ( + ${set_fields.join(",")} + ) VALUES ( + ${placeholders} + ) + ON CONFLICT (${upsert_index}) + DO + UPDATE SET ( + ${set_fields.join(",")} + ) = ( + ${placeholders} + ) + WHERE orders.${upsert_index} = '${object[upsert_index]}' + RETURNING *; + ` + return db.getFirstRow(query, values); +} From 18f8fbf2723fee468116db2aab5e8753e345138e Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Thu, 3 Aug 2017 11:26:01 +1000 Subject: [PATCH 2/4] wip --- lib/index.js | 1 + test/upsert_test.js | 0 2 files changed, 1 insertion(+) create mode 100644 test/upsert_test.js diff --git a/lib/index.js b/lib/index.js index 32e3da4..e05def3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -50,4 +50,5 @@ module.exports.getCount = (text, values) => { }; module.exports.createGeneric = require('./createGeneric'); +module.exports.upsert = require('./upsert'); module.exports.queryBuilder = require('./queryBuilder'); diff --git a/test/upsert_test.js b/test/upsert_test.js new file mode 100644 index 0000000..e69de29 From 22c8c72cf6b4f94359c3817972c43cfea648f23f Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Thu, 3 Aug 2017 11:40:43 +1000 Subject: [PATCH 3/4] add upsert --- test/upsert_test.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/upsert_test.js diff --git a/test/upsert_test.js b/test/upsert_test.js deleted file mode 100644 index e69de29..0000000 From fdac01f771c39c177a475b4c529df76647a97981 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Thu, 3 Aug 2017 11:45:49 +1000 Subject: [PATCH 4/4] fix tablename --- lib/upsert.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/upsert.js b/lib/upsert.js index 9fa4c24..2b51601 100644 --- a/lib/upsert.js +++ b/lib/upsert.js @@ -29,7 +29,7 @@ module.exports = function (object, table, settable_fields, upsert_index) { ) = ( ${placeholders} ) - WHERE orders.${upsert_index} = '${object[upsert_index]}' + WHERE ${table}.${upsert_index} = '${object[upsert_index]}' RETURNING *; ` return db.getFirstRow(query, values);