From a12c8b4acade54b9e905e557f045c69f3d1e7997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?So=CC=88ren=20Oldag?= Date: Tue, 28 Mar 2017 17:33:18 +0200 Subject: [PATCH] Support ObjectId in documents. --- lib/mongoHandler.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/mongoHandler.js b/lib/mongoHandler.js index bbc58f4..c70cc56 100644 --- a/lib/mongoHandler.js +++ b/lib/mongoHandler.js @@ -30,11 +30,6 @@ MongoStore._checkMinServerVersion = function() { }; -MongoStore._mongoUuid = function(uuid) { - return new mongodb.Binary(uuid, mongodb.Binary.SUBTYPE_UUID); -}; - - MongoStore._isRelationshipAttribute = function(attribute) { return attribute._settings && (attribute._settings.__one || attribute._settings.__many); }; @@ -73,6 +68,15 @@ MongoStore._filterElementToMongoExpr = function(filterElement) { }; +MongoStore.prototype._mongoUuid = function(uuid) { + if(this._config.idType === 'objectId') { + return new mongodb.ObjectId(uuid); + } + + return new mongodb.Binary(uuid, mongodb.Binary.SUBTYPE_UUID); +}; + + MongoStore.prototype._getSearchCriteria = function(request) { var self = this; var filter = request.processedFilter; @@ -259,7 +263,7 @@ MongoStore.prototype.search = function(request, callback) { */ MongoStore.prototype.find = function(request, callback) { var collection = this._db.collection(request.params.type); - var documentId = MongoStore._mongoUuid(request.params.id); + var documentId = this._mongoUuid(request.params.id); debug("findOne", JSON.stringify({ _id: documentId })); collection.findOne({ _id: documentId }, { _id: 0 }, function(err, result) { @@ -294,7 +298,7 @@ MongoStore.prototype.create = function(request, newResource, callback) { */ MongoStore.prototype.delete = function(request, callback) { var collection = this._db.collection(request.params.type); - var documentId = MongoStore._mongoUuid(request.params.id); + var documentId = this._mongoUuid(request.params.id); collection.deleteOne({ _id: documentId }, function(err, result) { if (err) return callback(MongoStore._unknownError(err)); if (result.deletedCount === 0) { @@ -311,7 +315,7 @@ MongoStore.prototype.delete = function(request, callback) { */ MongoStore.prototype.update = function(request, partialResource, callback) { var collection = this._db.collection(request.params.type); - var documentId = MongoStore._mongoUuid(request.params.id); + var documentId = this._mongoUuid(request.params.id); var partialDocument = _.omitBy(partialResource, function(value) { return value === undefined; }); debug("findOneAndUpdate", JSON.stringify(partialDocument)); collection.findOneAndUpdate({