Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
72 changes: 31 additions & 41 deletions lib/cursor/queryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ function QueryCursor(query) {
if (this.options.transform) {
this._transforms.push(this.options.transform);
}
if (this._mongooseOptions._transformForAsyncIterator) {
this._transforms.push(_transformForAsyncIterator);
}
// Re: gh-8039, you need to set the `cursor.batchSize` option, top-level
// `batchSize` option doesn't work.
if (this.options.batchSize) {
Expand Down Expand Up @@ -382,27 +379,6 @@ QueryCursor.prototype.addCursorFlag = function(flag, value) {
return this;
};

/*!
* ignore
*/

QueryCursor.prototype.transformNull = function(val) {
if (arguments.length === 0) {
val = true;
}
this._mongooseOptions.transformNull = val;
return this;
};

/*!
* ignore
*/

QueryCursor.prototype._transformForAsyncIterator = function() {
this._mongooseOptions._transformForAsyncIterator = true;
return this;
};

/**
* Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js).
* You do not need to call this function explicitly, the JavaScript runtime
Expand Down Expand Up @@ -435,18 +411,11 @@ QueryCursor.prototype._transformForAsyncIterator = function() {

if (Symbol.asyncIterator != null) {
QueryCursor.prototype[Symbol.asyncIterator] = function() {
return this.transformNull()._transformForAsyncIterator();
this._mongooseOptions._asyncIterator = true;
return this;
};
}

/*!
* ignore
*/

function _transformForAsyncIterator(doc) {
return doc == null ? { done: true } : { value: doc, done: false };
}

/**
* Get the next doc from the underlying cursor and mongooseify it
* (populate, etc.)
Expand All @@ -457,16 +426,37 @@ function _transformForAsyncIterator(doc) {

function _next(ctx, cb) {
let callback = cb;
if (ctx._transforms.length) {
callback = function(err, doc) {
if (err || (doc === null && !ctx._mongooseOptions.transformNull)) {
return cb(err, doc);

// Create a custom callback to handle transforms, async iterator, and transformNull
callback = function(err, doc) {
if (err) {
return cb(err);
}

// Handle null documents - if asyncIterator, we need `done: true`, otherwise just
// skip. In either case, avoid transforms.
if (doc === null) {
if (ctx._mongooseOptions._asyncIterator) {
return cb(null, { done: true });
} else {
return cb(null, null);
}
cb(err, ctx._transforms.reduce(function(doc, fn) {
}

// Apply transforms
if (ctx._transforms.length && doc !== null) {
doc = ctx._transforms.reduce(function(doc, fn) {
return fn.call(ctx, doc);
}, doc));
};
}
}, doc);
}

// For async iterator, convert to {value, done} format
if (ctx._mongooseOptions._asyncIterator) {
return cb(null, { value: doc, done: false });
}

return cb(null, doc);
};

if (ctx._error) {
return immediate(function() {
Expand Down
2 changes: 1 addition & 1 deletion test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2358,7 +2358,7 @@ describe('Query', function() {
const Model = db.model('Test', new Schema({ name: String }));

await Model.create({ name: 'test' });
const cursor = Model.find().transform(doc => doc?.name.toUpperCase() ?? null).cursor();
const cursor = Model.find().transform(doc => doc.name.toUpperCase()).cursor();
const names = [];
for await (const name of cursor) {
names.push(name);
Expand Down
Loading