mongoose 8.7.2 → 8.7.3

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.
@@ -196,6 +196,37 @@ AggregationCursor.prototype.close = async function close() {
196
196
  this.emit('close');
197
197
  };
198
198
 
199
+ /**
200
+ * Marks this cursor as destroyed. Will stop streaming and subsequent calls to
201
+ * `next()` will error.
202
+ *
203
+ * @return {this}
204
+ * @api private
205
+ * @method _destroy
206
+ */
207
+
208
+ AggregationCursor.prototype._destroy = function _destroy(_err, callback) {
209
+ let waitForCursor = null;
210
+ if (!this.cursor) {
211
+ waitForCursor = new Promise((resolve) => {
212
+ this.once('cursor', resolve);
213
+ });
214
+ } else {
215
+ waitForCursor = Promise.resolve();
216
+ }
217
+
218
+ waitForCursor
219
+ .then(() => this.cursor.close())
220
+ .then(() => {
221
+ this._closed = true;
222
+ callback();
223
+ })
224
+ .catch(error => {
225
+ callback(error);
226
+ });
227
+ return this;
228
+ };
229
+
199
230
  /**
200
231
  * Get the next document from this cursor. Will return `null` when there are
201
232
  * no documents left.
@@ -238,6 +238,39 @@ QueryCursor.prototype.close = async function close() {
238
238
  }
239
239
  };
240
240
 
241
+ /**
242
+ * Marks this cursor as destroyed. Will stop streaming and subsequent calls to
243
+ * `next()` will error.
244
+ *
245
+ * @return {this}
246
+ * @api private
247
+ * @method _destroy
248
+ */
249
+
250
+ QueryCursor.prototype._destroy = function _destroy(_err, callback) {
251
+ let waitForCursor = null;
252
+ if (!this.cursor) {
253
+ waitForCursor = new Promise((resolve) => {
254
+ this.once('cursor', resolve);
255
+ });
256
+ } else {
257
+ waitForCursor = Promise.resolve();
258
+ }
259
+
260
+ waitForCursor
261
+ .then(() => {
262
+ this.cursor.close();
263
+ })
264
+ .then(() => {
265
+ this._closed = true;
266
+ callback();
267
+ })
268
+ .catch(error => {
269
+ callback(error);
270
+ });
271
+ return this;
272
+ };
273
+
241
274
  /**
242
275
  * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will
243
276
  * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.7.2",
4
+ "version": "8.7.3",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -50,6 +50,12 @@ declare module 'mongoose' {
50
50
  autoIndex?: boolean;
51
51
  /** Set to `false` to disable Mongoose automatically calling `createCollection()` on every model created on this connection. */
52
52
  autoCreate?: boolean;
53
+ /**
54
+ * Sanitizes query filters against [query selector injection attacks](
55
+ * https://thecodebarbarian.com/2014/09/04/defending-against-query-selector-injection-attacks.html
56
+ * ) by wrapping any nested objects that have a property whose name starts with $ in a $eq.
57
+ */
58
+ sanitizeFilter?: boolean;
53
59
  }
54
60
 
