Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ObjectId in documents. #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions lib/mongoHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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({
Expand Down