mongoose 8.9.6 → 8.10.0

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.
package/lib/schemaType.js CHANGED
@@ -1555,7 +1555,7 @@ SchemaType._isRef = function(self, value, doc, init) {
1555
1555
  * ignore
1556
1556
  */
1557
1557
 
1558
- SchemaType.prototype._castRef = function _castRef(value, doc, init) {
1558
+ SchemaType.prototype._castRef = function _castRef(value, doc, init, options) {
1559
1559
  if (value == null) {
1560
1560
  return value;
1561
1561
  }
@@ -1587,7 +1587,7 @@ SchemaType.prototype._castRef = function _castRef(value, doc, init) {
1587
1587
  !doc.$__.populated[path].options.options ||
1588
1588
  !doc.$__.populated[path].options.options.lean) {
1589
1589
  const PopulatedModel = pop ? pop.options[populateModelSymbol] : doc.constructor.db.model(this.options.ref);
1590
- ret = new PopulatedModel(value);
1590
+ ret = PopulatedModel.hydrate(value, null, options);
1591
1591
  ret.$__.wasPopulated = { value: ret._doc._id, options: { [populateModelSymbol]: PopulatedModel } };
1592
1592
  }
1593
1593
 
@@ -1771,6 +1771,18 @@ SchemaType.prototype.getEmbeddedSchemaType = function getEmbeddedSchemaType() {
1771
1771
 
1772
1772
  SchemaType.prototype._duplicateKeyErrorMessage = null;
1773
1773
 
1774
+ /**
1775
+ * Returns this schema type's representation in a JSON schema.
1776
+ *
1777
+ * @param [options]
1778
+ * @param [options.useBsonType=false] If true, return a representation with `bsonType` for use with MongoDB's `$jsonSchema`.
1779
+ * @returns {Object} JSON schema properties
1780
+ */
1781
+
1782
+ SchemaType.prototype.toJSONSchema = function toJSONSchema() {
1783
+ throw new Error('Converting unsupported SchemaType to JSON Schema: ' + this.instance);
1784
+ };
1785
+
1774
1786
  /*!
1775
1787
  * Module exports.
1776
1788
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.9.6",
4
+ "version": "8.10.0",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -17,24 +17,26 @@
17
17
  "orm",
18
18
  "db"
19
19
  ],
20
+ "type": "commonjs",
20
21
  "license": "MIT",
21
22
  "dependencies": {
22
23
  "bson": "^6.10.1",
23
24
  "kareem": "2.6.3",
24
- "mongodb": "~6.12.0",
25
+ "mongodb": "~6.13.0",
25
26
  "mpath": "0.9.0",
26
27
  "mquery": "5.0.0",
27
28
  "ms": "2.1.3",
28
29
  "sift": "17.1.3"
29
30
  },
30
31
  "devDependencies": {
31
- "@babel/core": "7.26.0",
32
- "@babel/preset-env": "7.26.0",
32
+ "@babel/core": "7.26.7",
33
+ "@babel/preset-env": "7.26.7",
33
34
  "@typescript-eslint/eslint-plugin": "^8.19.1",
34
35
  "@typescript-eslint/parser": "^8.19.1",
35
36
  "acquit": "1.3.0",
36
37
  "acquit-ignore": "0.2.1",
37
38
  "acquit-require": "0.1.1",
39
+ "ajv": "8.17.1",
38
40
  "assert-browserify": "2.0.0",
39
41
  "babel-loader": "8.2.5",
40
42
  "broken-link-checker": "^0.7.8",
@@ -46,14 +48,14 @@
46
48
  "eslint-plugin-markdown": "^5.1.0",
47
49
  "eslint-plugin-mocha-no-only": "1.2.0",
48
50
  "express": "^4.19.2",
49
- "fs-extra": "~11.2.0",
51
+ "fs-extra": "~11.3.0",
50
52
  "highlight.js": "11.11.1",
51
53
  "lodash.isequal": "4.5.0",
52
54
  "lodash.isequalwith": "4.4.0",
53
55
  "markdownlint-cli2": "^0.17.1",
54
- "marked": "15.0.4",
56
+ "marked": "15.0.6",
55
57
  "mkdirp": "^3.0.1",
56
- "mocha": "11.0.1",
58
+ "mocha": "11.1.0",
57
59
  "moment": "2.30.1",
58
60
  "mongodb-memory-server": "10.1.3",
59
61
  "ncp": "^2.0.0",
@@ -63,8 +65,8 @@
63
65
  "sinon": "19.0.2",
64
66
  "stream-browserify": "3.0.0",
65
67
  "tsd": "0.31.2",
66
- "typescript": "5.7.2",
67
- "uuid": "11.0.3",
68
+ "typescript": "5.7.3",
69
+ "uuid": "11.0.5",
68
70
  "webpack": "5.97.1"
69
71
  },
70
72
  "directories": {
@@ -102,6 +104,8 @@
102
104
  "test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys --allow-write ./test/deno.js",
103
105
  "test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
104
106
  "test-tsd": "node ./test/types/check-types-filename && tsd",
107
+ "setup-test-encryption": "bash scripts/configure-cluster-with-encryption.sh",
108
+ "test-encryption": "mocha --exit ./test/encryption/*.test.js",
105
109
  "tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
106
110
  "test-coverage": "nyc --reporter=html --reporter=text npm test",
107
111
  "ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check"
@@ -59,6 +59,8 @@ declare module 'mongoose' {
59
59
  }
60
60
 
61
61
  class Connection extends events.EventEmitter implements SessionStarter {
62
+ aggregate<ResultType = unknown>(pipeline?: PipelineStage[] | null, options?: AggregateOptions): Aggregate<Array<ResultType>>;
63
+
62
64
  /** Returns a promise that resolves when this connection successfully connects to MongoDB */
63
65
  asPromise(): Promise<this>;
64
66
 
@@ -256,21 +256,21 @@ declare module 'mongoose' {
256
256
  set(value: string | Record<string, any>): this;
257
257
 
258
258
  /** The return value of this method is used in calls to JSON.stringify(doc). */
259
- toJSON(options?: ToObjectOptions & { flattenMaps?: true, flattenObjectIds?: false }): FlattenMaps<Require_id<DocType>>;
260
- toJSON(options: ToObjectOptions & { flattenObjectIds: false }): FlattenMaps<Require_id<DocType>>;
261
- toJSON(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<FlattenMaps<Require_id<DocType>>>;
262
- toJSON(options: ToObjectOptions & { flattenMaps: false }): Require_id<DocType>;
263
- toJSON(options: ToObjectOptions & { flattenMaps: false; flattenObjectIds: true }): ObjectIdToString<Require_id<DocType>>;
264
-
265
- toJSON<T = Require_id<DocType>>(options?: ToObjectOptions & { flattenMaps?: true, flattenObjectIds?: false }): FlattenMaps<T>;
266
- toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenObjectIds: false }): FlattenMaps<T>;
267
- toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<FlattenMaps<T>>;
268
- toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenMaps: false }): T;
269
- toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenMaps: false; flattenObjectIds: true }): ObjectIdToString<T>;
259
+ toJSON(options?: ToObjectOptions & { flattenMaps?: true, flattenObjectIds?: false }): FlattenMaps<Default__v<Require_id<DocType>>>;
260
+ toJSON(options: ToObjectOptions & { flattenObjectIds: false }): FlattenMaps<Default__v<Require_id<DocType>>>;
261
+ toJSON(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<FlattenMaps<Default__v<Require_id<DocType>>>>;
262
+ toJSON(options: ToObjectOptions & { flattenMaps: false }): Default__v<Require_id<DocType>>;
263
+ toJSON(options: ToObjectOptions & { flattenMaps: false; flattenObjectIds: true }): ObjectIdToString<Default__v<Require_id<DocType>>>;
264
+
265
+ toJSON<T = Default__v<Require_id<DocType>>>(options?: ToObjectOptions & { flattenMaps?: true, flattenObjectIds?: false }): FlattenMaps<T>;
266
+ toJSON<T = Default__v<Require_id<DocType>>>(options: ToObjectOptions & { flattenObjectIds: false }): FlattenMaps<T>;
267
+ toJSON<T = Default__v<Require_id<DocType>>>(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<FlattenMaps<T>>;
268
+ toJSON<T = Default__v<Require_id<DocType>>>(options: ToObjectOptions & { flattenMaps: false }): T;
269
+ toJSON<T = Default__v<Require_id<DocType>>>(options: ToObjectOptions & { flattenMaps: false; flattenObjectIds: true }): ObjectIdToString<T>;
270
270
 
271
271
  /** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
272
- toObject(options?: ToObjectOptions): Require_id<DocType>;
273
- toObject<T>(options?: ToObjectOptions): Require_id<T>;
272
+ toObject(options?: ToObjectOptions): Default__v<Require_id<DocType>>;
273
+ toObject<T>(options?: ToObjectOptions): Default__v<Require_id<T>>;
274
274
 
275
275
  /** Clears the modified state on the specified path. */
276
276
  unmarkModified<T extends keyof DocType>(path: T): void;
@@ -2301,6 +2301,18 @@ declare module 'mongoose' {
2301
2301
  }
2302
2302
  }
2303
2303
 
2304
+ export interface Median {
2305
+ /**
2306
+ * Returns an approximation of the median, the 50th percentile, as a scalar value.
2307
+ *
2308
+ * @see https://www.mongodb.com/docs/v7.0/reference/operator/aggregation/median/
2309
+ */
2310
+ $median: {
2311
+ input: number | Expression,
2312
+ method: 'approximate'
2313
+ }
2314
+ }
2315
+
2304
2316
  export interface StdDevPop {
2305
2317
  /**
2306
2318
  * Calculates the population standard deviation of the input values. Use if the values encompass the entire
@@ -2859,6 +2871,7 @@ declare module 'mongoose' {
2859
2871
  Expression.Locf |
2860
2872
  Expression.Max |
2861
2873
  Expression.MaxN |
2874
+ Expression.Median |
2862
2875
  Expression.Min |
2863
2876
  Expression.MinN |
2864
2877
  Expression.Push |
@@ -2891,6 +2904,7 @@ declare module 'mongoose' {
2891
2904
  Expression.ExpMovingAvg |
2892
2905
  Expression.Integral |
2893
2906
  Expression.Max |
2907
+ Expression.Median |
2894
2908
  Expression.Min |
2895
2909
  Expression.StdDevPop |
2896
2910
  Expression.StdDevSamp |
@@ -2963,6 +2977,7 @@ declare module 'mongoose' {
2963
2977
  Expression.LastN |
2964
2978
  Expression.Max |
2965
2979
  Expression.MaxN |
2980
+ Expression.Median |
2966
2981
  Expression.MergeObjects |
2967
2982
  Expression.Min |
2968
2983
  Expression.MinN |
package/types/index.d.ts CHANGED
@@ -508,6 +508,8 @@ declare module 'mongoose' {
508
508
  statics: { [F in keyof TStaticMethods]: TStaticMethods[F] } &
509
509
  { [name: string]: (this: TModelType, ...args: any[]) => unknown };
510
510
 
511
+ toJSONSchema(options?: { useBsonType?: boolean }): Record<string, any>;
512
+
511
513
  /** Creates a virtual type with the given name. */
512
514
  virtual<T = HydratedDocument<DocType, TVirtuals & TInstanceMethods, TQueryHelpers>>(
513
515
  name: keyof TVirtuals | string,
@@ -827,7 +829,7 @@ declare module 'mongoose' {
827
829
  ? Types.DocumentArray<FlattenMaps<ItemType>> : FlattenMaps<T>;
828
830
 
829
831
  export type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;
830
- export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId | Buffer | Function | mongodb.Binary;
832
+ export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId | Buffer | Function | mongodb.Binary | mongodb.ClientSession;
831
833
 
832
834
  export type SchemaDefinitionType<T> = T extends Document ? Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>> : T;
833
835
 
package/types/models.d.ts CHANGED
@@ -577,6 +577,13 @@ declare module 'mongoose' {
577
577
  Array<MergeType<THydratedDocumentType, Omit<DocContents, '_id'>>>
578
578
  >;
579
579
 
580
+ /**
581
+ * Shortcut for saving one document to the database.
582
+ * `MyModel.insertOne(obj, options)` is almost equivalent to `new MyModel(obj).save(options)`.
583
+ * The difference is that `insertOne()` checks if `obj` is already a document, and checks for discriminators.
584
+ */
585
+ insertOne<DocContents = AnyKeys<TRawDocType>>(doc: DocContents | TRawDocType, options?: SaveOptions): Promise<THydratedDocumentType>;
586
+
580
587
  /**
581
588
  * List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection.
582
589
  * This function only works when connected to MongoDB Atlas.
@@ -608,6 +615,13 @@ declare module 'mongoose' {
608
615
  */
609
616
  updateSearchIndex(name: string, definition: AnyObject): Promise<void>;
610
617
 
618
+ /**
619
+ * Changes the Connection instance this model uses to make requests to MongoDB.
620
+ * This function is most useful for changing the Connection that a Model defined using `mongoose.model()` uses
621
+ * after initialization.
622
+ */
623
+ useConnection(connection: Connection): this;
624
+
611
625
  /** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */
612
626
  validate(): Promise<void>;
613
627
  validate(obj: any): Promise<void>;
@@ -869,17 +883,20 @@ declare module 'mongoose' {
869
883
 
870
884
  /** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */
871
885
  updateMany<ResultDoc = THydratedDocumentType>(
872
- filter?: RootFilterQuery<TRawDocType>,
873
- update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
886
+ filter: RootFilterQuery<TRawDocType>,
887
+ update: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
874
888
  options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null
875
889
  ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateMany', TInstanceMethods & TVirtuals>;
876
890
 
877
891
  /** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */
878
892
  updateOne<ResultDoc = THydratedDocumentType>(
879
- filter?: RootFilterQuery<TRawDocType>,
880
- update?: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
893
+ filter: RootFilterQuery<TRawDocType>,
894
+ update: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline,
881
895
  options?: (mongodb.UpdateOptions & MongooseUpdateQueryOptions<TRawDocType>) | null
882
896
  ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateOne', TInstanceMethods & TVirtuals>;
897
+ updateOne<ResultDoc = THydratedDocumentType>(
898
+ update: UpdateQuery<TRawDocType> | UpdateWithAggregationPipeline
899
+ ): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'updateOne', TInstanceMethods & TVirtuals>;
883
900
 
884
901
  /** Creates a Query, applies the passed conditions, and returns the Query. */
885
902
  where<ResultDoc = THydratedDocumentType>(
package/types/query.d.ts CHANGED
@@ -850,20 +850,26 @@ declare module 'mongoose' {
850
850
  * the `multi` option.
851
851
  */
852
852
  updateMany(
853
- filter?: RootFilterQuery<RawDocType>,
854
- update?: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline,
853
+ filter: RootFilterQuery<RawDocType>,
854
+ update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline,
855
855
  options?: QueryOptions<DocType> | null
856
856
  ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateMany', TDocOverrides>;
857
+ updateMany(
858
+ update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline
859
+ ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateMany', TDocOverrides>;
857
860
 
858
861
  /**
859
862
  * Declare and/or execute this query as an updateOne() operation. Same as
860
863
  * `update()`, except it does not support the `multi` or `overwrite` options.
861
864
  */
862
865
  updateOne(
863
- filter?: RootFilterQuery<RawDocType>,
864
- update?: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline,
866
+ filter: RootFilterQuery<RawDocType>,
867
+ update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline,
865
868
  options?: QueryOptions<DocType> | null
866
869
  ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateOne', TDocOverrides>;
870
+ updateOne(
871
+ update: UpdateQuery<RawDocType> | UpdateWithAggregationPipeline
872
+ ): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType, 'updateOne', TDocOverrides>;
867
873
 
868
874
  /**
869
875
  * Sets the specified number of `mongod` servers, or tag set of `mongod` servers,
@@ -300,6 +300,8 @@ declare module 'mongoose' {
300
300
  /** Declares a full text index. */
301
301
  text(bool: boolean): this;
302
302
 
303
+ toJSONSchema(options?: { useBsonType?: boolean }): Record<string, any>;
304
+
303
305
  /** Defines a custom function for transforming this path when converting a document to JSON. */
304
306
  transform(fn: (value: any) => any): this;
305
307