mongoose 8.18.2 → 8.19.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/connection.js CHANGED
@@ -1025,7 +1025,7 @@ Connection.prototype.onOpen = function() {
1025
1025
  * @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
1026
1026
  * @param {Number} [options.bufferTimeoutMS=10000] Mongoose specific option. If `bufferCommands` is true, Mongoose will throw an error after `bufferTimeoutMS` if the operation is still buffered.
1027
1027
  * @param {String} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
1028
- * @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
1028
+ * @param {String} [options.user] username for authentication, equivalent to `options.auth.username`. Maintained for backwards compatibility.
1029
1029
  * @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
1030
1030
  * @param {Number} [options.maxPoolSize=100] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
1031
1031
  * @param {Number} [options.minPoolSize=0] The minimum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
package/lib/document.js CHANGED
@@ -766,6 +766,8 @@ function init(self, obj, doc, opts, prefix) {
766
766
  doc[i] = {};
767
767
  if (!strict && !(i in docSchema.tree) && !(i in docSchema.methods) && !(i in docSchema.virtuals)) {
768
768
  self[i] = doc[i];
769
+ } else if (opts?.virtuals && (i in docSchema.virtuals)) {
770
+ self[i] = doc[i];
769
771
  }
770
772
  }
771
773
  init(self, value, doc[i], opts, path + '.');
@@ -773,6 +775,8 @@ function init(self, obj, doc, opts, prefix) {
773
775
  doc[i] = value;
774
776
  if (!strict && !prefix) {
775
777
  self[i] = value;
778
+ } else if (opts?.virtuals && (i in docSchema.virtuals)) {
779
+ self[i] = value;
776
780
  }
777
781
  } else {
778
782
  // Retain order when overwriting defaults
@@ -789,9 +793,9 @@ function init(self, obj, doc, opts, prefix) {
789
793
  if (opts && opts.setters) {
790
794
  // Call applySetters with `init = false` because otherwise setters are a noop
791
795
  const overrideInit = false;
792
- doc[i] = schemaType.applySetters(value, self, overrideInit);
796
+ doc[i] = schemaType.applySetters(value, self, overrideInit, null, opts);
793
797
  } else {
794
- doc[i] = schemaType.cast(value, self, true);
798
+ doc[i] = schemaType.cast(value, self, true, undefined, opts);
795
799
  }
796
800
  } catch (e) {
797
801
  self.invalidate(e.path, new ValidatorError({
@@ -213,9 +213,9 @@ function castPipelineOperator(op, val) {
213
213
  * @api private
214
214
  */
215
215
 
216
- function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
216
+ function walkUpdatePath(schema, obj, op, options, context, filter, prefix) {
217
217
  const strict = options.strict;
218
- const prefix = pref ? pref + '.' : '';
218
+ prefix = prefix ? prefix + '.' : '';
219
219
  const keys = Object.keys(obj);
220
220
  let i = keys.length;
221
221
  let hasKeys = false;
@@ -380,7 +380,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
380
380
  }
381
381
  } else {
382
382
  const checkPath = (key === '$each' || key === '$or' || key === '$and' || key === '$in') ?
383
- pref : prefix + key;
383
+ prefix : prefix + key;
384
384
  schematype = schema._getSchema(checkPath);
385
385
 
386
386
  // You can use `$setOnInsert` with immutable keys
@@ -24,7 +24,7 @@ module.exports = function getPath(schema, path, discriminatorValueMap) {
24
24
  cur = cur.length === 0 ? piece : cur + '.' + piece;
25
25
 
26
26
  schematype = schema.path(cur);
27
- if (schematype != null && schematype.schema) {
27
+ if (schematype?.schema) {
28
28
  schema = schematype.schema;
29
29
  if (!isArray && schematype.$isMongooseDocumentArray) {
30
30
  isArray = true;
@@ -33,6 +33,9 @@ module.exports = function getPath(schema, path, discriminatorValueMap) {
33
33
  schema = schema.discriminators[discriminatorValueMap[cur]] ?? schema;
34
34
  }
35
35
  cur = '';
36
+ } else if (schematype?.instance === 'Mixed') {
37
+ // If we found a mixed path, no point in digging further, the end result is always Mixed
38
+ break;
36
39
  }
37
40
  }
38
41
 
package/lib/model.js CHANGED
@@ -3966,6 +3966,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
3966
3966
  * @param {Object} [options] optional options
3967
3967
  * @param {Boolean} [options.setters=false] if true, apply schema setters when hydrating
3968
3968
  * @param {Boolean} [options.hydratedPopulatedDocs=false] if true, populates the docs if passing pre-populated data
3969
+ * @param {Boolean} [options.virtuals=false] if true, sets any virtuals present on `obj`
3969
3970
  * @return {Document} document instance
3970
3971
  * @api public
3971
3972
  */
@@ -3973,6 +3974,10 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
3973
3974
  Model.hydrate = function(obj, projection, options) {
3974
3975
  _checkContext(this, 'hydrate');
3975
3976
 
3977
+ if (options?.virtuals && options?.hydratedPopulatedDocs === false) {
3978
+ throw new MongooseError('Cannot set `hydratedPopulatedDocs` option to false if `virtuals` option is truthy because `virtuals: true` also sets populated virtuals');
3979
+ }
3980
+
3976
3981
  if (projection != null) {
3977
3982
  if (obj != null && obj.$__ != null) {
3978
3983
  obj = obj.toObject(internalToObjectOptions);
@@ -4598,11 +4603,17 @@ async function _populatePath(model, docs, populateOptions) {
4598
4603
 
4599
4604
  function _execPopulateQuery(mod, match, select) {
4600
4605
  let subPopulate = clone(mod.options.populate);
4601
- const queryOptions = Object.assign({
4602
- skip: mod.options.skip,
4603
- limit: mod.options.limit,
4604
- perDocumentLimit: mod.options.perDocumentLimit
4605
- }, mod.options.options);
4606
+ const queryOptions = {};
4607
+ if (mod.options.skip !== undefined) {
4608
+ queryOptions.skip = mod.options.skip;
4609
+ }
4610
+ if (mod.options.limit !== undefined) {
4611
+ queryOptions.limit = mod.options.limit;
4612
+ }
4613
+ if (mod.options.perDocumentLimit !== undefined) {
4614
+ queryOptions.perDocumentLimit = mod.options.perDocumentLimit;
4615
+ }
4616
+ Object.assign(queryOptions, mod.options.options);
4606
4617
 
4607
4618
  if (mod.count) {
4608
4619
  delete queryOptions.skip;
package/lib/mongoose.js CHANGED
@@ -368,7 +368,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
368
368
  * @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html), except for 4 mongoose-specific options explained below.
369
369
  * @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
370
370
  * @param {String} [options.dbName] The name of the database you want to use. If not provided, Mongoose uses the database name from connection string.
371
- * @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
371
+ * @param {String} [options.user] username for authentication, equivalent to `options.auth.username`. Maintained for backwards compatibility.
372
372
  * @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
373
373
  * @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
374
374
  * @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html#promiseLibrary).
@@ -420,7 +420,7 @@ Mongoose.prototype.createConnection = function createConnection(uri, options) {
420
420
  * @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](https://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
421
421
  * @param {Number} [options.bufferTimeoutMS=10000] Mongoose specific option. If `bufferCommands` is true, Mongoose will throw an error after `bufferTimeoutMS` if the operation is still buffered.
422
422
  * @param {String} [options.dbName] The name of the database we want to use. If not provided, use database name from connection string.
423
- * @param {String} [options.user] username for authentication, equivalent to `options.auth.user`. Maintained for backwards compatibility.
423
+ * @param {String} [options.user] username for authentication, equivalent to `options.auth.username`. Maintained for backwards compatibility.
424
424
  * @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
425
425
  * @param {Number} [options.maxPoolSize=100] The maximum number of sockets the MongoDB driver will keep open for this connection. Keep in mind that MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
426
426
  * @param {Number} [options.minPoolSize=0] The minimum number of sockets the MongoDB driver will keep open for this connection.
@@ -492,7 +492,7 @@ SchemaDocumentArray.prototype.cast = function(value, doc, init, prev, options) {
492
492
  }
493
493
 
494
494
  subdoc = new Constructor(null, value, initDocumentOptions, selected, i);
495
- rawArray[i] = subdoc.$init(rawArray[i]);
495
+ rawArray[i] = subdoc.$init(rawArray[i], options);
496
496
  } else {
497
497
  if (prev && typeof prev.id === 'function') {
498
498
  subdoc = prev.id(rawArray[i]._id);
package/lib/schema/map.js CHANGED
@@ -39,7 +39,7 @@ class SchemaMap extends SchemaType {
39
39
  if (_val == null) {
40
40
  _val = map.$__schemaType._castNullish(_val);
41
41
  } else {
42
- _val = map.$__schemaType.cast(_val, doc, true, null, { path: path + '.' + key });
42
+ _val = map.$__schemaType.cast(_val, doc, true, null, { ...options, path: path + '.' + key });
43
43
  }
44
44
  map.$init(key, _val);
45
45
  }
@@ -49,7 +49,7 @@ class SchemaMap extends SchemaType {
49
49
  if (_val == null) {
50
50
  _val = map.$__schemaType._castNullish(_val);
51
51
  } else {
52
- _val = map.$__schemaType.cast(_val, doc, true, null, { path: path + '.' + key });
52
+ _val = map.$__schemaType.cast(_val, doc, true, null, { ...options, path: path + '.' + key });
53
53
  }
54
54
  map.$init(key, _val);
55
55
  }
@@ -203,7 +203,7 @@ SchemaSubdocument.prototype.cast = function(val, doc, init, priorVal, options) {
203
203
  if (init) {
204
204
  subdoc = new Constructor(void 0, selected, doc, false, { defaults: false });
205
205
  delete subdoc.$__.defaults;
206
- subdoc.$init(val);
206
+ subdoc.$init(val, options);
207
207
  const exclude = isExclusive(selected);
208
208
  applyDefaults(subdoc, selected, exclude);
209
209
  } else {
package/lib/schemaType.js CHANGED
@@ -44,6 +44,7 @@ function SchemaType(path, options, instance) {
44
44
  this[schemaTypeSymbol] = true;
45
45
  this.path = path;
46
46
  this.instance = instance;
47
+ this.schemaName = this.constructor.schemaName;
47
48
  this.validators = [];
48
49
  this.getters = this.constructor.hasOwnProperty('getters') ?
49
50
  this.constructor.getters.slice() :
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.18.2",
4
+ "version": "8.19.0",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
@@ -22,13 +22,14 @@
22
22
  "dependencies": {
23
23
  "bson": "^6.10.4",
24
24
  "kareem": "2.6.3",
25
- "mongodb": "~6.18.0",
25
+ "mongodb": "~6.20.0",
26
26
  "mpath": "0.9.0",
27
27
  "mquery": "5.0.0",
28
28
  "ms": "2.1.3",
29
29
  "sift": "17.1.3"
30
30
  },
31
31
  "devDependencies": {
32
+ "@ark/attest": "0.48.2",
32
33
  "@babel/core": "7.28.3",
33
34
  "@babel/preset-env": "7.28.3",
34
35
  "@mongodb-js/mongodb-downloader": "^0.4.2",
@@ -111,7 +112,8 @@
111
112
  "test-encryption": "mocha --exit ./test/encryption/*.test.js",
112
113
  "tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
113
114
  "test-coverage": "nyc --reporter=html --reporter=text npm test",
114
- "ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check"
115
+ "ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check",
116
+ "attest-benchmark": "node ./benchmarks/typescript/infer.bench.mts"
115
117
  },
116
118
  "main": "./index.js",
117
119
  "types": "./types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -566,11 +566,18 @@ declare module 'mongoose' {
566
566
  static ObjectId: typeof Schema.Types.ObjectId;
567
567
  }
568
568
 
569
- export type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number;
570
- export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String;
571
- export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean;
572
- export type DateSchemaDefinition = DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date;
573
- export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId;
569
+ export type NumberSchemaDefinition = typeof Number | 'number' | 'Number' | typeof Schema.Types.Number | Schema.Types.Number;
570
+ export type StringSchemaDefinition = typeof String | 'string' | 'String' | typeof Schema.Types.String | Schema.Types.String;
571
+ export type BooleanSchemaDefinition = typeof Boolean | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean | Schema.Types.Boolean;
572
+ export type DateSchemaDefinition = DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date | Schema.Types.Date;
573
+ export type ObjectIdSchemaDefinition = 'ObjectId' | 'ObjectID' | typeof Schema.Types.ObjectId | Schema.Types.ObjectId | Types.ObjectId;
574
+ export type BufferSchemaDefinition = typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer;
575
+ export type Decimal128SchemaDefinition = 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 | Schema.Types.Decimal128 | Types.Decimal128;
576
+ export type BigintSchemaDefinition = 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | Schema.Types.BigInt | typeof BigInt | BigInt;
577
+ export type UuidSchemaDefinition = 'uuid' | 'UUID' | typeof Schema.Types.UUID | Schema.Types.UUID;
578
+ export type MapSchemaDefinition = MapConstructor | 'Map' | typeof Schema.Types.Map;
579
+ export type UnionSchemaDefinition = 'Union' | 'union' | typeof Schema.Types.Union | Schema.Types.Union;
580
+ export type DoubleSchemaDefinition = 'double' | 'Double' | typeof Schema.Types.Double | Schema.Types.Double;
574
581
 
575
582
  export type SchemaDefinitionWithBuiltInClass<T> = T extends number
576
583
  ? NumberSchemaDefinition
@@ -582,7 +589,9 @@ declare module 'mongoose' {
582
589
  ? DateSchemaDefinition
583
590
  : (Function | string);
584
591
 
585
- export type SchemaDefinitionProperty<T = undefined, EnforcedDocType = any, THydratedDocumentType = HydratedDocument<EnforcedDocType>> = SchemaDefinitionWithBuiltInClass<T>
592
+ export type SchemaDefinitionProperty<T = undefined, EnforcedDocType = any, THydratedDocumentType = HydratedDocument<EnforcedDocType>> =
593
+ // ThisType intersection here avoids corrupting ThisType for SchemaTypeOptions (see test gh11828)
594
+ | SchemaDefinitionWithBuiltInClass<T> & ThisType<EnforcedDocType>
586
595
  | SchemaTypeOptions<T extends undefined ? any : T, EnforcedDocType, THydratedDocumentType>
587
596
  | typeof SchemaType
588
597
  | Schema<any, any, any>
@@ -905,8 +914,8 @@ declare module 'mongoose' {
905
914
  * https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
906
915
  */
907
916
  type FlattenProperty<T> = T extends Map<any, infer V>
908
- ? Record<string, V> : T extends TreatAsPrimitives
909
- ? T : T extends Types.DocumentArray<infer ItemType>
917
+ ? Record<string, V> : T extends TreatAsPrimitives
918
+ ? T : T extends Document ? T : T extends Types.DocumentArray<infer ItemType>
910
919
  ? Types.DocumentArray<FlattenMaps<ItemType>> : FlattenMaps<T>;
911
920
 
912
921
  export type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;
@@ -1,24 +1,27 @@
1
1
  import {
2
- IsPathRequired,
3
2
  IsSchemaTypeFromBuiltinClass,
4
- RequiredPaths,
5
- OptionalPaths,
6
- PathWithTypePropertyBaseType,
7
- PathEnumOrString
3
+ PathEnumOrString,
4
+ type OptionalPathKeys,
5
+ type RequiredPathKeys
8
6
  } from './inferschematype';
9
7
 
10
8
  declare module 'mongoose' {
11
- export type InferRawDocType<
12
- DocDefinition,
13
- TSchemaOptions extends Record<any, any> = DefaultSchemaOptions
14
- > = ApplySchemaOptions<{
15
- [
16
- K in keyof (RequiredPaths<DocDefinition, TSchemaOptions['typeKey']> &
17
- OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>)
18
- ]: IsPathRequired<DocDefinition[K], TSchemaOptions['typeKey']> extends true
19
- ? ObtainRawDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>
20
- : ObtainRawDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']> | null;
21
- }, TSchemaOptions>;
9
+ export type InferRawDocType<DocDefinition, TSchemaOptions extends Record<any, any> = DefaultSchemaOptions> = Show<
10
+ ApplySchemaOptions<
11
+ {
12
+ [K in RequiredPathKeys<DocDefinition, TSchemaOptions['typeKey']>]: ObtainRawDocumentPathType<
13
+ DocDefinition[K],
14
+ TSchemaOptions['typeKey']
15
+ >;
16
+ } & {
17
+ [K in OptionalPathKeys<DocDefinition, TSchemaOptions['typeKey']>]?: ObtainRawDocumentPathType<
18
+ DocDefinition[K],
19
+ TSchemaOptions['typeKey']
20
+ > | null;
21
+ },
22
+ TSchemaOptions
23
+ >
24
+ >;
22
25
 
23
26
  /**
24
27
  * @summary Obtains schema Path type.
@@ -26,18 +29,15 @@ declare module 'mongoose' {
26
29
  * @param {PathValueType} PathValueType Document definition path type.
27
30
  * @param {TypeKey} TypeKey A generic refers to document definition.
28
31
  */
29
- type ObtainRawDocumentPathType<
30
- PathValueType,
31
- TypeKey extends string = DefaultTypeKey
32
- > = ResolveRawPathType<
33
- PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? PathValueType[TypeKey] : PathValueType,
34
- PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? Omit<PathValueType, TypeKey> : {},
35
- TypeKey
36
- >;
32
+ type ObtainRawDocumentPathType<PathValueType, TypeKey extends string = DefaultTypeKey> =
33
+ TypeKey extends keyof PathValueType ?
34
+ ResolveRawPathType<PathValueType[TypeKey], Omit<PathValueType, TypeKey>, TypeKey>
35
+ : ResolveRawPathType<PathValueType, {}, TypeKey>;
37
36
 
38
- type UnionToRawPathType<T extends readonly any[]> = T[number] extends infer U
39
- ? ResolveRawPathType<U>
40
- : never;
37
+ // can be efficiently checked like:
38
+ // `[T] extends [neverOrAny] ? T : ...`
39
+ // to avoid edge cases
40
+ type neverOrAny = ' ~neverOrAny~';
41
41
 
42
42
  /**
43
43
  * Same as inferSchemaType, except:
@@ -52,73 +52,54 @@ declare module 'mongoose' {
52
52
  * @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
53
53
  * @returns Number, "Number" or "number" will be resolved to number type.
54
54
  */
55
- type ResolveRawPathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends string = DefaultSchemaOptions['typeKey']> =
56
- PathValueType extends Schema ?
57
- InferSchemaType<PathValueType> :
58
- PathValueType extends (infer Item)[] ?
59
- IfEquals<Item, never, any[], Item extends Schema ?
55
+ type ResolveRawPathType<
56
+ PathValueType,
57
+ Options extends SchemaTypeOptions<PathValueType> = {},
58
+ TypeKey extends string = DefaultSchemaOptions['typeKey']
59
+ > =
60
+ [PathValueType] extends [neverOrAny] ? PathValueType
61
+ : PathValueType extends Schema ? InferSchemaType<PathValueType>
62
+ : PathValueType extends ReadonlyArray<infer Item> ?
63
+ Item extends never ? any[]
64
+ : Item extends Schema ?
60
65
  // If Item is a schema, infer its type.
61
- Array<InferSchemaType<Item>> :
62
- Item extends Record<TypeKey, any> ?
63
- Item[TypeKey] extends Function | String ?
64
- // If Item has a type key that's a string or a callable, it must be a scalar,
65
- // so we can directly obtain its path type.
66
- ObtainRawDocumentPathType<Item, TypeKey>[] :
67
- // If the type key isn't callable, then this is an array of objects, in which case
68
- // we need to call InferRawDocType to correctly infer its type.
69
- Array<InferRawDocType<Item>> :
70
- IsSchemaTypeFromBuiltinClass<Item> extends true ?
71
- ObtainRawDocumentPathType<Item, TypeKey>[] :
72
- IsItRecordAndNotAny<Item> extends true ?
73
- Item extends Record<string, never> ?
74
- ObtainRawDocumentPathType<Item, TypeKey>[] :
75
- Array<InferRawDocType<Item>> :
76
- ObtainRawDocumentPathType<Item, TypeKey>[]
77
- >:
78
- PathValueType extends ReadonlyArray<infer Item> ?
79
- IfEquals<Item, never, any[], Item extends Schema ?
80
- Array<InferSchemaType<Item>> :
81
- Item extends Record<TypeKey, any> ?
82
- Item[TypeKey] extends Function | String ?
83
- ObtainRawDocumentPathType<Item, TypeKey>[] :
84
- InferRawDocType<Item>[]:
85
- IsSchemaTypeFromBuiltinClass<Item> extends true ?
86
- ObtainRawDocumentPathType<Item, TypeKey>[] :
87
- IsItRecordAndNotAny<Item> extends true ?
88
- Item extends Record<string, never> ?
89
- ObtainRawDocumentPathType<Item, TypeKey>[] :
90
- Array<InferRawDocType<Item>> :
91
- ObtainRawDocumentPathType<Item, TypeKey>[]
92
- >:
93
- PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
94
- IfEquals<PathValueType, Schema.Types.String> extends true ? PathEnumOrString<Options['enum']> :
95
- IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']> :
96
- PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
97
- IfEquals<PathValueType, Schema.Types.Number> extends true ? number :
98
- PathValueType extends DateSchemaDefinition ? NativeDate :
99
- IfEquals<PathValueType, Schema.Types.Date> extends true ? NativeDate :
100
- PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
101
- PathValueType extends BooleanSchemaDefinition ? boolean :
102
- IfEquals<PathValueType, Schema.Types.Boolean> extends true ? boolean :
103
- PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId :
104
- IfEquals<PathValueType, Types.ObjectId> extends true ? Types.ObjectId :
105
- IfEquals<PathValueType, Schema.Types.ObjectId> extends true ? Types.ObjectId :
106
- PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
107
- IfEquals<PathValueType, Schema.Types.Decimal128> extends true ? Types.Decimal128 :
108
- IfEquals<PathValueType, Types.Decimal128> extends true ? Types.Decimal128 :
109
- IfEquals<PathValueType, Schema.Types.BigInt> extends true ? bigint :
110
- IfEquals<PathValueType, BigInt> extends true ? bigint :
111
- PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | typeof BigInt ? bigint :
112
- PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
113
- IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
114
- PathValueType extends MapConstructor | 'Map' ? Map<string, ObtainRawDocumentPathType<Options['of']>> :
115
- IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ObtainRawDocumentPathType<Options['of']>> :
116
- PathValueType extends 'Union' | 'union' | typeof Schema.Types.Union ? Options['of'] extends readonly any[] ? UnionToRawPathType<Options['of']> : never :
117
- PathValueType extends ArrayConstructor ? any[] :
118
- PathValueType extends typeof Schema.Types.Mixed ? any:
119
- IfEquals<PathValueType, ObjectConstructor> extends true ? any:
120
- IfEquals<PathValueType, {}> extends true ? any:
121
- PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
122
- PathValueType extends Record<string, any> ? InferRawDocType<PathValueType> :
123
- unknown;
66
+ Array<InferSchemaType<Item>>
67
+ : TypeKey extends keyof Item ?
68
+ Item[TypeKey] extends Function | String ?
69
+ // If Item has a type key that's a string or a callable, it must be a scalar,
70
+ // so we can directly obtain its path type.
71
+ ObtainRawDocumentPathType<Item, TypeKey>[]
72
+ : // If the type key isn't callable, then this is an array of objects, in which case
73
+ // we need to call InferRawDocType to correctly infer its type.
74
+ Array<InferRawDocType<Item>>
75
+ : IsSchemaTypeFromBuiltinClass<Item> extends true ? ResolveRawPathType<Item, { enum: Options['enum'] }, TypeKey>[]
76
+ : IsItRecordAndNotAny<Item> extends true ?
77
+ Item extends Record<string, never> ?
78
+ ObtainRawDocumentPathType<Item, TypeKey>[]
79
+ : Array<InferRawDocType<Item>>
80
+ : ObtainRawDocumentPathType<Item, TypeKey>[]
81
+ : PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']>
82
+ : IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']>
83
+ : PathValueType extends NumberSchemaDefinition ?
84
+ Options['enum'] extends ReadonlyArray<any> ?
85
+ Options['enum'][number]
86
+ : number
87
+ : PathValueType extends DateSchemaDefinition ? NativeDate
88
+ : PathValueType extends BufferSchemaDefinition ? Buffer
89
+ : PathValueType extends BooleanSchemaDefinition ? boolean
90
+ : PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId
91
+ : PathValueType extends Decimal128SchemaDefinition ? Types.Decimal128
92
+ : PathValueType extends BigintSchemaDefinition ? bigint
93
+ : PathValueType extends UuidSchemaDefinition ? Buffer
94
+ : PathValueType extends DoubleSchemaDefinition ? Types.Double
95
+ : PathValueType extends MapSchemaDefinition ? Map<string, ObtainRawDocumentPathType<Options['of']>>
96
+ : PathValueType extends UnionSchemaDefinition ?
97
+ ResolveRawPathType<Options['of'] extends ReadonlyArray<infer Item> ? Item : never>
98
+ : PathValueType extends ArrayConstructor ? any[]
99
+ : PathValueType extends typeof Schema.Types.Mixed ? any
100
+ : IfEquals<PathValueType, ObjectConstructor> extends true ? any
101
+ : IfEquals<PathValueType, {}> extends true ? any
102
+ : PathValueType extends typeof SchemaType ? PathValueType['prototype']
103
+ : PathValueType extends Record<string, any> ? InferRawDocType<PathValueType>
104
+ : unknown;
124
105
  }