From 604831b492479ab381dcdf80cbe6fb69537a9d59 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Wed, 28 Feb 2018 16:24:58 -0500 Subject: [PATCH] feat(db-ref): support passing a namespace into a DBRef ctor --- lib/bson/db_ref.js | 7 +++++++ lib/bson/parser/deserializer.js | 14 +------------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/bson/db_ref.js b/lib/bson/db_ref.js index b8ffa68a..9df2fb8f 100644 --- a/lib/bson/db_ref.js +++ b/lib/bson/db_ref.js @@ -11,6 +11,13 @@ function DBRef(collection, oid, db, fields) { if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields); + // check if namespace has been provided + var parts = collection.split('.'); + if (parts.length === 2) { + db = parts.shift(); + collection = parts.shift(); + } + this._bsontype = 'DBRef'; this.collection = collection; this.oid = oid; diff --git a/lib/bson/parser/deserializer.js b/lib/bson/parser/deserializer.js index c9d864b8..4414fbe4 100644 --- a/lib/bson/parser/deserializer.js +++ b/lib/bson/parser/deserializer.js @@ -534,26 +534,14 @@ var deserializeObject = function(buffer, index, options, isArray) { // Update the index index = index + 12; - for (i = 0; i < namespace.length; i++) { if (namespace.charCodeAt(i) === 0xfffd) { throw new Error('Invalid UTF-8 string in BSON document'); } } - // Split the namespace - var parts = namespace.split('.'); - - var db, collection; - if (parts.length === 2) { - db = parts.shift(); - collection = parts.shift(); - } else { - collection = namespace; - } - // Upgrade to DBRef type - object[name] = new DBRef(collection, oid, db); + object[name] = new DBRef(namespace, oid); } else { throw new Error( 'Detected unknown BSON type ' +