Skip to content
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

feat(QueryCursor): add getDriverCursor() function that returns the raw driver cursor #14745

Merged
merged 1 commit into from
Jul 15, 2024
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
20 changes: 20 additions & 0 deletions lib/cursor/queryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const eachAsync = require('../helpers/cursor/eachAsync');
const helpers = require('../queryHelpers');
const kareem = require('kareem');
const immediate = require('../helpers/immediate');
const { once } = require('node:events');
const util = require('util');

/**
Expand Down Expand Up @@ -135,6 +136,25 @@ QueryCursor.prototype._read = function() {
});
};

/**
* Returns the underlying cursor from the MongoDB Node driver that this cursor uses.
*
* @method getDriverCursor
* @memberOf QueryCursor
* @returns {Cursor} MongoDB Node driver cursor instance
* @instance
* @api public
*/

QueryCursor.prototype.getDriverCursor = async function getDriverCursor() {
if (this.cursor) {
return this.cursor;
}

await once(this, 'cursor');
return this.cursor;
};

/**
* Registers a transform function which subsequently maps documents retrieved
* via the streams interface or `.next()`
Expand Down
19 changes: 19 additions & 0 deletions test/query.cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,25 @@ describe('QueryCursor', function() {
assert.ok(err);
assert.ok(err.message.includes('skipMiddlewareFunction'), err.message);
});

it('returns the underlying Node driver cursor with getDriverCursor()', async function() {
const schema = new mongoose.Schema({ name: String });

const Movie = db.model('Movie', schema);

await Movie.deleteMany({});
await Movie.create([
{ name: 'Kickboxer' },
{ name: 'Ip Man' },
{ name: 'Enter the Dragon' }
]);

const cursor = await Movie.find({}).cursor();
assert.ok(!cursor.cursor);
const driverCursor = await cursor.getDriverCursor();
assert.ok(cursor.cursor);
assert.equal(driverCursor, cursor.cursor);
});
});

async function delay(ms) {
Expand Down