Skip to content

Commit 583741a

Browse files
nbbeekenW-A-James
andauthored
fix(NODE-5003): cloned cursors are not legacy api (#12)
Co-authored-by: Warren James <[email protected]>
1 parent f69b5fe commit 583741a

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

src/legacy_wrappers/cursors.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ module.exports = Object.create(null);
77
Object.defineProperty(module.exports, '__esModule', { value: true });
88

99
module.exports.makeLegacyFindCursor = function (baseClass) {
10+
function toLegacyHelper(cursor) {
11+
Object.setPrototypeOf(cursor, LegacyFindCursor.prototype);
12+
return cursor;
13+
}
14+
1015
class LegacyFindCursor extends baseClass {
1116
/** @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead */
1217
count(options, callback) {
@@ -56,19 +61,28 @@ module.exports.makeLegacyFindCursor = function (baseClass) {
5661
tryNext(callback) {
5762
return maybeCallback(super.tryNext(), callback);
5863
}
64+
clone() {
65+
const cursor = super.clone();
66+
return toLegacyHelper(cursor);
67+
}
5968
}
6069

6170
Object.defineProperty(baseClass.prototype, toLegacy, {
6271
enumerable: false,
6372
value: function () {
64-
return Object.setPrototypeOf(this, LegacyFindCursor.prototype);
73+
return toLegacyHelper(this);
6574
}
6675
});
6776

6877
return LegacyFindCursor;
6978
};
7079

7180
module.exports.makeLegacyListCollectionsCursor = function (baseClass) {
81+
function toLegacyHelper(cursor) {
82+
Object.setPrototypeOf(cursor, LegacyListCollectionsCursor.prototype);
83+
return cursor;
84+
}
85+
7286
class LegacyListCollectionsCursor extends baseClass {
7387
close(options, callback) {
7488
callback =
@@ -95,19 +109,28 @@ module.exports.makeLegacyListCollectionsCursor = function (baseClass) {
95109
tryNext(callback) {
96110
return maybeCallback(super.tryNext(), callback);
97111
}
112+
clone() {
113+
const cursor = super.clone();
114+
return toLegacyHelper(cursor);
115+
}
98116
}
99117

100118
Object.defineProperty(baseClass.prototype, toLegacy, {
101119
enumerable: false,
102120
value: function () {
103-
return Object.setPrototypeOf(this, LegacyListCollectionsCursor.prototype);
121+
return toLegacyHelper(this);
104122
}
105123
});
106124

107125
return LegacyListCollectionsCursor;
108126
};
109127

110128
module.exports.makeLegacyListIndexesCursor = function (baseClass) {
129+
function toLegacyHelper(cursor) {
130+
Object.setPrototypeOf(cursor, LegacyListIndexesCursor.prototype);
131+
return cursor;
132+
}
133+
111134
class LegacyListIndexesCursor extends baseClass {
112135
close(options, callback) {
113136
callback =
@@ -134,19 +157,28 @@ module.exports.makeLegacyListIndexesCursor = function (baseClass) {
134157
tryNext(callback) {
135158
return maybeCallback(super.tryNext(), callback);
136159
}
160+
clone() {
161+
const cursor = super.clone();
162+
return toLegacyHelper(cursor);
163+
}
137164
}
138165

139166
Object.defineProperty(baseClass.prototype, toLegacy, {
140167
enumerable: false,
141168
value: function () {
142-
return Object.setPrototypeOf(this, LegacyListIndexesCursor.prototype);
169+
return toLegacyHelper(this);
143170
}
144171
});
145172

146173
return LegacyListIndexesCursor;
147174
};
148175

149176
module.exports.makeLegacyAggregationCursor = function (baseClass) {
177+
function toLegacyHelper(cursor) {
178+
Object.setPrototypeOf(cursor, LegacyAggregationCursor.prototype);
179+
return cursor;
180+
}
181+
150182
class LegacyAggregationCursor extends baseClass {
151183
explain(verbosity, callback) {
152184
callback =
@@ -184,12 +216,16 @@ module.exports.makeLegacyAggregationCursor = function (baseClass) {
184216
tryNext(callback) {
185217
return maybeCallback(super.tryNext(), callback);
186218
}
219+
clone() {
220+
const cursor = super.clone();
221+
return toLegacyHelper(cursor);
222+
}
187223
}
188224

189225
Object.defineProperty(baseClass.prototype, toLegacy, {
190226
enumerable: false,
191227
value: function () {
192-
return Object.setPrototypeOf(this, LegacyAggregationCursor.prototype);
228+
return toLegacyHelper(this);
193229
}
194230
});
195231

test/tools/api.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ const api = [
4040
{ className: 'Admin', method: 'validateCollection', returnType: 'Promise<Document>' },
4141

4242
{ className: 'AggregationCursor', method: 'explain', returnType: 'Promise<Document>' },
43+
{ className: 'AggregationCursor', method: 'clone', returnType: 'AggregationCursor', notAsync: true },
44+
45+
{ className: 'FindCursor', method: 'clone', returnType: 'FindCursor', notAsync: true },
46+
{ className: 'ListIndexesCursor', method: 'clone', returnType: 'ListIndexesCursor', notAsync: true },
47+
{ className: 'ListCollectionsCursor', method: 'clone', returnType: 'ListCollectionsCursor', notAsync: true },
48+
4349

4450
// Super class of Unordered/Ordered Bulk operations
4551
// This is listed here as a reference for completeness, but it is tested by the subclass overrides of execute

test/unit/legacy_wrappers/collection.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ describe('legacy_wrappers/collection.js', () => {
4343
expect(collection.find()).to.be.instanceOf(LegacyFindCursor);
4444
});
4545

46+
it('collection.listIndexes().clone() should return legacy listIndexes cursor', () => {
47+
expect(collection.listIndexes().clone()).to.be.instanceOf(LegacyListIndexesCursor);
48+
});
49+
50+
it('collection.aggregate().clone() should return legacy AggregationCursor', () => {
51+
expect(collection.aggregate().clone()).to.be.instanceOf(LegacyAggregationCursor);
52+
});
53+
54+
it('collection.find().clone() should return legacy FindCursor', () => {
55+
expect(collection.find().clone()).to.be.instanceOf(LegacyFindCursor);
56+
});
57+
4658
describe('rename()', () => {
4759
let client;
4860
let collection;

test/unit/legacy_wrappers/db.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ describe('legacy_wrappers/db.js', () => {
2828
expect(db.listCollections()).to.be.instanceOf(LegacyListCollectionsCursor);
2929
});
3030

31+
it('db.listCollections().clone() should return legacy listCollections cursor', () => {
32+
expect(db.listCollections().clone()).to.be.instanceOf(LegacyListCollectionsCursor);
33+
});
34+
3135
it('should return legacy ChangeStream', () => {
3236
expect(db.watch()).to.be.instanceOf(LegacyChangeStream);
3337
});

0 commit comments

Comments
 (0)