Skip to content

Commit

Permalink
Merge pull request #12313 from Automattic/vkarpov15/gh-9056
Browse files Browse the repository at this point in the history
Use setPrototypeOf() instead of __proto__ to allow running on Deno
  • Loading branch information
AbdelrahmanHafez committed Aug 22, 2022
2 parents 56ce8f7 + 39d76e8 commit 18d8227
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/connection.js
Expand Up @@ -81,7 +81,7 @@ function Connection(base) {
* Inherit from EventEmitter
*/

Connection.prototype.__proto__ = EventEmitter.prototype;
Object.setPrototypeOf(Connection.prototype, EventEmitter.prototype);

/**
* Connection ready state
Expand Down
2 changes: 1 addition & 1 deletion lib/drivers/node-mongodb-native/collection.js
Expand Up @@ -34,7 +34,7 @@ function NativeCollection(name, conn, options) {
* Inherit from abstract Collection.
*/

NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
Object.setPrototypeOf(NativeCollection.prototype, MongooseCollection.prototype);

/**
* Called when the connection opens.
Expand Down
2 changes: 1 addition & 1 deletion lib/drivers/node-mongodb-native/connection.js
Expand Up @@ -32,7 +32,7 @@ NativeConnection.STATES = STATES;
* Inherits from Connection.
*/

NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
Object.setPrototypeOf(NativeConnection.prototype, MongooseConnection.prototype);

/**
* Switches to a different database using the same connection pool.
Expand Down
12 changes: 6 additions & 6 deletions lib/model.js
Expand Up @@ -126,7 +126,7 @@ function Model(doc, fields, skipId) {
* top level (non-sub) documents.
*/

Model.prototype.__proto__ = Document.prototype;
Object.setPrototypeOf(Model.prototype, Document.prototype);
Model.prototype.$isMongooseModelPrototype = true;

/**
Expand Down Expand Up @@ -1218,7 +1218,7 @@ Model.discriminator = function(name, schema, options) {
model = this.db.model(model || name, schema, this.$__collection.name);
this.discriminators[name] = model;
const d = this.discriminators[name];
d.prototype.__proto__ = this.prototype;
Object.setPrototypeOf(d.prototype, this.prototype);
Object.defineProperty(d, 'baseModelName', {
value: this.modelName,
configurable: true,
Expand Down Expand Up @@ -4944,8 +4944,8 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
model.modelName = name;

if (!(model.prototype instanceof Model)) {
model.__proto__ = Model;
model.prototype.__proto__ = Model.prototype;
Object.setPrototypeOf(model, Model);
Object.setPrototypeOf(model.prototype, Model.prototype);
}
model.model = function model(name) {
return this.db.model(name);
Expand Down Expand Up @@ -5037,8 +5037,8 @@ Model.__subclass = function subclass(conn, schema, collection) {
_this.call(this, doc, fields, skipId);
};

Model.__proto__ = _this;
Model.prototype.__proto__ = _this.prototype;
Object.setPrototypeOf(Model, _this);
Object.setPrototypeOf(Model.prototype, _this.prototype);
Model.db = conn;
Model.prototype.db = conn;
Model.prototype[modelDbSymbol] = conn;
Expand Down
2 changes: 1 addition & 1 deletion test/document.isselected.test.js
Expand Up @@ -27,7 +27,7 @@ function TestDocument() {
* Inherits from Document.
*/

TestDocument.prototype.__proto__ = Document.prototype;
Object.setPrototypeOf(TestDocument.prototype, Document.prototype);

for (const i in EventEmitter.prototype) {
TestDocument[i] = EventEmitter.prototype[i];
Expand Down
2 changes: 1 addition & 1 deletion test/document.populate.test.js
Expand Up @@ -30,7 +30,7 @@ function TestDocument() {
* Inherits from Document.
*/

TestDocument.prototype.__proto__ = Document.prototype;
Object.setPrototypeOf(TestDocument.prototype, Document.prototype);

/**
* Set a dummy schema to simulate compilation.
Expand Down
2 changes: 1 addition & 1 deletion test/document.test.js
Expand Up @@ -38,7 +38,7 @@ function TestDocument() {
* Inherits from Document.
*/

TestDocument.prototype.__proto__ = Document.prototype;
Object.setPrototypeOf(TestDocument.prototype, Document.prototype);

for (const i in EventEmitter.prototype) {
TestDocument[i] = EventEmitter.prototype[i];
Expand Down
2 changes: 1 addition & 1 deletion test/document.unit.test.js
Expand Up @@ -22,7 +22,7 @@ describe('sharding', function() {
this.$__schema = mockSchema;
this.$__ = {};
};
Stub.prototype.__proto__ = mongoose.Document.prototype;
Object.setPrototypeOf(Stub.prototype, mongoose.Document.prototype);
const d = new Stub();
const currentTime = new Date();
d._doc = { date: currentTime };
Expand Down
2 changes: 0 additions & 2 deletions test/model.discriminator.test.js
Expand Up @@ -96,8 +96,6 @@ describe('model', function() {
const employee = new Employee();
assert.ok(employee instanceof Person);
assert.ok(employee instanceof Employee);
assert.strictEqual(employee.__proto__.constructor, Employee);
assert.strictEqual(employee.__proto__.__proto__.constructor, Person);
});

it('can define static and instance methods', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/schema.test.js
Expand Up @@ -33,7 +33,7 @@ function TestDocument() {
* Inherits from Document.
*/

TestDocument.prototype.__proto__ = Document.prototype;
Object.setPrototypeOf(TestDocument.prototype, Document.prototype);

/**
* Test.
Expand Down
4 changes: 2 additions & 2 deletions test/types.document.test.js
Expand Up @@ -33,7 +33,7 @@ describe('types.document', function() {
mongoose.Document.call(this, {});
}
Dummy = _Dummy;
Dummy.prototype.__proto__ = mongoose.Document.prototype;
Object.setPrototypeOf(Dummy.prototype, mongoose.Document.prototype);
Dummy.prototype.$__setSchema(new Schema());

function _Subdocument() {
Expand All @@ -43,7 +43,7 @@ describe('types.document', function() {
}
Subdocument = _Subdocument;

Subdocument.prototype.__proto__ = ArraySubdocument.prototype;
Object.setPrototypeOf(Subdocument.prototype, ArraySubdocument.prototype);

for (const i in EventEmitter.prototype) {
Subdocument[i] = EventEmitter.prototype[i];
Expand Down
2 changes: 1 addition & 1 deletion test/types.documentarray.test.js
Expand Up @@ -29,7 +29,7 @@ function TestDoc(schema) {
* Inherits from ArraySubdocument.
*/

Subdocument.prototype.__proto__ = ArraySubdocument.prototype;
Object.setPrototypeOf(Subdocument.prototype, ArraySubdocument.prototype);

/**
* Set schema.
Expand Down

0 comments on commit 18d8227

Please sign in to comment.