Skip to content

fix(NODE-5003): cloned cursors are not legacy api #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/legacy_wrappers/cursors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ module.exports = Object.create(null);
Object.defineProperty(module.exports, '__esModule', { value: true });

module.exports.makeLegacyFindCursor = function (baseClass) {
function toLegacyHelper(cursor) {
Object.setPrototypeOf(cursor, LegacyFindCursor.prototype);
return cursor;
}

class LegacyFindCursor extends baseClass {
/** @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead */
count(options, callback) {
Expand Down Expand Up @@ -56,19 +61,28 @@ module.exports.makeLegacyFindCursor = function (baseClass) {
tryNext(callback) {
return maybeCallback(super.tryNext(), callback);
}
clone() {
const cursor = super.clone();
return toLegacyHelper(cursor);
}
}

Object.defineProperty(baseClass.prototype, toLegacy, {
enumerable: false,
value: function () {
return Object.setPrototypeOf(this, LegacyFindCursor.prototype);
return toLegacyHelper(this);
}
});

return LegacyFindCursor;
};

module.exports.makeLegacyListCollectionsCursor = function (baseClass) {
function toLegacyHelper(cursor) {
Object.setPrototypeOf(cursor, LegacyListCollectionsCursor.prototype);
return cursor;
}

class LegacyListCollectionsCursor extends baseClass {
close(options, callback) {
callback =
Expand All @@ -95,19 +109,28 @@ module.exports.makeLegacyListCollectionsCursor = function (baseClass) {
tryNext(callback) {
return maybeCallback(super.tryNext(), callback);
}
clone() {
const cursor = super.clone();
return toLegacyHelper(cursor);
}
}

Object.defineProperty(baseClass.prototype, toLegacy, {
enumerable: false,
value: function () {
return Object.setPrototypeOf(this, LegacyListCollectionsCursor.prototype);
return toLegacyHelper(this);
}
});

return LegacyListCollectionsCursor;
};

module.exports.makeLegacyListIndexesCursor = function (baseClass) {
function toLegacyHelper(cursor) {
Object.setPrototypeOf(cursor, LegacyListIndexesCursor.prototype);
return cursor;
}

class LegacyListIndexesCursor extends baseClass {
close(options, callback) {
callback =
Expand All @@ -134,19 +157,28 @@ module.exports.makeLegacyListIndexesCursor = function (baseClass) {
tryNext(callback) {
return maybeCallback(super.tryNext(), callback);
}
clone() {
const cursor = super.clone();
return toLegacyHelper(cursor);
}
}

Object.defineProperty(baseClass.prototype, toLegacy, {
enumerable: false,
value: function () {
return Object.setPrototypeOf(this, LegacyListIndexesCursor.prototype);
return toLegacyHelper(this);
}
});

return LegacyListIndexesCursor;
};

module.exports.makeLegacyAggregationCursor = function (baseClass) {
function toLegacyHelper(cursor) {
Object.setPrototypeOf(cursor, LegacyAggregationCursor.prototype);
return cursor;
}

class LegacyAggregationCursor extends baseClass {
explain(verbosity, callback) {
callback =
Expand Down Expand Up @@ -184,12 +216,16 @@ module.exports.makeLegacyAggregationCursor = function (baseClass) {
tryNext(callback) {
return maybeCallback(super.tryNext(), callback);
}
clone() {
const cursor = super.clone();
return toLegacyHelper(cursor);
}
}

Object.defineProperty(baseClass.prototype, toLegacy, {
enumerable: false,
value: function () {
return Object.setPrototypeOf(this, LegacyAggregationCursor.prototype);
return toLegacyHelper(this);
}
});

Expand Down
6 changes: 6 additions & 0 deletions test/tools/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ const api = [
{ className: 'Admin', method: 'validateCollection', returnType: 'Promise<Document>' },

{ className: 'AggregationCursor', method: 'explain', returnType: 'Promise<Document>' },
{ className: 'AggregationCursor', method: 'clone', returnType: 'AggregationCursor', notAsync: true },

{ className: 'FindCursor', method: 'clone', returnType: 'FindCursor', notAsync: true },
{ className: 'ListIndexesCursor', method: 'clone', returnType: 'ListIndexesCursor', notAsync: true },
{ className: 'ListCollectionsCursor', method: 'clone', returnType: 'ListCollectionsCursor', notAsync: true },


// Super class of Unordered/Ordered Bulk operations
// This is listed here as a reference for completeness, but it is tested by the subclass overrides of execute
Expand Down
12 changes: 12 additions & 0 deletions test/unit/legacy_wrappers/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ describe('legacy_wrappers/collection.js', () => {
expect(collection.find()).to.be.instanceOf(LegacyFindCursor);
});

it('collection.listIndexes().clone() should return legacy listIndexes cursor', () => {
expect(collection.listIndexes().clone()).to.be.instanceOf(LegacyListIndexesCursor);
});

it('collection.aggregate().clone() should return legacy AggregationCursor', () => {
expect(collection.aggregate().clone()).to.be.instanceOf(LegacyAggregationCursor);
});

it('collection.find().clone() should return legacy FindCursor', () => {
expect(collection.find().clone()).to.be.instanceOf(LegacyFindCursor);
});

describe('rename()', () => {
let client;
let collection;
Expand Down
4 changes: 4 additions & 0 deletions test/unit/legacy_wrappers/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ describe('legacy_wrappers/db.js', () => {
expect(db.listCollections()).to.be.instanceOf(LegacyListCollectionsCursor);
});

it('db.listCollections().clone() should return legacy listCollections cursor', () => {
expect(db.listCollections().clone()).to.be.instanceOf(LegacyListCollectionsCursor);
});

it('should return legacy ChangeStream', () => {
expect(db.watch()).to.be.instanceOf(LegacyChangeStream);
});
Expand Down