55
61
  class Connection extends events.EventEmitter implements SessionStarter {
package/types/cursor.d.ts CHANGED
@@ -26,6 +26,12 @@ declare module 'mongoose' {
26
26
  */
27
27
  close(): Promise<void>;
28
28
 
29
+ /**
30
+ * Destroy this cursor, closing the underlying cursor. Will stop streaming
31
+ * and subsequent calls to `next()` will error.
32
+ */
33
+ destroy(): this;
34
+
29
35
  /**
30
36
  * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will
31
37
  * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even
package/types/index.d.ts CHANGED
@@ -706,6 +706,9 @@ declare module 'mongoose' {
706
706
  [K in keyof T]: FlattenProperty<T[K]>;
707
707
  };
708
708
 
709
+ /**
710
+ * Converts any Buffer properties into mongodb.Binary instances, which is what `lean()` returns
711
+ */
709
712
  export type BufferToBinary<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
710
713
  [K in keyof T]: T[K] extends Buffer
711
714
  ? mongodb.Binary
@@ -718,6 +721,76 @@ declare module 'mongoose' {
718
721
  : BufferToBinary<T[K]>;
719
722
  } : T;
720
723
 
724
+ /**
725
+ * Converts any Buffer properties into { type: 'buffer', data: [1, 2, 3] } format for JSON serialization
726
+ */
727
+ export type BufferToJSON<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
728
+ [K in keyof T]: T[K] extends Buffer
729
+ ? { type: 'buffer', data: number[] }
730
+ : T[K] extends (Buffer | null | undefined)
731
+ ? { type: 'buffer', data: number[] } | null | undefined
732
+ : T[K] extends Types.DocumentArray<infer ItemType>
733
+ ? Types.DocumentArray<BufferToBinary<ItemType>>
734
+ : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
735
+ ? HydratedSingleSubdocument<SubdocType>
736
+ : BufferToBinary<T[K]>;
737
+ } : T;
738
+
739
+ /**
740
+ * Converts any ObjectId properties into strings for JSON serialization
741
+ */
742
+ export type ObjectIdToString<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
743
+ [K in keyof T]: T[K] extends mongodb.ObjectId
744
+ ? string
745
+ : T[K] extends (mongodb.ObjectId | null | undefined)
746
+ ? string | null | undefined
747
+ : T[K] extends Types.DocumentArray<infer ItemType>
748
+ ? Types.DocumentArray<ObjectIdToString<ItemType>>
749
+ : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
750
+ ? HydratedSingleSubdocument<ObjectIdToString<SubdocType>>
751
+ : ObjectIdToString<T[K]>;
752
+ } : T;
753
+
754
+ /**
755
+ * Converts any Date properties into strings for JSON serialization
756
+ */
757
+ export type DateToString<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
758
+ [K in keyof T]: T[K] extends NativeDate
759
+ ? string
760
+ : T[K] extends (NativeDate | null | undefined)
761
+ ? string | null | undefined
762
+ : T[K] extends Types.DocumentArray<infer ItemType>
763
+ ? Types.DocumentArray<DateToString<ItemType>>
764
+ : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
765
+ ? HydratedSingleSubdocument<DateToString<SubdocType>>
766
+ : DateToString<T[K]>;
767
+ } : T;
768
+
769
+ /**
770
+ * Converts any Mongoose subdocuments (single nested or doc arrays) into POJO equivalents
771
+ */
772
+ export type SubdocsToPOJOs<T> = T extends TreatAsPrimitives ? T : T extends Record<string, any> ? {
773
+ [K in keyof T]: T[K] extends NativeDate
774
+ ? string
775
+ : T[K] extends (NativeDate | null | undefined)
776
+ ? string | null | undefined
777
+ : T[K] extends Types.DocumentArray<infer ItemType>
778
+ ? ItemType[]
779
+ : T[K] extends Types.Subdocument<unknown, unknown, infer SubdocType>
780
+ ? SubdocType
781
+ : SubdocsToPOJOs<T[K]>;
782
+ } : T;
783
+
784
+ export type JSONSerialized<T> = SubdocsToPOJOs<
785
+ FlattenMaps<
786
+ BufferToJSON<
787
+ ObjectIdToString<
788
+ DateToString<T>
789
+ >
790
+ >
791
+ >
792
+ >;
793
+
721
794
  /**
722
795
  * Separate type is needed for properties of union type (for example, Types.DocumentArray | undefined) to apply conditional check to each member of it
723
796
  * https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
@@ -146,9 +146,10 @@ type RequiredPathKeys<T, TypeKey extends string = DefaultTypeKey> = {
146
146
  * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
147
147
  * @returns a record contains required paths with the corresponding type.
148
148
  */
149
- type RequiredPaths<T, TypeKey extends string = DefaultTypeKey> = {
150
- [K in RequiredPathKeys<T, TypeKey>]: T[K];
151
- };
149
+ type RequiredPaths<T, TypeKey extends string = DefaultTypeKey> = Pick<
150
+ { -readonly [K in keyof T]: T[K] },
151
+ RequiredPathKeys<T, TypeKey>
152
+ >;
152
153
 
153
154
  /**
154
155
  * @summary A Utility to obtain schema's optional path keys.
@@ -166,9 +167,10 @@ type OptionalPathKeys<T, TypeKey extends string = DefaultTypeKey> = {
166
167
  * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
167
168
  * @returns a record contains optional paths with the corresponding type.
168
169
  */
169
- type OptionalPaths<T, TypeKey extends string = DefaultTypeKey> = {
170
- [K in OptionalPathKeys<T, TypeKey>]?: T[K];
171
- };
170
+ type OptionalPaths<T, TypeKey extends string = DefaultTypeKey> = Pick<
171
+ { -readonly [K in keyof T]?: T[K] },
172
+ OptionalPathKeys<T, TypeKey>
173
+ >;
172
174
 
173
175
  /**
174
176
  * @summary Allows users to optionally choose their own type for a schema field for stronger typing.
package/types/models.d.ts CHANGED
@@ -64,12 +64,9 @@ declare module 'mongoose' {
64
64
  throwOnValidationError?: boolean;
65
65
  }
66
66
 
67
- type InsertManyResult<T> = mongodb.InsertManyResult<T> & {
68
- insertedIds: {
69
- [key: number]: InferId<T>;
70
- };
67
+ interface InsertManyResult<T> extends mongodb.InsertManyResult<T> {
71
68
  mongoose?: { validationErrors?: Array<Error.CastError | Error.ValidatorError> };
72
- };
69
+ }
73
70
 
74
71
  type UpdateWriteOpResult = mongodb.UpdateResult;
75
72
  type UpdateResult = mongodb.UpdateResult;