Skip to content

Commit ff9ef1f

Browse files
committed
fix: consistent toLegacy
1 parent 7fc7135 commit ff9ef1f

File tree

8 files changed

+45
-79
lines changed

8 files changed

+45
-79
lines changed

src/legacy_wrappers/admin.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ Object.defineProperty(module.exports, '__esModule', { value: true });
77

88
module.exports.makeLegacyAdmin = function (baseClass) {
99
class LegacyAdmin extends baseClass {
10-
constructor(db) {
11-
if (db instanceof baseClass || db instanceof LegacyAdmin) {
12-
super(db.s.db);
13-
} else {
14-
super(db);
15-
}
16-
}
17-
1810
addUser(username, password, options, callback) {
1911
callback =
2012
typeof callback === 'function'
@@ -136,7 +128,7 @@ module.exports.makeLegacyAdmin = function (baseClass) {
136128
Object.defineProperty(baseClass.prototype, toLegacy, {
137129
enumerable: false,
138130
value: function () {
139-
return new LegacyAdmin(this);
131+
return Object.setPrototypeOf(this, LegacyAdmin.prototype);
140132
}
141133
});
142134

src/legacy_wrappers/bulk.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
'use strict';
22

3+
const { maybeCallback } = require('../utils');
4+
35
module.exports = Object.create(null);
46
Object.defineProperty(module.exports, '__esModule', { value: true });
57

68
module.exports.makeLegacyOrderedBulkOperation = function (baseClass) {
79
return class LegacyOrderedBulkOperation extends baseClass {
8-
execute(callback) {
9-
10+
execute(options, callback) {
11+
callback =
12+
typeof callback === 'function'
13+
? callback
14+
: typeof options === 'function'
15+
? options
16+
: undefined;
17+
options = typeof options !== 'function' ? options : undefined;
18+
return maybeCallback(super.execute(options), callback);
1019
}
1120
};
1221
};
1322

1423
module.exports.makeLegacyUnorderedBulkOperation = function (baseClass) {
1524
return class LegacyUnorderedBulkOperation extends baseClass {
16-
execute(callback) {
17-
25+
execute(options, callback) {
26+
callback =
27+
typeof callback === 'function'
28+
? callback
29+
: typeof options === 'function'
30+
? options
31+
: undefined;
32+
options = typeof options !== 'function' ? options : undefined;
33+
return maybeCallback(super.execute(options), callback);
1834
}
1935
};
2036
};

src/legacy_wrappers/change_stream.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ Object.defineProperty(module.exports, '__esModule', { value: true });
77

88
module.exports.makeLegacyChangeStream = function (baseClass) {
99
class LegacyChangeStream extends baseClass {
10-
constructor(parent, pipeline, options) {
11-
if (parent instanceof baseClass || parent instanceof LegacyChangeStream) {
12-
super(parent.parent, parent.pipeline, parent.options);
13-
} else {
14-
super(parent, pipeline, options);
15-
}
16-
}
17-
1810
close(callback) {
1911
return maybeCallback(super.close(), callback);
2012
}
@@ -32,7 +24,7 @@ module.exports.makeLegacyChangeStream = function (baseClass) {
3224
Object.defineProperty(baseClass.prototype, toLegacy, {
3325
enumerable: false,
3426
value: function () {
35-
return new LegacyChangeStream(this);
27+
return Object.setPrototypeOf(this, LegacyChangeStream.prototype);
3628
}
3729
});
3830

src/legacy_wrappers/collection.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ Object.defineProperty(module.exports, '__esModule', { value: true });
77

88
module.exports.makeLegacyCollection = function (baseClass) {
99
class LegacyCollection extends baseClass {
10-
constructor(db, name, options) {
11-
if (db instanceof baseClass || db instanceof LegacyCollection) {
12-
super(db.s.db, db.s.namespace.collection, db.s.options);
13-
} else {
14-
super(db, name, options);
15-
}
16-
}
17-
1810
// async APIs
1911
bulkWrite(operations, options, callback) {
2012
callback =
@@ -419,7 +411,7 @@ module.exports.makeLegacyCollection = function (baseClass) {
419411
Object.defineProperty(baseClass.prototype, toLegacy, {
420412
enumerable: false,
421413
value: function () {
422-
return new LegacyCollection(this);
414+
return Object.setPrototypeOf(this, LegacyCollection.prototype);
423415
}
424416
});
425417

src/legacy_wrappers/cursors.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,7 @@ module.exports.makeLegacyFindCursor = function (baseClass) {
116116
};
117117

118118
module.exports.makeLegacyListCollectionsCursor = function (baseClass) {
119-
class LegacyListCollectionsCursor extends baseClass {
120-
constructor(db, filter, options) {
121-
if (db instanceof baseClass) {
122-
super(db.parent, db.filter, db.options);
123-
} else {
124-
super(db, filter, options);
125-
}
126-
}
127-
}
119+
class LegacyListCollectionsCursor extends baseClass {}
128120

129121
for (const [name, method] of commonCursorFunctions) {
130122
Object.defineProperty(LegacyListCollectionsCursor.prototype, name, {
@@ -136,23 +128,15 @@ module.exports.makeLegacyListCollectionsCursor = function (baseClass) {
136128
Object.defineProperty(baseClass.prototype, toLegacy, {
137129
enumerable: false,
138130
value: function () {
139-
return new LegacyListCollectionsCursor(this);
131+
return Object.setPrototypeOf(this, LegacyListCollectionsCursor.prototype);
140132
}
141133
});
142134

143135
return LegacyListCollectionsCursor;
144136
};
145137

146138
module.exports.makeLegacyListIndexesCursor = function (baseClass) {
147-
class LegacyListIndexesCursor extends baseClass {
148-
constructor(collection, options) {
149-
if (collection instanceof baseClass) {
150-
super(collection.parent, collection.options);
151-
} else {
152-
super(collection, options);
153-
}
154-
}
155-
}
139+
class LegacyListIndexesCursor extends baseClass {}
156140

157141
for (const [name, method] of commonCursorFunctions) {
158142
Object.defineProperty(LegacyListIndexesCursor.prototype, name, {
@@ -164,7 +148,7 @@ module.exports.makeLegacyListIndexesCursor = function (baseClass) {
164148
Object.defineProperty(baseClass.prototype, toLegacy, {
165149
enumerable: false,
166150
value: function () {
167-
return new LegacyListIndexesCursor(this);
151+
return Object.setPrototypeOf(this, LegacyListIndexesCursor.prototype);
168152
}
169153
});
170154

src/legacy_wrappers/db.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ Object.defineProperty(module.exports, '__esModule', { value: true });
77

88
module.exports.makeLegacyDb = function (baseClass) {
99
class LegacyDb extends baseClass {
10-
constructor(client, databaseName, options) {
11-
if (client instanceof baseClass || client instanceof LegacyDb) {
12-
super(client.s.client, client.databaseName, client.s.options);
13-
} else {
14-
super(client, databaseName, options);
15-
}
16-
}
17-
1810
command(command, options, callback) {
1911
callback =
2012
typeof callback === 'function'
@@ -187,7 +179,7 @@ module.exports.makeLegacyDb = function (baseClass) {
187179
Object.defineProperty(baseClass.prototype, toLegacy, {
188180
enumerable: false,
189181
value: function () {
190-
return new LegacyDb(this);
182+
return Object.setPrototypeOf(this, LegacyDb.prototype);
191183
}
192184
});
193185

src/legacy_wrappers/gridfs.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ Object.defineProperty(module.exports, '__esModule', { value: true });
77

88
module.exports.makeLegacyGridFSBucket = function (baseClass) {
99
class LegacyGridFSBucket extends baseClass {
10-
constructor(db, options) {
11-
if (db instanceof baseClass || db instanceof LegacyGridFSBucket) {
12-
super(db.s.db, db.s.options);
13-
} else {
14-
super(db, options);
15-
}
16-
}
17-
1810
delete(id, callback) {
1911
return maybeCallback(super.delete(id), callback);
2012
}
@@ -36,7 +28,7 @@ module.exports.makeLegacyGridFSBucket = function (baseClass) {
3628
Object.defineProperty(baseClass.prototype, toLegacy, {
3729
enumerable: false,
3830
value: function () {
39-
return new LegacyGridFSBucket(this);
31+
return Object.setPrototypeOf(this, LegacyGridFSBucket.prototype);
4032
}
4133
});
4234

src/legacy_wrappers/session.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
'use strict';
22

3-
const { toLegacy } = require('../utils');
3+
const { toLegacy, maybeCallback } = require('../utils');
44

55
module.exports = Object.create(null);
66
Object.defineProperty(module.exports, '__esModule', { value: true });
77

88
module.exports.makeLegacyClientSession = function (baseClass) {
99
class LegacyClientSession extends baseClass {
10-
abortTransaction() {
11-
throw new Error('not done yet');
10+
abortTransaction(callback) {
11+
return maybeCallback(super.abortTransaction(), callback);
1212
}
13-
commitTransaction() {
14-
throw new Error('not done yet');
15-
}
16-
endSession() {
17-
throw new Error('not done yet');
13+
14+
commitTransaction(callback) {
15+
return maybeCallback(super.commitTransaction(), callback);
1816
}
19-
withTransaction() {
20-
throw new Error('not done yet');
17+
18+
endSession(options, callback) {
19+
callback =
20+
typeof callback === 'function'
21+
? callback
22+
: typeof options === 'function'
23+
? options
24+
: undefined;
25+
options = typeof options !== 'function' ? options : undefined;
26+
return maybeCallback(super.endSession(options), callback);
2127
}
2228
}
2329

0 commit comments

Comments
 (0)