Skip to content

Commit c4f0027

Browse files
committed
feat: use setPrototypeOf() instead of __proto__ to allow running on Deno
Re: #9056 Re: #12315
1 parent f04eaed commit c4f0027

12 files changed

+17
-19
lines changed

lib/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function Connection(base) {
8181
* Inherit from EventEmitter
8282
*/
8383

84-
Connection.prototype.__proto__ = EventEmitter.prototype;
84+
Object.setPrototypeOf(Connection.prototype, EventEmitter.prototype);
8585

8686
/**
8787
* Connection ready state

lib/drivers/node-mongodb-native/collection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function NativeCollection(name, conn, options) {
3434
* Inherit from abstract Collection.
3535
*/
3636

37-
NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
37+
Object.setPrototypeOf(NativeCollection.prototype, MongooseCollection.prototype);
3838

3939
/**
4040
* Called when the connection opens.

lib/drivers/node-mongodb-native/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ NativeConnection.STATES = STATES;
3232
* Inherits from Connection.
3333
*/
3434

35-
NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
35+
Object.setPrototypeOf(NativeConnection.prototype, MongooseConnection.prototype);
3636

3737
/**
3838
* Switches to a different database using the same connection pool.

lib/model.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function Model(doc, fields, skipId) {
127127
* @api private
128128
*/
129129

130-
Model.prototype.__proto__ = Document.prototype;
130+
Object.setPrototypeOf(Model.prototype, Document.prototype);
131131
Model.prototype.$isMongooseModelPrototype = true;
132132

133133
/**
@@ -1230,7 +1230,7 @@ Model.discriminator = function(name, schema, options) {
12301230
model = this.db.model(model || name, schema, this.$__collection.name);
12311231
this.discriminators[name] = model;
12321232
const d = this.discriminators[name];
1233-
d.prototype.__proto__ = this.prototype;
1233+
Object.setPrototypeOf(d.prototype, this.prototype);
12341234
Object.defineProperty(d, 'baseModelName', {
12351235
value: this.modelName,
12361236
configurable: true,
@@ -4979,8 +4979,8 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
49794979
model.modelName = name;
49804980

49814981
if (!(model.prototype instanceof Model)) {
4982-
model.__proto__ = Model;
4983-
model.prototype.__proto__ = Model.prototype;
4982+
Object.setPrototypeOf(model, Model);
4983+
Object.setPrototypeOf(model.prototype, Model.prototype);
49844984
}
49854985
model.model = function model(name) {
49864986
return this.db.model(name);
@@ -5077,8 +5077,8 @@ Model.__subclass = function subclass(conn, schema, collection) {
50775077
_this.call(this, doc, fields, skipId);
50785078
};
50795079

5080-
Model.__proto__ = _this;
5081-
Model.prototype.__proto__ = _this.prototype;
5080+
Object.setPrototypeOf(Model, _this);
5081+
Object.setPrototypeOf(Model.prototype, _this.prototype);
50825082
Model.db = conn;
50835083
Model.prototype.db = conn;
50845084
Model.prototype[modelDbSymbol] = conn;

test/document.isselected.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function TestDocument() {
2727
* Inherits from Document.
2828
*/
2929

30-
TestDocument.prototype.__proto__ = Document.prototype;
30+
Object.setPrototypeOf(TestDocument.prototype, Document.prototype);
3131

3232
for (const i in EventEmitter.prototype) {
3333
TestDocument[i] = EventEmitter.prototype[i];

test/document.populate.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function TestDocument() {
3030
* Inherits from Document.
3131
*/
3232

33-
TestDocument.prototype.__proto__ = Document.prototype;
33+
Object.setPrototypeOf(TestDocument.prototype, Document.prototype);
3434

3535
/**
3636
* Set a dummy schema to simulate compilation.

test/document.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function TestDocument() {
3838
* Inherits from Document.
3939
*/
4040

41-
TestDocument.prototype.__proto__ = Document.prototype;
41+
Object.setPrototypeOf(TestDocument.prototype, Document.prototype);
4242

4343
for (const i in EventEmitter.prototype) {
4444
TestDocument[i] = EventEmitter.prototype[i];

test/document.unit.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('sharding', function() {
2222
this.$__schema = mockSchema;
2323
this.$__ = {};
2424
};
25-
Stub.prototype.__proto__ = mongoose.Document.prototype;
25+
Object.setPrototypeOf(Stub.prototype, mongoose.Document.prototype);
2626
const d = new Stub();
2727
const currentTime = new Date();
2828
d._doc = { date: currentTime };

test/model.discriminator.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ describe('model', function() {
9696
const employee = new Employee();
9797
assert.ok(employee instanceof Person);
9898
assert.ok(employee instanceof Employee);
99-
assert.strictEqual(employee.__proto__.constructor, Employee);
100-
assert.strictEqual(employee.__proto__.__proto__.constructor, Person);
10199
});
102100

103101
it('can define static and instance methods', function() {

test/schema.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function TestDocument() {
3333
* Inherits from Document.
3434
*/
3535

36-
TestDocument.prototype.__proto__ = Document.prototype;
36+
Object.setPrototypeOf(TestDocument.prototype, Document.prototype);
3737

3838
/**
3939
* Test.

0 commit comments

Comments
 (0)