mongoose 8.15.0 → 8.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -83,6 +83,9 @@ function QueryCursor(query) {
83
83
  // Max out the number of documents we'll populate in parallel at 5000.
84
84
  this.options._populateBatchSize = Math.min(this.options.batchSize, 5000);
85
85
  }
86
+ if (query._mongooseOptions._asyncIterator) {
87
+ this._mongooseOptions._asyncIterator = true;
88
+ }
86
89
 
87
90
  if (model.collection._shouldBufferCommands() && model.collection.buffer) {
88
91
  model.collection.queue.push([
@@ -379,29 +382,6 @@ QueryCursor.prototype.addCursorFlag = function(flag, value) {
379
382
  return this;
380
383
  };
381
384
 
382
- /*!
383
- * ignore
384
- */
385
-
386
- QueryCursor.prototype.transformNull = function(val) {
387
- if (arguments.length === 0) {
388
- val = true;
389
- }
390
- this._mongooseOptions.transformNull = val;
391
- return this;
392
- };
393
-
394
- /*!
395
- * ignore
396
- */
397
-
398
- QueryCursor.prototype._transformForAsyncIterator = function() {
399
- if (this._transforms.indexOf(_transformForAsyncIterator) === -1) {
400
- this.map(_transformForAsyncIterator);
401
- }
402
- return this;
403
- };
404
-
405
385
  /**
406
386
  * Returns an asyncIterator for use with [`for/await/of` loops](https://thecodebarbarian.com/getting-started-with-async-iterators-in-node-js).
407
387
  * You do not need to call this function explicitly, the JavaScript runtime
@@ -433,19 +413,13 @@ QueryCursor.prototype._transformForAsyncIterator = function() {
433
413
  */
434
414
 
435
415
  if (Symbol.asyncIterator != null) {
436
- QueryCursor.prototype[Symbol.asyncIterator] = function() {
437
- return this.transformNull()._transformForAsyncIterator();
416
+ QueryCursor.prototype[Symbol.asyncIterator] = function queryCursorAsyncIterator() {
417
+ // Set so QueryCursor knows it should transform results for async iterators into `{ value, done }` syntax
418
+ this._mongooseOptions._asyncIterator = true;
419
+ return this;
438
420
  };
439
421
  }
440
422
 
441
- /*!
442
- * ignore
443
- */
444
-
445
- function _transformForAsyncIterator(doc) {
446
- return doc == null ? { done: true } : { value: doc, done: false };
447
- }
448
-
449
423
  /**
450
424
  * Get the next doc from the underlying cursor and mongooseify it
451
425
  * (populate, etc.)
@@ -456,16 +430,38 @@ function _transformForAsyncIterator(doc) {
456
430
 
457
431
  function _next(ctx, cb) {
458
432
  let callback = cb;
459
- if (ctx._transforms.length) {
460
- callback = function(err, doc) {
461
- if (err || (doc === null && !ctx._mongooseOptions.transformNull)) {
462
- return cb(err, doc);
433
+
434
+ // Create a custom callback to handle transforms, async iterator, and transformNull
435
+ callback = function(err, doc) {
436
+ if (err) {
437
+ return cb(err);
438
+ }
439
+
440
+ // Handle null documents - if asyncIterator, we need to return `done: true`, otherwise just
441
+ // skip. In either case, avoid transforms.
442
+ if (doc === null) {
443
+ if (ctx._mongooseOptions._asyncIterator) {
444
+ return cb(null, { done: true });
445
+ } else {
446
+ return cb(null, null);
463
447
  }
464
- cb(err, ctx._transforms.reduce(function(doc, fn) {
448
+ }
449
+
450
+ // Apply transforms
451
+ if (ctx._transforms.length && doc !== null) {
452
+ doc = ctx._transforms.reduce(function(doc, fn) {
465
453
  return fn.call(ctx, doc);
466
- }, doc));
467
- };
468
- }
454
+ }, doc);
455
+ }
456
+
457
+ // This option is set in `Symbol.asyncIterator` code paths.
458
+ // For async iterator, we need to convert to {value, done} format
459
+ if (ctx._mongooseOptions._asyncIterator) {
460
+ return cb(null, { value: doc, done: false });
461
+ }
462
+
463
+ return cb(null, doc);
464
+ };
469
465
 
470
466
  if (ctx._error) {
471
467
  return immediate(function() {
package/lib/query.js CHANGED
@@ -5476,8 +5476,10 @@ Query.prototype.nearSphere = function() {
5476
5476
  */
5477
5477
 
5478
5478
  if (Symbol.asyncIterator != null) {
5479
- Query.prototype[Symbol.asyncIterator] = function() {
5480
- return this.cursor().transformNull()._transformForAsyncIterator();
5479
+ Query.prototype[Symbol.asyncIterator] = function queryAsyncIterator() {
5480
+ // Set so QueryCursor knows it should transform results for async iterators into `{ value, done }` syntax
5481
+ this._mongooseOptions._asyncIterator = true;
5482
+ return this.cursor();
5481
5483
  };
5482
5484
  }
5483
5485
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.15.0",
4
+ "version": "8.15.1",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
package/types/index.d.ts CHANGED
@@ -684,10 +684,16 @@ declare module 'mongoose' {
684
684
  }
685
685
  : Element;
686
686
  type _IDType = { _id?: boolean | 1 | 0 };
687
- export type InclusionProjection<T> = IsItRecordAndNotAny<T> extends true ? Projector<WithLevel1NestedPaths<T>, true | 1> & _IDType : AnyObject;
688
- export type ExclusionProjection<T> = IsItRecordAndNotAny<T> extends true ? Projector<WithLevel1NestedPaths<T>, false | 0> & _IDType : AnyObject;
689
-
690
- export type ProjectionType<T> = (InclusionProjection<T> & AnyObject) | (ExclusionProjection<T> & AnyObject) | string;
687
+ export type InclusionProjection<T> = IsItRecordAndNotAny<T> extends true
688
+ ? Omit<Projector<WithLevel1NestedPaths<T>, true | 1>, '_id'> & _IDType
689
+ : AnyObject;
690
+ export type ExclusionProjection<T> = IsItRecordAndNotAny<T> extends true
691
+ ? Omit<Projector<WithLevel1NestedPaths<T>, false | 0>, '_id'> & _IDType
692
+ : AnyObject;
693
+
694
+ export type ProjectionType<T> = (InclusionProjection<T> & AnyObject)
695
+ | (ExclusionProjection<T> & AnyObject)
696
+ | string;
691
697
  export type SortValues = SortOrder;
692
698
 
693
699
  export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';
@@ -319,10 +319,11 @@ declare module 'mongoose' {
319
319
  export interface VectorSearch {
320
320
  /** [`$vectorSearch` reference](https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/) */
321
321
  $vectorSearch: {
322
+ exact?: boolean;
322
323
  index: string,
323
324
  path: string,
324
325
  queryVector: number[],
325
- numCandidates: number,
326
+ numCandidates?: number,
326
327
  limit: number,
327
328
  filter?: Expression,
328
329
  }