Skip to content

Commit 15646ba

Browse files
Refactor by comments
1 parent 3c326b6 commit 15646ba

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,24 +629,33 @@ if (typeof (DBRef) != "undefined") {
629629
};
630630

631631
DBRef.prototype.toString = function() {
632-
return `DBRef("${this.collection}", ${this.oid.tojson()}` +
632+
return `DBRef("${this.collection}", ${tojson(this.oid)}` +
633633
(this.db ? `, "${this.db}"` : "") + ")";
634634
};
635635

636636
Object.defineProperty(DBRef.prototype, "$ref", {
637637
get: function () {
638638
return this.collection;
639639
},
640+
set: function (value) {
641+
this.collection = value;
642+
},
640643
});
641644
Object.defineProperty(DBRef.prototype, "$id", {
642645
get: function () {
643646
return this.oid;
644647
},
648+
set: function (value) {
649+
this.oid = value;
650+
},
645651
});
646652
Object.defineProperty(DBRef.prototype, "$db", {
647653
get: function () {
648654
return this.db;
649655
},
656+
set: function (value) {
657+
this.db = value;
658+
},
650659
});
651660
} else {
652661
print("warning: no DBRef");

snippets/mongocompat/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,19 @@ assert.strictEqual(dbRef.$db, 'testDb');
8181
const dbRefNoDb = DBRef('testColl', id);
8282
assert.strictEqual(dbRefNoDb.toString(), 'DBRef("testColl", ObjectId("68ffa28b77bba38c9ddcf376"))');
8383
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)