Skip to content

Commit 604831b

Browse files
committed
feat(db-ref): support passing a namespace into a DBRef ctor
1 parent 896f613 commit 604831b

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

Diff for: lib/bson/db_ref.js

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
function DBRef(collection, oid, db, fields) {
1212
if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields);
1313

14+
// check if namespace has been provided
15+
var parts = collection.split('.');
16+
if (parts.length === 2) {
17+
db = parts.shift();
18+
collection = parts.shift();
19+
}
20+
1421
this._bsontype = 'DBRef';
1522
this.collection = collection;
1623
this.oid = oid;

Diff for: lib/bson/parser/deserializer.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -534,26 +534,14 @@ var deserializeObject = function(buffer, index, options, isArray) {
534534

535535
// Update the index
536536
index = index + 12;
537-
538537
for (i = 0; i < namespace.length; i++) {
539538
if (namespace.charCodeAt(i) === 0xfffd) {
540539
throw new Error('Invalid UTF-8 string in BSON document');
541540
}
542541
}
543542

544-
// Split the namespace
545-
var parts = namespace.split('.');
546-
547-
var db, collection;
548-
if (parts.length === 2) {
549-
db = parts.shift();
550-
collection = parts.shift();
551-
} else {
552-
collection = namespace;
553-
}
554-
555543
// Upgrade to DBRef type
556-
object[name] = new DBRef(collection, oid, db);
544+
object[name] = new DBRef(namespace, oid);
557545
} else {
558546
throw new Error(
559547
'Detected unknown BSON type ' +

0 commit comments

Comments
 (0)