Skip to content

Commit

Permalink
feat(QueryCursor): add getDriverCursor() function that returns the ra…
Browse files Browse the repository at this point in the history
…w driver cursor
  • Loading branch information
vkarpov15 committed Jul 12, 2024
1 parent 70e8e19 commit 22b41df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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

0 comments on commit 22b41df

Please sign in to comment.