Skip to content

Commit

Permalink
feat(db-ref): support passing a namespace into a DBRef ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 28, 2018
1 parent 896f613 commit 604831b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
7 changes: 7 additions & 0 deletions lib/bson/db_ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 1 addition & 13 deletions lib/bson/parser/deserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' +
Expand Down

0 comments on commit 604831b

Please sign in to comment.