From e2ee4138b9aecd8b921d121f30e339896f21ae08 Mon Sep 17 00:00:00 2001 From: Alan Sikora Date: Mon, 20 Feb 2017 19:08:14 -0300 Subject: [PATCH 1/5] Remove $setOnInsert --- lib/clients/mongoclient.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/clients/mongoclient.js b/lib/clients/mongoclient.js index a73cccd..ed79d4c 100644 --- a/lib/clients/mongoclient.js +++ b/lib/clients/mongoclient.js @@ -149,12 +149,7 @@ class MongoClient extends DatabaseClient { return new Promise(function(resolve, reject) { const db = that._mongo.collection(collection); - let update = values; - if (options.upsert) { - update = { $setOnInsert: update }; - } else { - update = { $set: update }; - } + let update = { $set: values }; db.findOneAndUpdate(query, update, options, function(error, result) { if (error) return reject(error); @@ -404,4 +399,4 @@ const castQueryIds = function(query) { return query; }; -module.exports = MongoClient; \ No newline at end of file +module.exports = MongoClient; From 12f40bfbbe74f941b78a25bee12146f0d6c177d3 Mon Sep 17 00:00:00 2001 From: Alan Sikora Date: Mon, 20 Feb 2017 19:48:59 -0300 Subject: [PATCH 2/5] Disallow upsert without query keys --- lib/clients/mongoclient.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/clients/mongoclient.js b/lib/clients/mongoclient.js index ed79d4c..f83e08c 100644 --- a/lib/clients/mongoclient.js +++ b/lib/clients/mongoclient.js @@ -146,6 +146,21 @@ class MongoClient extends DatabaseClient { // Always return the updated object options.returnOriginal = false; + // If the query is empty, and trying to upsert, just create... + if (options.upsert && Object.keys(query).length === 0) { + delete options.upsert + + return new Promise(function(resolve, reject) { + const db = that._mongo.collection(collection); + + db.insert(values, options, function(error, result) { + if (error) return reject(error); + resolve(result.value); + }); + }); + } + + // ...otherwise, run the regular findOneAndUpdate return new Promise(function(resolve, reject) { const db = that._mongo.collection(collection); @@ -260,7 +275,7 @@ class MongoClient extends DatabaseClient { options = options || {}; options.unique = options.unique || false; options.sparse = options.sparse || false; - + const db = this._mongo.collection(collection); let keys = {}; From 620a84b7b237534c40eb4d2bb2c7e2142f9a0b18 Mon Sep 17 00:00:00 2001 From: Alan Sikora Date: Mon, 20 Feb 2017 19:50:17 -0300 Subject: [PATCH 3/5] Bumo --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c480b77..7a2a851 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "camo", - "version": "0.12.3", + "version": "0.12.4", "description": "A class-based ES6 ODM for Mongo-like databases.", "author": { "name": "Scott Robinson", From 66029db9bf5873913caf750a3eadc58f07e307b2 Mon Sep 17 00:00:00 2001 From: Guilherme Martini Date: Mon, 20 Feb 2017 21:38:52 -0300 Subject: [PATCH 4/5] returning inserted object --- lib/clients/mongoclient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/clients/mongoclient.js b/lib/clients/mongoclient.js index f83e08c..7973fc8 100644 --- a/lib/clients/mongoclient.js +++ b/lib/clients/mongoclient.js @@ -155,7 +155,7 @@ class MongoClient extends DatabaseClient { db.insert(values, options, function(error, result) { if (error) return reject(error); - resolve(result.value); + resolve(result.ops && result.ops.length && result.ops[0]); }); }); } From 2ac8860c378e8c0be5bbce3f673a1bdf58404793 Mon Sep 17 00:00:00 2001 From: Guilherme Martini Date: Mon, 20 Feb 2017 21:52:16 -0300 Subject: [PATCH 5/5] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7a2a851..5e0af54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "camo", - "version": "0.12.4", + "version": "0.12.6", "description": "A class-based ES6 ODM for Mongo-like databases.", "author": { "name": "Scott Robinson",