Skip to content

Commit 996c829

Browse files
STREAMS-1972: Add old shell compatibility for DBRef for JS Engine (#19)
1 parent 7f418ce commit 996c829

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -602,36 +602,61 @@ if (typeof (DBPointer) != "undefined") {
602602
// DBRef
603603
if (typeof (DBRef) != "undefined") {
604604
DBRef.prototype.fetch = function() {
605-
assert(this.$ref, "need a ns");
606-
assert(this.$id, "need an id");
607-
var coll = this.$db ? db.getSiblingDB(this.$db).getCollection(this.$ref) : db[this.$ref];
608-
return coll.findOne({_id: this.$id});
605+
assert(this.collection, "need a ns");
606+
assert(this.oid, "need an id");
607+
var coll = this.db ? db.getSiblingDB(this.db).getCollection(this.collection) : db[this.collection];
608+
return coll.findOne({_id: this.oid});
609609
};
610610

611611
DBRef.prototype.tojson = function(indent) {
612612
return this.toString();
613613
};
614614

615615
DBRef.prototype.getDb = function() {
616-
return this.$db || undefined;
616+
return this.db || undefined;
617617
};
618618

619619
DBRef.prototype.getCollection = function() {
620-
return this.$ref;
620+
return this.collection;
621621
};
622622

623623
DBRef.prototype.getRef = function() {
624-
return this.$ref;
624+
return this.collection;
625625
};
626626

627627
DBRef.prototype.getId = function() {
628-
return this.$id;
628+
return this.oid;
629629
};
630630

631631
DBRef.prototype.toString = function() {
632-
return "DBRef(" + tojson(this.$ref) + ", " + tojson(this.$id) +
633-
(this.$db ? ", " + tojson(this.$db) : "") + ")";
632+
return `DBRef("${this.collection}", ${tojson(this.oid)}` +
633+
(this.db ? `, "${this.db}"` : "") + ")";
634634
};
635+
636+
Object.defineProperty(DBRef.prototype, "$ref", {
637+
get: function () {
638+
return this.collection;
639+
},
640+
set: function (value) {
641+
this.collection = value;
642+
},
643+
});
644+
Object.defineProperty(DBRef.prototype, "$id", {
645+
get: function () {
646+
return this.oid;
647+
},
648+
set: function (value) {
649+
this.oid = value;
650+
},
651+
});
652+
Object.defineProperty(DBRef.prototype, "$db", {
653+
get: function () {
654+
return this.db;
655+
},
656+
set: function (value) {
657+
this.db = value;
658+
},
659+
});
635660
} else {
636661
print("warning: no DBRef");
637662
}

snippets/mongocompat/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,30 @@ assert.strictEqual(tsFromStr.i, 255);
7070
assert.strictEqual(tsFromStr.t, 0);
7171
assert.strictEqual(Timestamp.MAX_VALUE._bsontype, 'Long');
7272
assert.strictEqual(Timestamp.MAX_VALUE, Long.MAX_UNSIGNED_VALUE);
73+
74+
const id = ObjectId('68ffa28b77bba38c9ddcf376');
75+
const dbRef = DBRef('testColl', id, 'testDb');
76+
assert.strictEqual(dbRef.toString(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"), "testDb")');
77+
assert.strictEqual(dbRef.tojson(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"), "testDb")');
78+
assert.strictEqual(dbRef.$ref, 'testColl');
79+
assert.strictEqual(dbRef.$id, id);
80+
assert.strictEqual(dbRef.$db, 'testDb');
81+
const dbRefNoDb = DBRef('testColl', id);
82+
assert.strictEqual(dbRefNoDb.toString(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"))');
83+
assert.strictEqual(dbRefNoDb.$db, undefined);
84+
const dbRefStringId = DBRef('testColl', '68ffa28b77bba38c9ddcf376');
85+
assert.strictEqual(dbRefStringId.toString(), 'DBRef("testColl", "68ffa28b77bba38c9ddcf376")');
86+
const dbRefForSetters = DBRef('originalColl', id, 'originalDb');
87+
dbRefForSetters.$ref = 'newColl';
88+
assert.strictEqual(dbRefForSetters.$ref, 'newColl');
89+
assert.strictEqual(dbRefForSetters.collection, 'newColl');
90+
assert.strictEqual(dbRefForSetters.toString(), 'DBRef("newColl", ObjectId("68ffa28b77bba38c9ddcf376"), "originalDb")');
91+
const newId = ObjectId('507f1f77bcf86cd799439011');
92+
dbRefForSetters.$id = newId;
93+
assert.strictEqual(dbRefForSetters.$id, newId);
94+
assert.strictEqual(dbRefForSetters.oid, newId);
95+
assert.strictEqual(dbRefForSetters.toString(), 'DBRef("newColl", ObjectId("507f1f77bcf86cd799439011"), "originalDb")');
96+
dbRefForSetters.$db = 'newDb';
97+
assert.strictEqual(dbRefForSetters.$db, 'newDb');
98+
assert.strictEqual(dbRefForSetters.db, 'newDb');
99+
assert.strictEqual(dbRefForSetters.toString(), 'DBRef("newColl", ObjectId("507f1f77bcf86cd799439011"), "newDb")');

0 commit comments

Comments
 (0)